Skip to content

Commit 8694e4d

Browse files
committed
feat: update
1 parent b1645d7 commit 8694e4d

File tree

3 files changed

+90
-62
lines changed

3 files changed

+90
-62
lines changed

docs/rules/no-duplicate-attr-inheritance.md

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ since: v7.0.0
1515
This rule aims to prevent duplicate attribute inheritance.
1616
This rule to warn to apply `inheritAttrs: false` when it detects `v-bind="$attrs"` being used.
1717

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

2020
```vue
2121
<template>
@@ -26,11 +26,12 @@ export default {
2626
/* ✓ GOOD */
2727
inheritAttrs: false
2828
}
29+
</script>
2930
```
3031

3132
</eslint-code-block>
3233

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

3536
```vue
3637
<template>
@@ -41,13 +42,41 @@ export default {
4142
/* ✗ BAD */
4243
// inheritAttrs: true (default)
4344
}
45+
</script>
46+
```
47+
48+
</eslint-code-block>
49+
50+
### `"checkMultiRootNodes": true`
51+
52+
<eslint-code-block :rules="{'vue/no-duplicate-attr-inheritance': ['error', { checkMultiRootNodes: true }]}">
53+
54+
```vue
55+
<template>
56+
<div v-bind="$attrs" />
57+
<div />
58+
</template>
59+
<script>
60+
export default {
61+
/* ✗ BAD */
62+
// inheritAttrs: true (default)
63+
}
64+
</script>
4465
```
4566

4667
</eslint-code-block>
4768

4869
## :wrench: Options
4970

50-
Nothing.
71+
```json
72+
{
73+
"vue/no-duplicate-attr-inheritance": ["error", {
74+
"checkMultiRootNodes": false,
75+
}]
76+
}
77+
```
78+
79+
- `"checkMultiRootNodes"` ... If `true`, check and warn when there are multiple root nodes. (Default: `false`)
5180

5281
## :books: Further Reading
5382

lib/rules/no-duplicate-attr-inheritance.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ module.exports = {
128128

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

131-
if (checkMultiRootNodes && isMultiRootNodes(rootElements)) return
131+
if (!checkMultiRootNodes && isMultiRootNodes(rootElements)) return
132132

133133
if (attrsRefs.length > 0) {
134134
for (const attrsRef of attrsRefs) {

tests/lib/rules/no-duplicate-attr-inheritance.js

Lines changed: 57 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -43,60 +43,56 @@ ruleTester.run('no-duplicate-attr-inheritance', rule, {
4343
</script>
4444
`
4545
},
46+
// ignore multi root by default
4647
{
4748
filename: 'test.vue',
4849
code: `
4950
<script setup>
5051
defineOptions({ inheritAttrs: true })
5152
</script>
5253
<template><div v-bind="$attrs"/><div/></template>
53-
`,
54-
options: [{ checkMultiRootNodes: true }]
54+
`
5555
},
56-
// ignore multi root
5756
{
5857
filename: 'test.vue',
5958
code: `
60-
<template>
61-
<div v-if="condition1"></div>
62-
<div v-if="condition2" v-bind="$attrs"></div>
63-
<div v-else></div>
64-
</template>
65-
`,
66-
options: [{ checkMultiRootNodes: true }]
59+
<template>
60+
<div v-if="condition1"></div>
61+
<div v-if="condition2" v-bind="$attrs"></div>
62+
<div v-else></div>
63+
</template>
64+
`
6765
},
6866
{
6967
filename: 'test.vue',
7068
code: `
71-
<template>
72-
<div v-if="condition1"></div>
73-
<div v-else-if="condition2"></div>
74-
<div v-bind="$attrs"></div>
75-
</template>
76-
`,
77-
options: [{ checkMultiRootNodes: true }]
69+
<template>
70+
<div v-if="condition1"></div>
71+
<div v-else-if="condition2"></div>
72+
<div v-bind="$attrs"></div>
73+
</template>
74+
`
7875
},
7976
{
8077
filename: 'test.vue',
8178
code: `
82-
<template>
83-
<div v-bind="$attrs"></div>
84-
<div v-if="condition1"></div>
85-
<div v-else></div>
86-
</template>
87-
`,
88-
options: [{ checkMultiRootNodes: true }]
79+
<template>
80+
<div v-bind="$attrs"></div>
81+
<div v-if="condition1"></div>
82+
<div v-else></div>
83+
</template>
84+
`
8985
},
9086
{
9187
filename: 'test.vue',
9288
code: `
93-
<template>
94-
<div v-if="condition1"></div>
95-
<div v-else-if="condition2"></div>
96-
<div v-if="condition3" v-bind="$attrs"></div>
97-
</template>
89+
<template>
90+
<div v-if="condition1"></div>
91+
<div v-else-if="condition2"></div>
92+
<div v-if="condition3" v-bind="$attrs"></div>
93+
</template>
9894
`,
99-
options: [{ checkMultiRootNodes: true }]
95+
options: [{ checkMultiRootNodes: false }]
10096
},
10197
{
10298
filename: 'test.vue',
@@ -207,62 +203,65 @@ ruleTester.run('no-duplicate-attr-inheritance', rule, {
207203
}
208204
]
209205
},
206+
{
207+
filename: 'test.vue',
208+
code: `<template><div v-bind="$attrs"></div><div></div></template>`,
209+
options: [{ checkMultiRootNodes: true }],
210+
errors: [{ message: 'Set "inheritAttrs" to false.' }]
211+
},
210212
{
211213
filename: 'test.vue',
212214
code: `
213-
<script setup>
214-
defineOptions({ inheritAttrs: true })
215-
</script>
216-
<template><div v-bind="$attrs"/><div/></template>
215+
<template>
216+
<div v-if="condition1" v-bind="$attrs"></div>
217+
<div v-else></div>
218+
<div v-if="condition2"></div>
219+
</template>
217220
`,
218-
options: [{ checkMultiRootNodes: false }],
221+
options: [{ checkMultiRootNodes: true }],
219222
errors: [{ message: 'Set "inheritAttrs" to false.' }]
220223
},
221-
// single root with a condition group
224+
// condition group as a single root node
222225
{
223226
filename: 'test.vue',
224227
code: `
225-
<template>
226-
<div v-if="condition1" v-bind="$attrs"></div>
227-
<div v-else-if="condition2"></div>
228-
<div v-else></div>
229-
</template>
228+
<template>
229+
<div v-if="condition1" v-bind="$attrs"></div>
230+
<div v-else-if="condition2"></div>
231+
<div v-else></div>
232+
</template>
230233
`,
231-
options: [{ checkMultiRootNodes: true }],
232234
errors: [{ message: 'Set "inheritAttrs" to false.' }]
233235
},
234236
{
235237
filename: 'test.vue',
236238
code: `
237-
<template>
238-
<div v-if="condition1" v-bind="$attrs"></div>
239-
<div v-else-if="condition2"></div>
240-
<div v-else-if="condition3"></div>
241-
<div v-else></div>
242-
</template>
239+
<template>
240+
<div v-if="condition1" v-bind="$attrs"></div>
241+
<div v-else-if="condition2"></div>
242+
<div v-else-if="condition3"></div>
243+
<div v-else></div>
244+
</template>
243245
`,
244-
options: [{ checkMultiRootNodes: true }],
245246
errors: [{ message: 'Set "inheritAttrs" to false.' }]
246247
},
247248
{
248249
filename: 'test.vue',
249250
code: `
250-
<template>
251-
<div v-if="condition1" v-bind="$attrs"></div>
252-
<div v-else></div>
253-
</template>
251+
<template>
252+
<div v-if="condition1" v-bind="$attrs"></div>
253+
<div v-else></div>
254+
</template>
254255
`,
255-
options: [{ checkMultiRootNodes: true }],
256256
errors: [{ message: 'Set "inheritAttrs" to false.' }]
257257
},
258258
{
259259
filename: 'test.vue',
260260
code: `
261-
<template>
262-
<div v-if="condition1" v-bind="$attrs"></div>
263-
</template>
261+
<template>
262+
<div v-if="condition1" v-bind="$attrs"></div>
263+
</template>
264264
`,
265-
options: [{ checkMultiRootNodes: true }],
266265
errors: [{ message: 'Set "inheritAttrs" to false.' }]
267266
}
268267
]

0 commit comments

Comments
 (0)