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
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
29 changes: 29 additions & 0 deletions docs/rules/no-unused-vars.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Disallow unused variable definitions of v-for directives or scope attributes. (no-unused-vars)

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

## :book: Rule Details

:-1: Examples of **incorrect** code for this rule:

```html
<template>
<ol v-for="i in 5"><!-- "i" is defined but never used. -->
<li>item</li>
</ol>
</template>
```

:+1: Examples of **correct** code for this rule:

```html
<template>
<ol v-for="i in 5">
<li>{{i}}</li><!-- "i" is defined and used. -->
</ol>
</template>
```

## :wrench: Options

Nothing.
47 changes: 47 additions & 0 deletions lib/rules/no-unused-vars.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/**
* @fileoverview warn variable definitions of v-for directives or scope attributes if those are not used.
* @author 薛定谔的猫<hh_2013@foxmail.com>
*/
'use strict'

const utils = require('../utils')

/**
* Creates AST event handlers for require-v-for-key.
*
* @param {RuleContext} context - The rule context.
* @returns {Object} AST event handlers.
*/
function create (context) {
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
})
}
}
}
})
}

// ------------------------------------------------------------------------------
// Rule Definition
// ------------------------------------------------------------------------------

module.exports = {
create,
meta: {
docs: {
description: 'warn variable definitions of v-for directives or scope attributes if those are not used',
category: 'Possible Errors',
recommended: false
},
fixable: null,
schema: []
}
}
67 changes: 67 additions & 0 deletions tests/lib/rules/no-unused-vars.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/**
* @author 薛定谔的猫<hh_2013@foxmail.com>
* @copyright 2017 薛定谔的猫. All rights reserved.
* See LICENSE file in root directory for full license.
*/
'use strict'

// ------------------------------------------------------------------------------
// Requirements
// ------------------------------------------------------------------------------

const RuleTester = require('eslint').RuleTester
const rule = require('../../../lib/rules/no-unused-vars')

// ------------------------------------------------------------------------------
// Tests
// ------------------------------------------------------------------------------

const tester = new RuleTester({
parser: 'vue-eslint-parser',
parserOptions: { ecmaVersion: 2015 }
})

tester.run('no-unused-vars', rule, {
valid: [
{
code: '<template><ol v-for="i in 5"><li>{{i}}</li></ol></template>'
},
{
code: '<template><ol v-for="i in 5"><li :prop="i"></li></ol></template>'
},
{
code: '<template v-for="i in 5"><comp v-for="j in 10">{{i}}{{j}}</comp></template>'
},
{
code: '<template><ol v-for="i in data"><li v-for="f in i">{{ f.bar.baz }}</li></ol></template>'
},
{
code: '<template scope="props">{{props}}</template>'
},
{
code: '<template scope="props"><span v-if="props"></span></template>'
}
],
invalid: [
{
code: '<template><ol v-for="i in 5"><li></li></ol></template>',
errors: ['\'i\' is defined but never used.']
},
{
code: '<template scope="props"></template>',
errors: ['\'props\' is defined but never used.']
},
{
code: '<template v-for="i in 5"><comp v-for="j in 10">{{i}}{{i}}</comp></template>',
errors: ['\'j\' is defined but never used.']
},
{
code: '<template><ol v-for="i in data"><li v-for="f in i"></li></ol></template>',
errors: ['\'f\' is defined but never used.']
},
{
code: '<template><div v-for="(v, i, c) in foo"></div></template>',
errors: ['\'v\' is defined but never used.', '\'i\' is defined but never used.', '\'c\' is defined but never used.']
}
]
})