no-duplicate-attr-inheritance.js 1.9 KB

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