Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

⭐️New: Add rule no-duplicate-attr-inheritance #627

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
38 changes: 38 additions & 0 deletions 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.
50 changes: 50 additions & 0 deletions 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.'
})
}
}
})
)
}
}
104 changes: 104 additions & 0 deletions 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: '<template><div><div></div></div></template>'
},
{
filename: 'test.vue',
code: `
<template><div><div></div></div></template>
<script>
export default { inheritAttrs: true }
</script>
`
},
{
filename: 'test.vue',
code: `
<template><div><div></div></div></template>
<script>
const data = {};
export default {
...data,
inheritAttrs: true
}
</script>
`
},
{
filename: 'test.vue',
code: `
<template><div><div></div></div></template>
<script>
const inheritAttrs = false;
export default { inheritAttrs }
</script>
`
},
{
filename: 'test.vue',
code: `
<template><div><div v-bind="$attrs"></div></div></template>
<script>
export default { inheritAttrs: false }
</script>
`
},
{
filename: 'test.vue',
code: `
<template><div><div v-bind="$attrs"></div></div></template>
<script>
export default { inheritAttrs: 0 }
</script>
`
}
],

invalid: [
{
filename: 'test.vue',
code: '<template><div><div v-bind="$attrs"></div></div></template>',
errors: ['Set "inheritAttrs" to false.']
},
{
filename: 'test.vue',
code: `
<template><div><div v-bind="$attrs"></div></div></template>
<script>
export default {
inheritAttrs: true
}
</script>
`,
errors: ['Set "inheritAttrs" to false.']
}
]
})
2 changes: 1 addition & 1 deletion tests/lib/utils/index.js
Expand Up @@ -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']
)
})
})