-
-
Notifications
You must be signed in to change notification settings - Fork 679
New: no-unused-vars (fixes #100). #187
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
Conversation
528ca71
to
e031c21
Compare
16330c3
to
06706fc
Compare
@aladdin-add you are missing tests for nested interations from what i understund you are doing alot recursive loops with or: |
SGTM. is there a way to get all properties and |
VElement (node) {
if (node.variables) {
for (const variable of node.variables) {
variables.push(varNode)
}
}
}, |
I was a little confused. seems it's just declared variables in the element, and didn't include the inside ones. let's be clear, we need to scan every inside elements(and inside), to check if it is used. (hope it can has a function in |
@aladdin-add function create (context) {
let stack = []
let unused = []
return utils.defineTemplateBodyVisitor(context, {
'VElement:exit' () {
for (const node of unused) {
context.report({
node,
loc: node.loc,
message: `'{{name}}' is defined but never used.`,
data: {
name: node.name
}
})
}
unused = stack.pop()
},
VElement (node) {
stack.push(unused)
unused = []
if (node.variables) {
for (const variable of node.variables) {
unused.push(variable.id)
}
}
},
VExpressionContainer (node) {
if (node.references) {
for (const reference of node.references) {
if (reference.mode !== 'w') {
const name = reference.id.name
unused = unused.filter((el) => el.name !== name)
stack = stack.map((obj) => obj.filter((el) => el.name !== name))
}
}
}
}
})
} btw. please merge code with master 🗡 there is nice update to parser there |
06706fc
to
c627564
Compare
great! and it looks better! |
can you add few more tests ? -> nested v-for and multiple scopes ? and usage of |
and test for:
|
3eb9f78
to
fa13aef
Compare
fa13aef
to
a3651e1
Compare
Thank you for contributing! I'm sorry for late response. I think we can simplify this rule. return utils.defineTemplateBodyVisitor(context, {
VElement(node) {
for (const variable of node.variables) {
if (variable.references.length === 0) {
context.report({
node: variable.id,
loc: variable.id.loc,
message: `'{{name}}' is defined but never used.`,
data: variable.id
})
}
}
}
}) |
@aladdin-add we are missing one more test: <template><div v-for="(v, i, c) in foo"></div></template> should warn about 3 unused variables |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, thank you!
fixes #100
almost finished.:)
I'm not very familiar with the ASTs, so please let me know if missing something.