Skip to content

Commit 5ed7397

Browse files
authored
Add support for Vue2 functional component to vue/no-unused-properties and vue/no-undef-properties rules (#1761)
1 parent ae34c65 commit 5ed7397

File tree

3 files changed

+105
-3
lines changed

3 files changed

+105
-3
lines changed

Diff for: lib/utils/property-references.js

+16-3
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,12 @@ function definePropertyReferenceExtractor(context) {
9696
/** @type {{ toRefNodes: Set<ESNode>, toRefsNodes: Set<ESNode>} | null} */
9797
let toRefSet = null
9898

99+
let isFunctionalTemplate = false
100+
const templateBody = context.getSourceCode().ast.templateBody
101+
if (templateBody) {
102+
isFunctionalTemplate = utils.hasAttribute(templateBody, 'functional')
103+
}
104+
99105
function getToRefSet() {
100106
if (toRefSet) {
101107
return toRefSet
@@ -578,16 +584,23 @@ function definePropertyReferenceExtractor(context) {
578584

579585
ignoreRef = (name) => globalScope.set.has(name)
580586
}
587+
/** @type {IPropertyReferences[]} */
581588
const references = []
582589
for (const id of node.references
583590
.filter((ref) => ref.variable == null)
584591
.map((ref) => ref.id)) {
585592
if (ignoreRef(id.name)) {
586593
continue
587594
}
588-
references.push(
589-
extractFromName(id.name, id, () => extractFromExpression(id, true))
590-
)
595+
if (!isFunctionalTemplate) {
596+
references.push(
597+
extractFromName(id.name, id, () => extractFromExpression(id, true))
598+
)
599+
} else {
600+
if (id.name === 'props') {
601+
references.push(extractFromExpression(id, true))
602+
}
603+
}
591604
}
592605
return mergePropertyReferences(references)
593606
}

Diff for: tests/lib/rules/no-undef-properties.js

+43
Original file line numberDiff line numberDiff line change
@@ -517,6 +517,24 @@ tester.run('no-undef-properties', rule, {
517517
};
518518
</script>
519519
`
520+
},
521+
522+
// Vue2 functional component
523+
{
524+
filename: 'test.vue',
525+
code: `
526+
<template functional>
527+
<div>{{props.a}} {{props.b}}</div>
528+
</template>
529+
530+
<script>
531+
export default {
532+
props: {
533+
a: String,
534+
b: String,
535+
},
536+
};
537+
</script>`
520538
}
521539
],
522540

@@ -1086,6 +1104,31 @@ tester.run('no-undef-properties', rule, {
10861104
line: 16
10871105
}
10881106
]
1107+
},
1108+
1109+
// Vue2 functional component
1110+
{
1111+
filename: 'test.vue',
1112+
code: `
1113+
<template functional>
1114+
<div>{{props.a}} {{props.b}} {{props.c}}</div>
1115+
</template>
1116+
1117+
<script>
1118+
export default {
1119+
props: {
1120+
a: String,
1121+
b: String,
1122+
},
1123+
};
1124+
</script>`,
1125+
errors: [
1126+
{
1127+
message: "'c' is not defined.",
1128+
line: 3,
1129+
column: 46
1130+
}
1131+
]
10891132
}
10901133
]
10911134
})

Diff for: tests/lib/rules/no-unused-properties.js

+46
Original file line numberDiff line numberDiff line change
@@ -1678,6 +1678,24 @@ tester.run('no-unused-properties', rule, {
16781678
<style lang="scss" scoped>
16791679
//
16801680
</style>`
1681+
},
1682+
1683+
// Vue2 functional component
1684+
{
1685+
filename: 'test.vue',
1686+
code: `
1687+
<template functional>
1688+
<div>{{props.a}} {{props.b}}</div>
1689+
</template>
1690+
1691+
<script>
1692+
export default {
1693+
props: {
1694+
a: String,
1695+
b: String,
1696+
},
1697+
};
1698+
</script>`
16811699
}
16821700
],
16831701

@@ -2756,6 +2774,34 @@ tester.run('no-unused-properties', rule, {
27562774
"'foo.bar.b' of data found, but never used.",
27572775
"'foo.baz' of data found, but never used."
27582776
]
2777+
},
2778+
2779+
// Vue2 functional component
2780+
{
2781+
filename: 'test.vue',
2782+
code: `
2783+
<template functional>
2784+
<div>{{a}} {{b}} {{props.c}}</div>
2785+
</template>
2786+
2787+
<script>
2788+
export default {
2789+
props: {
2790+
a: String,
2791+
b: String,
2792+
},
2793+
};
2794+
</script>`,
2795+
errors: [
2796+
{
2797+
message: "'a' of property found, but never used.",
2798+
line: 9
2799+
},
2800+
{
2801+
message: "'b' of property found, but never used.",
2802+
line: 10
2803+
}
2804+
]
27592805
}
27602806
]
27612807
})

0 commit comments

Comments
 (0)