Skip to content

Commit cd74105

Browse files
committed
feat(no-duplicate-attr-inheritance): ignore multi root
1 parent 50bde65 commit cd74105

File tree

2 files changed

+33
-7
lines changed

2 files changed

+33
-7
lines changed

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

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ module.exports = {
2626
create(context) {
2727
/** @type {string | number | boolean | RegExp | BigInt | null} */
2828
let inheritsAttrs = true
29+
/** @type {VReference[]} */
30+
const attrsRefs = []
2931

3032
/** @param {ObjectExpression} node */
3133
function processOptions(node) {
@@ -54,22 +56,37 @@ module.exports = {
5456
if (!inheritsAttrs) {
5557
return
5658
}
57-
const attrsRef = node.references.find((reference) => {
59+
const reference = node.references.find((reference) => {
5860
if (reference.variable != null) {
5961
// Not vm reference
6062
return false
6163
}
6264
return reference.id.name === '$attrs'
6365
})
6466

65-
if (attrsRef) {
66-
context.report({
67-
node: attrsRef.id,
68-
messageId: 'noDuplicateAttrInheritance'
69-
})
67+
if (reference) {
68+
attrsRefs.push(reference)
7069
}
7170
}
72-
})
71+
}),
72+
{
73+
'Program:exit'(program) {
74+
const element = program.templateBody
75+
if (element == null) {
76+
return
77+
}
78+
79+
const rootElements = element.children.filter(utils.isVElement)
80+
if (rootElements.length === 1 && attrsRefs.length > 0) {
81+
for (const attrsRef of attrsRefs) {
82+
context.report({
83+
node: attrsRef.id,
84+
messageId: 'noDuplicateAttrInheritance'
85+
})
86+
}
87+
}
88+
}
89+
}
7390
)
7491
}
7592
}

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,15 @@ ruleTester.run('no-duplicate-attr-inheritance', rule, {
4343
</script>
4444
`
4545
},
46+
{
47+
filename: 'test.vue',
48+
code: `
49+
<script setup>
50+
defineOptions({ inheritAttrs: true })
51+
</script>
52+
<template><div v-bind="$attrs"/><div/></template>
53+
`
54+
},
4655
{
4756
filename: 'test.vue',
4857
code: `

0 commit comments

Comments
 (0)