no-duplicate-attr-inheritance.js 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. /**
  2. * @fileoverview Disable inheritAttrs when using v-bind="$attrs"
  3. * @author Hiroki Osame
  4. */
  5. 'use strict'
  6. const utils = require('../utils')
  7. module.exports = {
  8. meta: {
  9. type: 'suggestion',
  10. docs: {
  11. description:
  12. 'enforce `inheritAttrs` to be set to `false` when using `v-bind="$attrs"`',
  13. categories: undefined,
  14. recommended: false,
  15. url: 'https://eslint.vuejs.org/rules/no-duplicate-attr-inheritance.html'
  16. },
  17. fixable: null,
  18. schema: [
  19. // fill in your schema
  20. ]
  21. },
  22. /** @param {RuleContext} context */
  23. create(context) {
  24. /** @type {string | number | boolean | RegExp | BigInt | null} */
  25. let inheritsAttrs = true
  26. return Object.assign(
  27. utils.executeOnVue(context, (node) => {
  28. const inheritAttrsProp = utils.findProperty(node, 'inheritAttrs')
  29. if (inheritAttrsProp && inheritAttrsProp.value.type === 'Literal') {
  30. inheritsAttrs = inheritAttrsProp.value.value
  31. }
  32. }),
  33. utils.defineTemplateBodyVisitor(context, {
  34. /** @param {VExpressionContainer} node */
  35. "VAttribute[directive=true][key.name.name='bind'][key.argument=null] > VExpressionContainer"(
  36. node
  37. ) {
  38. if (!inheritsAttrs) {
  39. return
  40. }
  41. const attrsRef = node.references.find((reference) => {
  42. if (reference.variable != null) {
  43. // Not vm reference
  44. return false
  45. }
  46. return reference.id.name === '$attrs'
  47. })
  48. if (attrsRef) {
  49. context.report({
  50. node: attrsRef.id,
  51. message: 'Set "inheritAttrs" to false.'
  52. })
  53. }
  54. }
  55. })
  56. )
  57. }
  58. }