Skip to content

Commit 43c3f3a

Browse files
aladdin-addmysticatea
authored andcommitted
New: no-unused-vars (fixes #100)(#187)
1 parent 73a9dd4 commit 43c3f3a

File tree

3 files changed

+143
-0
lines changed

3 files changed

+143
-0
lines changed

docs/rules/no-unused-vars.md

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Disallow unused variable definitions of v-for directives or scope attributes. (no-unused-vars)
2+
3+
This rule report variable definitions of v-for directives or scope attributes if those are not used.
4+
5+
## :book: Rule Details
6+
7+
:-1: Examples of **incorrect** code for this rule:
8+
9+
```html
10+
<template>
11+
<ol v-for="i in 5"><!-- "i" is defined but never used. -->
12+
<li>item</li>
13+
</ol>
14+
</template>
15+
```
16+
17+
:+1: Examples of **correct** code for this rule:
18+
19+
```html
20+
<template>
21+
<ol v-for="i in 5">
22+
<li>{{i}}</li><!-- "i" is defined and used. -->
23+
</ol>
24+
</template>
25+
```
26+
27+
## :wrench: Options
28+
29+
Nothing.

lib/rules/no-unused-vars.js

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/**
2+
* @fileoverview warn variable definitions of v-for directives or scope attributes if those are not used.
3+
* @author 薛定谔的猫<[email protected]>
4+
*/
5+
'use strict'
6+
7+
const utils = require('../utils')
8+
9+
/**
10+
* Creates AST event handlers for require-v-for-key.
11+
*
12+
* @param {RuleContext} context - The rule context.
13+
* @returns {Object} AST event handlers.
14+
*/
15+
function create (context) {
16+
return utils.defineTemplateBodyVisitor(context, {
17+
VElement (node) {
18+
for (const variable of node.variables) {
19+
if (variable.references.length === 0) {
20+
context.report({
21+
node: variable.id,
22+
loc: variable.id.loc,
23+
message: `'{{name}}' is defined but never used.`,
24+
data: variable.id
25+
})
26+
}
27+
}
28+
}
29+
})
30+
}
31+
32+
// ------------------------------------------------------------------------------
33+
// Rule Definition
34+
// ------------------------------------------------------------------------------
35+
36+
module.exports = {
37+
create,
38+
meta: {
39+
docs: {
40+
description: 'warn variable definitions of v-for directives or scope attributes if those are not used',
41+
category: 'Possible Errors',
42+
recommended: false
43+
},
44+
fixable: null,
45+
schema: []
46+
}
47+
}

tests/lib/rules/no-unused-vars.js

+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/**
2+
* @author 薛定谔的猫<[email protected]>
3+
* @copyright 2017 薛定谔的猫. All rights reserved.
4+
* See LICENSE file in root directory for full license.
5+
*/
6+
'use strict'
7+
8+
// ------------------------------------------------------------------------------
9+
// Requirements
10+
// ------------------------------------------------------------------------------
11+
12+
const RuleTester = require('eslint').RuleTester
13+
const rule = require('../../../lib/rules/no-unused-vars')
14+
15+
// ------------------------------------------------------------------------------
16+
// Tests
17+
// ------------------------------------------------------------------------------
18+
19+
const tester = new RuleTester({
20+
parser: 'vue-eslint-parser',
21+
parserOptions: { ecmaVersion: 2015 }
22+
})
23+
24+
tester.run('no-unused-vars', rule, {
25+
valid: [
26+
{
27+
code: '<template><ol v-for="i in 5"><li>{{i}}</li></ol></template>'
28+
},
29+
{
30+
code: '<template><ol v-for="i in 5"><li :prop="i"></li></ol></template>'
31+
},
32+
{
33+
code: '<template v-for="i in 5"><comp v-for="j in 10">{{i}}{{j}}</comp></template>'
34+
},
35+
{
36+
code: '<template><ol v-for="i in data"><li v-for="f in i">{{ f.bar.baz }}</li></ol></template>'
37+
},
38+
{
39+
code: '<template scope="props">{{props}}</template>'
40+
},
41+
{
42+
code: '<template scope="props"><span v-if="props"></span></template>'
43+
}
44+
],
45+
invalid: [
46+
{
47+
code: '<template><ol v-for="i in 5"><li></li></ol></template>',
48+
errors: ['\'i\' is defined but never used.']
49+
},
50+
{
51+
code: '<template scope="props"></template>',
52+
errors: ['\'props\' is defined but never used.']
53+
},
54+
{
55+
code: '<template v-for="i in 5"><comp v-for="j in 10">{{i}}{{i}}</comp></template>',
56+
errors: ['\'j\' is defined but never used.']
57+
},
58+
{
59+
code: '<template><ol v-for="i in data"><li v-for="f in i"></li></ol></template>',
60+
errors: ['\'f\' is defined but never used.']
61+
},
62+
{
63+
code: '<template><div v-for="(v, i, c) in foo"></div></template>',
64+
errors: ['\'v\' is defined but never used.', '\'i\' is defined but never used.', '\'c\' is defined but never used.']
65+
}
66+
]
67+
})

0 commit comments

Comments
 (0)