Skip to content

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

Merged
merged 7 commits into from
Oct 30, 2017
Merged

Conversation

aladdin-add
Copy link
Contributor

@aladdin-add aladdin-add commented Sep 16, 2017

fixes #100

almost finished.:)

I'm not very familiar with the ASTs, so please let me know if missing something.

@aladdin-add aladdin-add force-pushed the issue100 branch 4 times, most recently from 528ca71 to e031c21 Compare September 16, 2017 17:31
@aladdin-add aladdin-add changed the title WIP: New: no-unused-vars (fixes #100). New: no-unused-vars (fixes #100). Sep 16, 2017
@armano2
Copy link
Contributor

armano2 commented Sep 16, 2017

@aladdin-add you are missing tests for nested interations

from what i understund you are doing alot recursive loops with collectUsed.
There is 2 fields witch can come in handy for this:
https://github.com/mysticatea/vue-eslint-parser/blob/master/docs/ast.md#vexpressioncontainer
-> Reference

or:
get all properties and declarations + at end compare them (it will avoid recursive loops)

@aladdin-add
Copy link
Contributor Author

aladdin-add commented Sep 16, 2017

SGTM. is there a way to get all properties and {{}}? (seems it also need recurse, and it's that function collectUsed is doing)

@armano2
Copy link
Contributor

armano2 commented Sep 16, 2017

@aladdin-add

VElement (node) {
  if (node.variables) {
    for (const variable of node.variables) {
      variables.push(varNode)
    }
  }
},

@aladdin-add
Copy link
Contributor Author

aladdin-add commented Sep 16, 2017

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 ast-utils to get all).

@armano2
Copy link
Contributor

armano2 commented Sep 16, 2017

@aladdin-add
whats about:

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

@aladdin-add
Copy link
Contributor Author

great! and it looks better!

@armano2
Copy link
Contributor

armano2 commented Sep 16, 2017

can you add few more tests ?

-> nested v-for and multiple scopes ?
v-for > v-for > [scope]

and usage of foo.bar.baz

@armano2
Copy link
Contributor

armano2 commented Sep 16, 2017

and test for:

<template><ol v-for="i in data"><li v-for="f in i">{{ f.bar.baz }}</li></ol></template>

@mysticatea
Copy link
Member

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
                })
            }
        }
    }
})

@armano2
Copy link
Contributor

armano2 commented Sep 24, 2017

@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

@michalsnik michalsnik added this to the v4.1.0 milestone Oct 9, 2017
Copy link
Member

@mysticatea mysticatea left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thank you!

@mysticatea mysticatea merged commit 43c3f3a into vuejs:master Oct 30, 2017
@aladdin-add aladdin-add deleted the issue100 branch October 30, 2017 04:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Rule Proposal: vue/no-unused-vars
4 participants