Skip to content

feat: add fixable, no more error when unused variable with prefix _ #1070

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 4 commits into from
Mar 14, 2020
Merged
Show file tree
Hide file tree
Changes from 2 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
2 changes: 1 addition & 1 deletion docs/rules/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ Enforce all the rules in this category, as well as all higher priority rules, wi
| [vue/no-template-key](./no-template-key.md) | disallow `key` attribute on `<template>` | |
| [vue/no-textarea-mustache](./no-textarea-mustache.md) | disallow mustaches in `<textarea>` | |
| [vue/no-unused-components](./no-unused-components.md) | disallow registering components that are not used inside templates | |
| [vue/no-unused-vars](./no-unused-vars.md) | disallow unused variable definitions of v-for directives or scope attributes | |
| [vue/no-unused-vars](./no-unused-vars.md) | disallow unused variable definitions of v-for directives or scope attributes | :wrench: |
| [vue/no-use-v-if-with-v-for](./no-use-v-if-with-v-for.md) | disallow use v-if on the same element as v-for | |
| [vue/require-component-is](./require-component-is.md) | require `v-bind:is` of `<component>` elements | |
| [vue/require-prop-type-constructor](./require-prop-type-constructor.md) | require prop type to be a constructor | :wrench: |
Expand Down
3 changes: 2 additions & 1 deletion docs/rules/no-unused-vars.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@ description: disallow unused variable definitions of v-for directives or scope a
> disallow unused variable definitions of v-for directives or scope attributes

- :gear: This rule is included in all of `"plugin:vue/essential"`, `"plugin:vue/strongly-recommended"` and `"plugin:vue/recommended"`.
- :wrench: The `--fix` option on the [command line](https://eslint.org/docs/user-guide/command-line-interface#fixing-problems) can automatically fix some of the problems reported by this rule.

## :book: Rule Details

This rule report variable definitions of v-for directives or scope attributes if those are not used.

<eslint-code-block :rules="{'vue/no-unused-vars': ['error']}">
<eslint-code-block fix :rules="{'vue/no-unused-vars': ['error']}">

```vue
<template>
Expand Down
31 changes: 27 additions & 4 deletions lib/rules/no-unused-vars.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,26 +18,49 @@ module.exports = {
category: 'essential',
url: 'https://eslint.vuejs.org/rules/no-unused-vars.html'
},
fixable: null,
schema: []
fixable: 'code',
schema: [
{
'type': 'object',
'properties': {
'varIgnorePattern': {
'type': 'string'
}
},
'additionalProperties': false
}
]
},

create (context) {
const option = context.options[0] || { varIgnorePattern: '^_' }
const pattern = option['varIgnorePattern'] || '^_'
// only use for construct a regularExpression
// eslint-disable-next-line no-eval
const regExp = eval(`/${pattern}/`)
return utils.defineTemplateBodyVisitor(context, {
VElement (node) {
const variables = node.variables

for (
let i = variables.length - 1;
i >= 0 && !variables[i].references.length;
i >= 0 && !regExp.test(variables[i].id.name) && !variables[i].references.length;
i--
) {
const variable = variables[i]
context.report({
node: variable.id,
loc: variable.id.loc,
message: `'{{name}}' is defined but never used.`,
data: variable.id
data: variable.id,
suggest: [
{
desc: 'Replace the unused-var with default ignore pattern',
fix: function (fixer) {
return fixer.replaceText(variable.id, `_${variable.id.name}`)
}
}
]
})
}
}
Expand Down
32 changes: 32 additions & 0 deletions tests/lib/rules/no-unused-vars.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,22 @@ tester.run('no-unused-vars', rule, {
},
{
code: '<template><div v-for="x in foo" :[x]></div></template>'
},
{
code: '<template><div v-for="_ in foo" ></div></template>',
options: [{ varIgnorePattern: '^_' }]

},
{
code: '<template><div v-for="_i in foo" ></div></template>'
},
{
code: '<template><div v-for="ignorei in foo" ></div></template>',
options: [{ varIgnorePattern: '^ignore' }]
},
{
code: '<template><div v-for="thisisignore in foo" ></div></template>',
options: [{ varIgnorePattern: 'ignore$' }]
}
],
invalid: [
Expand Down Expand Up @@ -82,6 +98,7 @@ tester.run('no-unused-vars', rule, {
{
code: '<template><div v-for="(a, b, c) in foo"></div></template>',
errors: ["'a' is defined but never used.", "'b' is defined but never used.", "'c' is defined but never used."]

},
{
code: '<template><div v-for="(a, b, c) in foo">{{a}}</div></template>',
Expand All @@ -98,6 +115,21 @@ tester.run('no-unused-vars', rule, {
{
code: '<template><div v-for="x in items">{{value | x}}</div></template>',
errors: ["'x' is defined but never used."]
},
{
code: '<template><div v-for="x in items">{{value}}</div></template>',
options: [{ varIgnorePattern: 'ignore$' }],
errors: ["'x' is defined but never used."]
},
{
code: '<template><span slot-scope="props"></span></template>',
errors: ["'props' is defined but never used."],
options: [{ varIgnorePattern: '^ignore' }]
},
{
code: '<template><span><template scope="props"></template></span></template>',
errors: ["'props' is defined but never used."],
options: [{ varIgnorePattern: '^ignore' }]
}
]
})