Skip to content

feat(no-duplicate-attr-inheritance): ignore multi root #2598

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

Merged
merged 7 commits into from
Nov 27, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 32 additions & 3 deletions docs/rules/no-duplicate-attr-inheritance.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ since: v7.0.0
This rule aims to prevent duplicate attribute inheritance.
This rule to warn to apply `inheritAttrs: false` when it detects `v-bind="$attrs"` being used.

<eslint-code-block :rules="{'vue/no-duplicate-attr-inheritance': ['error']}">
<eslint-code-block :rules="{'vue/no-duplicate-attr-inheritance': ['error', { checkMultiRootNodes: false }]}">

```vue
<template>
Expand All @@ -26,11 +26,12 @@ export default {
/* ✓ GOOD */
inheritAttrs: false
}
</script>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch!

```

</eslint-code-block>

<eslint-code-block :rules="{'vue/no-duplicate-attr-inheritance': ['error']}">
<eslint-code-block :rules="{'vue/no-duplicate-attr-inheritance': ['error', { checkMultiRootNodes: false }]}">

```vue
<template>
Expand All @@ -41,13 +42,41 @@ export default {
/* ✗ BAD */
// inheritAttrs: true (default)
}
</script>
```

</eslint-code-block>

### `"checkMultiRootNodes": true`

<eslint-code-block :rules="{'vue/no-duplicate-attr-inheritance': ['error', { checkMultiRootNodes: true }]}">

```vue
<template>
<div v-bind="$attrs" />
<div />
</template>
<script>
export default {
/* ✗ BAD */
// inheritAttrs: true (default)
}
</script>
```

</eslint-code-block>

## :wrench: Options

Nothing.
```json
{
"vue/no-duplicate-attr-inheritance": ["error", {
"checkMultiRootNodes": false,
}]
}
```

- `"checkMultiRootNodes"` ... If `true`, check and warn when there are multiple root nodes. (Default: `false`)

## :books: Further Reading

Expand Down
86 changes: 78 additions & 8 deletions lib/rules/no-duplicate-attr-inheritance.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,43 @@

const utils = require('../utils')

/** @param {VElement[]} elements */
function isConditionalGroup(elements) {
let hasIf = false
let hasElse = false

for (const element of elements) {
if (utils.hasDirective(element, 'if')) {
if (hasIf || hasElse) {
return false
}
hasIf = true
} else if (utils.hasDirective(element, 'else-if')) {
if (!hasIf || hasElse) {
return false
}
} else if (utils.hasDirective(element, 'else')) {
if (!hasIf || hasElse) {
return false
}
hasElse = true
} else {
return false
}
}

return hasIf && (elements.length === 1 || hasElse)
}

/** @param {VElement[]} elements */
function isMultiRootNodes(elements) {
if (elements.length > 1 && !isConditionalGroup(elements)) {
return true
}

return false
}

module.exports = {
meta: {
type: 'suggestion',
Expand All @@ -17,15 +54,30 @@ module.exports = {
url: 'https://eslint.vuejs.org/rules/no-duplicate-attr-inheritance.html'
},
fixable: null,
schema: [],
schema: [
{
type: 'object',
properties: {
checkMultiRootNodes: {
type: 'boolean'
}
},
additionalProperties: false
}
],
messages: {
noDuplicateAttrInheritance: 'Set "inheritAttrs" to false.'
}
},
/** @param {RuleContext} context */
create(context) {
const options = context.options[0] || {}
const checkMultiRootNodes = options.checkMultiRootNodes === true

/** @type {string | number | boolean | RegExp | BigInt | null} */
let inheritsAttrs = true
/** @type {VReference[]} */
const attrsRefs = []

/** @param {ObjectExpression} node */
function processOptions(node) {
Expand Down Expand Up @@ -54,22 +106,40 @@ module.exports = {
if (!inheritsAttrs) {
return
}
const attrsRef = node.references.find((reference) => {
const reference = node.references.find((reference) => {
if (reference.variable != null) {
// Not vm reference
return false
}
return reference.id.name === '$attrs'
})

if (attrsRef) {
context.report({
node: attrsRef.id,
messageId: 'noDuplicateAttrInheritance'
})
if (reference) {
attrsRefs.push(reference)
}
}
})
}),
{
'Program:exit'(program) {
const element = program.templateBody
if (element == null) {
return
}

const rootElements = element.children.filter(utils.isVElement)

if (!checkMultiRootNodes && isMultiRootNodes(rootElements)) return

if (attrsRefs.length > 0) {
for (const attrsRef of attrsRefs) {
context.report({
node: attrsRef.id,
messageId: 'noDuplicateAttrInheritance'
})
}
}
}
}
)
}
}
112 changes: 112 additions & 0 deletions tests/lib/rules/no-duplicate-attr-inheritance.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,57 @@ ruleTester.run('no-duplicate-attr-inheritance', rule, {
</script>
`
},
// ignore multi root by default
{
filename: 'test.vue',
code: `
<script setup>
defineOptions({ inheritAttrs: true })
</script>
<template><div v-bind="$attrs"/><div/></template>
`
},
{
filename: 'test.vue',
code: `
<template>
<div v-if="condition1"></div>
<div v-if="condition2" v-bind="$attrs"></div>
<div v-else></div>
</template>
`
},
{
filename: 'test.vue',
code: `
<template>
<div v-if="condition1"></div>
<div v-else-if="condition2"></div>
<div v-bind="$attrs"></div>
</template>
`
},
{
filename: 'test.vue',
code: `
<template>
<div v-bind="$attrs"></div>
<div v-if="condition1"></div>
<div v-else></div>
</template>
`
},
{
filename: 'test.vue',
code: `
<template>
<div v-if="condition1"></div>
<div v-else-if="condition2"></div>
<div v-if="condition3" v-bind="$attrs"></div>
</template>
`,
options: [{ checkMultiRootNodes: false }]
},
{
filename: 'test.vue',
code: `
Expand Down Expand Up @@ -151,6 +202,67 @@ ruleTester.run('no-duplicate-attr-inheritance', rule, {
line: 5
}
]
},
{
filename: 'test.vue',
code: `<template><div v-bind="$attrs"></div><div></div></template>`,
options: [{ checkMultiRootNodes: true }],
errors: [{ message: 'Set "inheritAttrs" to false.' }]
},
{
filename: 'test.vue',
code: `
<template>
<div v-if="condition1" v-bind="$attrs"></div>
<div v-else></div>
<div v-if="condition2"></div>
</template>
`,
options: [{ checkMultiRootNodes: true }],
errors: [{ message: 'Set "inheritAttrs" to false.' }]
},
// condition group as a single root node
{
filename: 'test.vue',
code: `
<template>
<div v-if="condition1" v-bind="$attrs"></div>
<div v-else-if="condition2"></div>
<div v-else></div>
</template>
`,
errors: [{ message: 'Set "inheritAttrs" to false.' }]
},
{
filename: 'test.vue',
code: `
<template>
<div v-if="condition1" v-bind="$attrs"></div>
<div v-else-if="condition2"></div>
<div v-else-if="condition3"></div>
<div v-else></div>
</template>
`,
errors: [{ message: 'Set "inheritAttrs" to false.' }]
},
{
filename: 'test.vue',
code: `
<template>
<div v-if="condition1" v-bind="$attrs"></div>
<div v-else></div>
</template>
`,
errors: [{ message: 'Set "inheritAttrs" to false.' }]
},
{
filename: 'test.vue',
code: `
<template>
<div v-if="condition1" v-bind="$attrs"></div>
</template>
`,
errors: [{ message: 'Set "inheritAttrs" to false.' }]
}
]
})
Loading