Skip to content

Commit 528ca71

Browse files
committed
WIP: New: no-unused-vars (fixes #100).
1 parent 71929fa commit 528ca71

File tree

3 files changed

+141
-0
lines changed

3 files changed

+141
-0
lines changed

docs/rules/no-unused-vars.md

Lines changed: 29 additions & 0 deletions
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

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/**
2+
* @fileoverview warn variable definitions of v-for directives or scope attributes if those are not used.
3+
* @author 薛定谔的猫<hh_2013@foxmail.com>
4+
*/
5+
'use strict'
6+
7+
const utils = require('../utils')
8+
9+
/**
10+
* Collect used variables recursively.
11+
*
12+
* @param {Node} node - The node to collect.
13+
* @param {Object} used - The object to restore result.
14+
* @returns {Object} used variables.
15+
*/
16+
function collectUsed (node, used) {
17+
if (node.type === 'VExpressionContainer') {
18+
node.references.forEach(ref => {
19+
used[ref.id.name] = true
20+
})
21+
}
22+
23+
if (node.type === 'VElement') {
24+
node.children.forEach(child => collectUsed(child, used))
25+
}
26+
27+
return used
28+
}
29+
30+
/**
31+
* Creates AST event handlers for require-v-for-key.
32+
*
33+
* @param {RuleContext} context - The rule context.
34+
* @returns {Object} AST event handlers.
35+
*/
36+
function create (context) {
37+
utils.registerTemplateBodyVisitor(context, {
38+
"VAttribute[directive=true][key.name='for']": function (node) {
39+
const vars = node.value.expression.left
40+
const used = collectUsed(node.parent.parent, {})
41+
42+
// report unused.
43+
vars.filter(v => !used[v.name]).forEach(v => {
44+
context.report({
45+
node,
46+
loc: v.loc,
47+
message: `'{{name}}' is defined but never used.`,
48+
data: {
49+
name: v.name
50+
}
51+
})
52+
})
53+
}
54+
})
55+
56+
return {}
57+
}
58+
59+
// ------------------------------------------------------------------------------
60+
// Rule Definition
61+
// ------------------------------------------------------------------------------
62+
63+
module.exports = {
64+
create,
65+
meta: {
66+
docs: {
67+
description: 'warn variable definitions of v-for directives or scope attributes if those are not used',
68+
category: 'Possible Errors',
69+
recommended: false
70+
},
71+
fixable: null,
72+
schema: []
73+
}
74+
}

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

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/**
2+
* @author 薛定谔的猫<hh_2013@foxmail.com>
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+
filename: 'test.vue',
28+
code: '<template><ol v-for="i in 5"><li>{{i}}</li></ol></template>'
29+
}
30+
],
31+
invalid: [
32+
{
33+
filename: 'test.vue',
34+
code: '<template><ol v-for="i in 5"></ol></template>',
35+
errors: ['\'i\' is defined but never used.']
36+
}
37+
]
38+
})

0 commit comments

Comments
 (0)