Skip to content

Add support for Vue2 functional component to vue/no-unused-properties and vue/no-undef-properties rules #1761

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 1 commit into from
Jan 19, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
19 changes: 16 additions & 3 deletions lib/utils/property-references.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,12 @@ function definePropertyReferenceExtractor(context) {
/** @type {{ toRefNodes: Set<ESNode>, toRefsNodes: Set<ESNode>} | null} */
let toRefSet = null

let isFunctionalTemplate = false
const templateBody = context.getSourceCode().ast.templateBody
if (templateBody) {
isFunctionalTemplate = utils.hasAttribute(templateBody, 'functional')
}

function getToRefSet() {
if (toRefSet) {
return toRefSet
Expand Down Expand Up @@ -578,16 +584,23 @@ function definePropertyReferenceExtractor(context) {

ignoreRef = (name) => globalScope.set.has(name)
}
/** @type {IPropertyReferences[]} */
const references = []
for (const id of node.references
.filter((ref) => ref.variable == null)
.map((ref) => ref.id)) {
if (ignoreRef(id.name)) {
continue
}
references.push(
extractFromName(id.name, id, () => extractFromExpression(id, true))
)
if (!isFunctionalTemplate) {
references.push(
extractFromName(id.name, id, () => extractFromExpression(id, true))
)
} else {
if (id.name === 'props') {
references.push(extractFromExpression(id, true))
}
}
}
return mergePropertyReferences(references)
}
Expand Down
43 changes: 43 additions & 0 deletions tests/lib/rules/no-undef-properties.js
Original file line number Diff line number Diff line change
Expand Up @@ -517,6 +517,24 @@ tester.run('no-undef-properties', rule, {
};
</script>
`
},

// Vue2 functional component
{
filename: 'test.vue',
code: `
<template functional>
<div>{{props.a}} {{props.b}}</div>
</template>
<script>
export default {
props: {
a: String,
b: String,
},
};
</script>`
}
],

Expand Down Expand Up @@ -1086,6 +1104,31 @@ tester.run('no-undef-properties', rule, {
line: 16
}
]
},

// Vue2 functional component
{
filename: 'test.vue',
code: `
<template functional>
<div>{{props.a}} {{props.b}} {{props.c}}</div>
</template>
<script>
export default {
props: {
a: String,
b: String,
},
};
</script>`,
errors: [
{
message: "'c' is not defined.",
line: 3,
column: 46
}
]
}
]
})
46 changes: 46 additions & 0 deletions tests/lib/rules/no-unused-properties.js
Original file line number Diff line number Diff line change
Expand Up @@ -1678,6 +1678,24 @@ tester.run('no-unused-properties', rule, {
<style lang="scss" scoped>
//
</style>`
},

// Vue2 functional component
{
filename: 'test.vue',
code: `
<template functional>
<div>{{props.a}} {{props.b}}</div>
</template>
<script>
export default {
props: {
a: String,
b: String,
},
};
</script>`
}
],

Expand Down Expand Up @@ -2756,6 +2774,34 @@ tester.run('no-unused-properties', rule, {
"'foo.bar.b' of data found, but never used.",
"'foo.baz' of data found, but never used."
]
},

// Vue2 functional component
{
filename: 'test.vue',
code: `
<template functional>
<div>{{a}} {{b}} {{props.c}}</div>
</template>
<script>
export default {
props: {
a: String,
b: String,
},
};
</script>`,
errors: [
{
message: "'a' of property found, but never used.",
line: 9
},
{
message: "'b' of property found, but never used.",
line: 10
}
]
}
]
})