Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 47dc4ba

Browse files
New: no-template-key (fixes #41)
1 parent fd16c40 commit 47dc4ba

File tree

5 files changed

+155
-0
lines changed

5 files changed

+155
-0
lines changed

‎README.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ The `--fix` option on the command line automatically fixes problems reported by
108108
| :white_check_mark: | [no-invalid-v-show](./docs/rules/no-invalid-v-show.md) | disallow invalid v-show directives. |
109109
| :white_check_mark: | [no-invalid-v-text](./docs/rules/no-invalid-v-text.md) | disallow invalid v-text directives. |
110110
| :white_check_mark: | [no-parsing-error](./docs/rules/no-parsing-error.md) | disallow parsing errors in `<template>`. |
111+
| | [no-template-key](./docs/rules/no-template-key.md) | disallow 'key' attribute on '<template>'. |
111112

112113
<!--RULES_TABLE_END-->
113114

‎docs/rules/no-template-key.md‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# Disallow 'key' attribute on '<template>' (no-template-key)
2+
3+
Vue.js disallows `key` attribute on `<template>` elements.
4+
5+
## 📖 Rule Details
6+
7+
This rule reports the `<template>` elements which have `key` attribute.
8+
9+
👎 Examples of **incorrect** code for this rule:
10+
11+
```html
12+
<template>
13+
<div>
14+
<template key="x"></template>
15+
<template v-bind:key="y"></template>
16+
<template :key="z"></template>
17+
</div>
18+
</template>
19+
```
20+
21+
👍 Examples of **correct** code for this rule:
22+
23+
```html
24+
<template>
25+
<div>
26+
<div key="x"></div>
27+
<template></template>
28+
</div>
29+
</template>
30+
```
31+
32+
## 🔧 Options
33+
34+
Nothing.

‎lib/recommended-rules.js‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ module.exports = {
2525
"vue/no-invalid-v-show": "error",
2626
"vue/no-invalid-v-text": "error",
2727
"vue/no-parsing-error": "error",
28+
"vue/no-template-key": "off",
2829
"vue/no-textarea-mustache": "error",
2930
"vue/require-component-is": "error",
3031
"vue/require-v-for-key": "error",

‎lib/rules/no-template-key.js‎

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/**
2+
* @author Toru Nagashima
3+
* @copyright 2016 Toru Nagashima. 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 utils = require('../utils')
13+
14+
// ------------------------------------------------------------------------------
15+
// Helpers
16+
// ------------------------------------------------------------------------------
17+
18+
/**
19+
* Creates AST event handlers for no-template-key.
20+
*
21+
* @param {RuleContext} context - The rule context.
22+
* @returns {object} AST event handlers.
23+
*/
24+
function create (context) {
25+
utils.registerTemplateBodyVisitor(context, {
26+
"VStartTag[id.name='template']" (node) {
27+
if (utils.hasAttribute(node, 'key') || utils.hasDirective(node, 'bind', 'key')) {
28+
context.report({
29+
node: node,
30+
loc: node.loc,
31+
message: "'<template>' cannot be keyed. Place the key on real elements instead."
32+
})
33+
}
34+
}
35+
})
36+
37+
return {}
38+
}
39+
40+
// ------------------------------------------------------------------------------
41+
// Rule Definition
42+
// ------------------------------------------------------------------------------
43+
44+
module.exports = {
45+
create,
46+
meta: {
47+
docs: {
48+
description: "disallow 'key' attribute on '<template>'.",
49+
category: 'Possible Errors',
50+
recommended: false
51+
},
52+
fixable: false,
53+
schema: []
54+
}
55+
}

‎tests/lib/rules/no-template-key.js‎

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/**
2+
* @author Toru Nagashima
3+
* @copyright 2017 Toru Nagashima. 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-template-key')
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-template-key', rule, {
25+
valid: [
26+
{
27+
filename: 'test.vue',
28+
code: ''
29+
},
30+
{
31+
filename: 'test.vue',
32+
code: '<template><template></template></template>'
33+
},
34+
{
35+
filename: 'test.vue',
36+
code: '<template><div key="foo"></div></template>'
37+
},
38+
{
39+
filename: 'test.vue',
40+
code: '<template><div v-bind:key="foo"></div></template>'
41+
},
42+
{
43+
filename: 'test.vue',
44+
code: '<template><div :key="foo"></div></template>'
45+
}
46+
],
47+
invalid: [
48+
{
49+
filename: 'test.vue',
50+
code: '<template><div><template key="foo"></template></div></template>',
51+
errors: ["'<template>' cannot be keyed. Place the key on real elements instead."]
52+
},
53+
{
54+
filename: 'test.vue',
55+
code: '<template><div><template v-bind:key="foo"></template></div></template>',
56+
errors: ["'<template>' cannot be keyed. Place the key on real elements instead."]
57+
},
58+
{
59+
filename: 'test.vue',
60+
code: '<template><div><template :key="foo"></template></div></template>',
61+
errors: ["'<template>' cannot be keyed. Place the key on real elements instead."]
62+
}
63+
]
64+
})

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /