-
-
Notifications
You must be signed in to change notification settings - Fork 651
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
1 parent
73a9dd4
commit 43c3f3a
Showing
3 changed files
with
143 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: [] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.'] | ||
} | ||
] | ||
}) |