diff --git a/docs/rules/no-duplicate-attr-inheritance.md b/docs/rules/no-duplicate-attr-inheritance.md new file mode 100644 index 000000000..8cac68c04 --- /dev/null +++ b/docs/rules/no-duplicate-attr-inheritance.md @@ -0,0 +1,38 @@ +# Disable inheritAttrs when using v-bind="$attrs" (vue/no-duplicate-attr-inheritance) + +- :gear: This rule is included in `"plugin:vue/recommended"`. + +Please describe the origin of the rule here. + + +## Rule Details + +This rule aims to... + +Examples of **incorrect** code for this rule: + +```js + +// fill me in + +``` + +Examples of **correct** code for this rule: + +```js + +// fill me in + +``` + +### Options + +If there are any options, describe them here. Otherwise, delete this section. + +## When Not To Use It + +Give a short description of when it would be appropriate to turn off this rule. + +## Further Reading + +If there are other links that describe the issue this rule addresses, please include them here in a bulleted list. diff --git a/lib/rules/no-duplicate-attr-inheritance.js b/lib/rules/no-duplicate-attr-inheritance.js new file mode 100644 index 000000000..f4e9222f6 --- /dev/null +++ b/lib/rules/no-duplicate-attr-inheritance.js @@ -0,0 +1,50 @@ +/** + * @fileoverview Disable inheritAttrs when using v-bind="$attrs" + * @author Hiroki Osame + */ +'use strict' + +const utils = require('../utils') + +// ------------------------------------------------------------------------------ +// Rule Definition +// ------------------------------------------------------------------------------ + +module.exports = { + meta: { + docs: { + description: 'enforce inheritAttrs: false when using v-bind="$attrs"', + category: undefined, + recommended: false, + url: 'https://github.com/vuejs/eslint-plugin-vue/blob/v5.0.0-beta.3/docs/rules/no-duplicate-attr-inheritance.md' + }, + fixable: null, + schema: [ + // fill in your schema + ] + }, + + create (context) { + let inheritsAttrs = true + + return Object.assign( + utils.executeOnVue(context, (node) => { + const inheritAttrsProp = node.properties.find(prop => (prop.type === 'Property' && prop.key.type === 'Identifier' && prop.key.name === 'inheritAttrs')) + + if (inheritAttrsProp && inheritAttrsProp.value.type === 'Literal') { + inheritsAttrs = inheritAttrsProp.value.value + } + }), + utils.defineTemplateBodyVisitor(context, { + "VAttribute[directive=true][key.name='bind'][value.expression.name='$attrs']" (node) { + if (inheritsAttrs) { + context.report({ + node, + message: 'Set "inheritAttrs" to false.' + }) + } + } + }) + ) + } +} diff --git a/tests/lib/rules/no-duplicate-attr-inheritance.js b/tests/lib/rules/no-duplicate-attr-inheritance.js new file mode 100644 index 000000000..e6a47b99d --- /dev/null +++ b/tests/lib/rules/no-duplicate-attr-inheritance.js @@ -0,0 +1,104 @@ +/** + * @fileoverview Disable inheritAttrs when using v-bind="$attrs" + * @author Hiroki Osame + */ +'use strict' + +// ------------------------------------------------------------------------------ +// Requirements +// ------------------------------------------------------------------------------ + +var rule = require('../../../lib/rules/no-duplicate-attr-inheritance') + +var RuleTester = require('eslint').RuleTester + +// ------------------------------------------------------------------------------ +// Tests +// ------------------------------------------------------------------------------ + +var ruleTester = new RuleTester({ + parser: 'vue-eslint-parser', + parserOptions: { + ecmaVersion: 2018, + sourceType: 'module' + } +}) +ruleTester.run('no-duplicate-attr-inheritance', rule, { + + valid: [ + { + filename: 'test.vue', + code: '' + }, + { + filename: 'test.vue', + code: ` + + + ` + }, + { + filename: 'test.vue', + code: ` + + + ` + }, + { + filename: 'test.vue', + code: ` + + + ` + }, + { + filename: 'test.vue', + code: ` + + + ` + }, + { + filename: 'test.vue', + code: ` + + + ` + } + ], + + invalid: [ + { + filename: 'test.vue', + code: '', + errors: ['Set "inheritAttrs" to false.'] + }, + { + filename: 'test.vue', + code: ` + + + `, + errors: ['Set "inheritAttrs" to false.'] + } + ] +}) diff --git a/tests/lib/utils/index.js b/tests/lib/utils/index.js index 067ddc593..d7a385afd 100644 --- a/tests/lib/utils/index.js +++ b/tests/lib/utils/index.js @@ -243,7 +243,7 @@ describe('getRegisteredComponents', () => { assert.deepEqual( utils.getRegisteredComponents(node).map(c => c.name), - ['PrimaryButton', 'secondaryButton', 'the-modal', 'the_dropdown', 'the_input', 'SomeComponent'], + ['PrimaryButton', 'secondaryButton', 'the-modal', 'the_dropdown', 'the_input', 'SomeComponent'] ) }) })