Skip to content

Commit

Permalink
New: no-unused-vars (fixes #100)(#187)
Browse files Browse the repository at this point in the history
  • Loading branch information
aladdin-add authored and mysticatea committed Oct 30, 2017
1 parent 73a9dd4 commit 43c3f3a
Show file tree
Hide file tree
Showing 3 changed files with 143 additions and 0 deletions.
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.']
}
]
})

0 comments on commit 43c3f3a

Please sign in to comment.