Skip to content
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

Update vue/no-unused-properties rule to support <script setup> #1555

Merged
merged 1 commit into from Jul 6, 2021
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
77 changes: 73 additions & 4 deletions lib/rules/no-unused-properties.js
Expand Up @@ -12,9 +12,27 @@ const utils = require('../utils')
const eslintUtils = require('eslint-utils')

/**
* @typedef {import('../utils').ComponentPropertyData} ComponentPropertyData
* @typedef {import('../utils').GroupName} GroupName
* @typedef {import('../utils').VueObjectData} VueObjectData
*/

/**
* @typedef {object} ComponentObjectPropertyData
* @property {string} name
* @property {GroupName} groupName
* @property {'object'} type
* @property {ASTNode} node
* @property {Property} property
*
* @typedef {object} ComponentNonObjectPropertyData
* @property {string} name
* @property {GroupName} groupName
* @property {'array' | 'type'} type
* @property {ASTNode} node
*
* @typedef { ComponentNonObjectPropertyData | ComponentObjectPropertyData } ComponentPropertyData
*/

/**
* @typedef {object} TemplatePropertiesContainer
* @property {UsedProperties} usedProperties
Expand Down Expand Up @@ -256,7 +274,7 @@ class ParamsUsedProperties {
return param
}
if (this.node.params[index]) {
return (this.params[index] = extractParamProperties(
return (this.params[index] = extractParamOrVerProperties(
this.node.params[index],
this.context
))
Expand All @@ -270,7 +288,7 @@ class ParamsUsedProperties {
* @param {RuleContext} context
* @returns {UsedProperties}
*/
function extractParamProperties(node, context) {
function extractParamOrVerProperties(node, context) {
const result = new UsedProperties()

while (node.type === 'AssignmentPattern') {
Expand Down Expand Up @@ -826,7 +844,58 @@ module.exports = {
}

const scriptVisitor = utils.compositingVisitors(
{},
utils.defineScriptSetupVisitor(context, {
onDefinePropsEnter(node, props) {
if (!groups.has('props')) {
return
}
const container = getVueComponentPropertiesContainer(node)

for (const prop of props) {
if (!prop.propName) {
continue
}
if (prop.type === 'object') {
container.properties.push({
type: prop.type,
name: prop.propName,
groupName: 'props',
node: prop.key,
property: prop.node
})
} else {
container.properties.push({
type: prop.type,
name: prop.propName,
groupName: 'props',
node: prop.key
})
}
}
let target = node
if (
target.parent &&
target.parent.type === 'CallExpression' &&
target.parent.arguments[0] === target &&
target.parent.callee.type === 'Identifier' &&
target.parent.callee.name === 'withDefaults'
) {
target = target.parent
}

if (
!target.parent ||
target.parent.type !== 'VariableDeclarator' ||
target.parent.init !== target
) {
return
}

const pattern = target.parent.id
const usedProps = extractParamOrVerProperties(pattern, context)
container.usedPropertiesForProps.merge(usedProps)
}
}),
utils.defineVueVisitor(context, {
onVueObjectEnter(node) {
const container = getVueComponentPropertiesContainer(node)
Expand Down
12 changes: 12 additions & 0 deletions tests/lib/rules/no-unused-properties.js
Expand Up @@ -2400,6 +2400,18 @@ tester.run('no-unused-properties', rule, {
"'a' of data found, but never used.",
"'b' of data found, but never used."
]
},
{
filename: 'test.vue',
code: `
<template>
{{a}}
</template>
<script setup>
const props = defineProps(['a', 'b', 'c'])
props.b
</script>`,
errors: ["'c' of property found, but never used."]
}
]
})