Skip to content

Commit b1645d7

Browse files
committed
feat: add option
1 parent 9d94342 commit b1645d7

File tree

2 files changed

+42
-9
lines changed

2 files changed

+42
-9
lines changed

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

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ function isConditionalGroup(elements) {
3535
}
3636

3737
/** @param {VElement[]} elements */
38-
function isMultiRoot(elements) {
38+
function isMultiRootNodes(elements) {
3939
if (elements.length > 1 && !isConditionalGroup(elements)) {
4040
return true
4141
}
@@ -54,13 +54,26 @@ module.exports = {
5454
url: 'https://eslint.vuejs.org/rules/no-duplicate-attr-inheritance.html'
5555
},
5656
fixable: null,
57-
schema: [],
57+
schema: [
58+
{
59+
type: 'object',
60+
properties: {
61+
checkMultiRootNodes: {
62+
type: 'boolean'
63+
}
64+
},
65+
additionalProperties: false
66+
}
67+
],
5868
messages: {
5969
noDuplicateAttrInheritance: 'Set "inheritAttrs" to false.'
6070
}
6171
},
6272
/** @param {RuleContext} context */
6373
create(context) {
74+
const options = context.options[0] || {}
75+
const checkMultiRootNodes = options.checkMultiRootNodes === true
76+
6477
/** @type {string | number | boolean | RegExp | BigInt | null} */
6578
let inheritsAttrs = true
6679
/** @type {VReference[]} */
@@ -114,8 +127,8 @@ module.exports = {
114127
}
115128

116129
const rootElements = element.children.filter(utils.isVElement)
117-
// ignore multi root
118-
if (isMultiRoot(rootElements)) return
130+
131+
if (checkMultiRootNodes && isMultiRootNodes(rootElements)) return
119132

120133
if (attrsRefs.length > 0) {
121134
for (const attrsRef of attrsRefs) {

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

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@ ruleTester.run('no-duplicate-attr-inheritance', rule, {
5050
defineOptions({ inheritAttrs: true })
5151
</script>
5252
<template><div v-bind="$attrs"/><div/></template>
53-
`
53+
`,
54+
options: [{ checkMultiRootNodes: true }]
5455
},
5556
// ignore multi root
5657
{
@@ -61,7 +62,8 @@ ruleTester.run('no-duplicate-attr-inheritance', rule, {
6162
<div v-if="condition2" v-bind="$attrs"></div>
6263
<div v-else></div>
6364
</template>
64-
`
65+
`,
66+
options: [{ checkMultiRootNodes: true }]
6567
},
6668
{
6769
filename: 'test.vue',
@@ -71,7 +73,8 @@ ruleTester.run('no-duplicate-attr-inheritance', rule, {
7173
<div v-else-if="condition2"></div>
7274
<div v-bind="$attrs"></div>
7375
</template>
74-
`
76+
`,
77+
options: [{ checkMultiRootNodes: true }]
7578
},
7679
{
7780
filename: 'test.vue',
@@ -81,7 +84,8 @@ ruleTester.run('no-duplicate-attr-inheritance', rule, {
8184
<div v-if="condition1"></div>
8285
<div v-else></div>
8386
</template>
84-
`
87+
`,
88+
options: [{ checkMultiRootNodes: true }]
8589
},
8690
{
8791
filename: 'test.vue',
@@ -91,7 +95,8 @@ ruleTester.run('no-duplicate-attr-inheritance', rule, {
9195
<div v-else-if="condition2"></div>
9296
<div v-if="condition3" v-bind="$attrs"></div>
9397
</template>
94-
`
98+
`,
99+
options: [{ checkMultiRootNodes: true }]
95100
},
96101
{
97102
filename: 'test.vue',
@@ -202,6 +207,17 @@ ruleTester.run('no-duplicate-attr-inheritance', rule, {
202207
}
203208
]
204209
},
210+
{
211+
filename: 'test.vue',
212+
code: `
213+
<script setup>
214+
defineOptions({ inheritAttrs: true })
215+
</script>
216+
<template><div v-bind="$attrs"/><div/></template>
217+
`,
218+
options: [{ checkMultiRootNodes: false }],
219+
errors: [{ message: 'Set "inheritAttrs" to false.' }]
220+
},
205221
// single root with a condition group
206222
{
207223
filename: 'test.vue',
@@ -212,6 +228,7 @@ ruleTester.run('no-duplicate-attr-inheritance', rule, {
212228
<div v-else></div>
213229
</template>
214230
`,
231+
options: [{ checkMultiRootNodes: true }],
215232
errors: [{ message: 'Set "inheritAttrs" to false.' }]
216233
},
217234
{
@@ -224,6 +241,7 @@ ruleTester.run('no-duplicate-attr-inheritance', rule, {
224241
<div v-else></div>
225242
</template>
226243
`,
244+
options: [{ checkMultiRootNodes: true }],
227245
errors: [{ message: 'Set "inheritAttrs" to false.' }]
228246
},
229247
{
@@ -234,6 +252,7 @@ ruleTester.run('no-duplicate-attr-inheritance', rule, {
234252
<div v-else></div>
235253
</template>
236254
`,
255+
options: [{ checkMultiRootNodes: true }],
237256
errors: [{ message: 'Set "inheritAttrs" to false.' }]
238257
},
239258
{
@@ -243,6 +262,7 @@ ruleTester.run('no-duplicate-attr-inheritance', rule, {
243262
<div v-if="condition1" v-bind="$attrs"></div>
244263
</template>
245264
`,
265+
options: [{ checkMultiRootNodes: true }],
246266
errors: [{ message: 'Set "inheritAttrs" to false.' }]
247267
}
248268
]

0 commit comments

Comments
 (0)