Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Separate rule that report <template v-for key> from no-template-key r…
…ule. (#1281)
- Loading branch information
Showing
9 changed files
with
280 additions
and
9 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
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
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,58 @@ | ||
--- | ||
pageClass: rule-details | ||
sidebarDepth: 0 | ||
title: vue/no-v-for-template-key | ||
description: disallow `key` attribute on `<template v-for>` | ||
--- | ||
# vue/no-v-for-template-key | ||
> disallow `key` attribute on `<template v-for>` | ||
- :gear: This rule is included in all of `"plugin:vue/essential"`, `"plugin:vue/strongly-recommended"` and `"plugin:vue/recommended"`. | ||
|
||
Vue.js disallows `key` attribute on `<template>` elements. | ||
|
||
## :book: Rule Details | ||
|
||
This rule reports the `<template v-for>` elements which have `key` attribute. | ||
|
||
<eslint-code-block :rules="{'vue/no-v-for-template-key': ['error']}"> | ||
|
||
```vue | ||
<template> | ||
<!-- ✓ GOOD --> | ||
<template v-for="item in list"> | ||
<div :key="item.id" /> | ||
</template> | ||
<!-- ✗ BAD --> | ||
<template v-for="item in list" :key="item.id"> | ||
<div /> | ||
</template> | ||
</template> | ||
``` | ||
|
||
</eslint-code-block> | ||
|
||
::: tip Note | ||
If you want to report keys placed on `<template>` without `v-for`, use the [vue/no-template-key] rule. | ||
::: | ||
|
||
## :wrench: Options | ||
|
||
Nothing. | ||
|
||
## :couple: Related Rules | ||
|
||
- [vue/no-template-key](./no-template-key.md) | ||
|
||
[vue/no-template-key]: ./no-template-key.md | ||
|
||
## :books: Further Reading | ||
|
||
- [API - Special Attributes - key](https://v3.vuejs.org/api/special-attributes.html#key) | ||
- [API (for v2) - Special Attributes - key](https://vuejs.org/v2/api/#key) | ||
|
||
## :mag: Implementation | ||
|
||
- [Rule source](https://github.com/vuejs/eslint-plugin-vue/blob/master/lib/rules/no-v-for-template-key.js) | ||
- [Test source](https://github.com/vuejs/eslint-plugin-vue/blob/master/tests/lib/rules/no-v-for-template-key.js) |
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
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
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
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,48 @@ | ||
/** | ||
* @author Yosuke Ota | ||
*/ | ||
'use strict' | ||
|
||
const utils = require('../utils') | ||
|
||
// ------------------------------------------------------------------------------ | ||
// Rule Definition | ||
// ------------------------------------------------------------------------------ | ||
|
||
module.exports = { | ||
meta: { | ||
type: 'problem', | ||
docs: { | ||
description: 'disallow `key` attribute on `<template v-for>`', | ||
categories: ['essential'], | ||
url: 'https://eslint.vuejs.org/rules/no-v-for-template-key.html' | ||
}, | ||
fixable: null, | ||
schema: [], | ||
messages: { | ||
disallow: | ||
"'<template v-for>' cannot be keyed. Place the key on real elements instead." | ||
} | ||
}, | ||
/** @param {RuleContext} context */ | ||
create(context) { | ||
return utils.defineTemplateBodyVisitor(context, { | ||
/** @param {VDirective} node */ | ||
"VElement[name='template'] > VStartTag > VAttribute[directive=true][key.name.name='for']"( | ||
node | ||
) { | ||
const element = node.parent.parent | ||
const keyNode = | ||
utils.getAttribute(element, 'key') || | ||
utils.getDirective(element, 'bind', 'key') | ||
if (keyNode) { | ||
context.report({ | ||
node: keyNode, | ||
loc: keyNode.loc, | ||
messageId: 'disallow' | ||
}) | ||
} | ||
} | ||
}) | ||
} | ||
} |
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
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,102 @@ | ||
/** | ||
* @author Yosuke Ota | ||
*/ | ||
'use strict' | ||
|
||
// ------------------------------------------------------------------------------ | ||
// Requirements | ||
// ------------------------------------------------------------------------------ | ||
|
||
const RuleTester = require('eslint').RuleTester | ||
const rule = require('../../../lib/rules/no-v-for-template-key') | ||
|
||
// ------------------------------------------------------------------------------ | ||
// Tests | ||
// ------------------------------------------------------------------------------ | ||
|
||
const tester = new RuleTester({ | ||
parser: require.resolve('vue-eslint-parser'), | ||
parserOptions: { ecmaVersion: 2015 } | ||
}) | ||
|
||
tester.run('no-v-for-template-key', rule, { | ||
valid: [ | ||
{ | ||
filename: 'test.vue', | ||
code: '' | ||
}, | ||
{ | ||
filename: 'test.vue', | ||
code: '<template><template></template></template>' | ||
}, | ||
{ | ||
filename: 'test.vue', | ||
code: '<template><div key="foo"></div></template>' | ||
}, | ||
{ | ||
filename: 'test.vue', | ||
code: '<template><div v-bind:key="foo"></div></template>' | ||
}, | ||
{ | ||
filename: 'test.vue', | ||
code: '<template><div :key="foo"></div></template>' | ||
}, | ||
{ | ||
filename: 'test.vue', | ||
code: '<template><div><template key="foo"></template></div></template>' | ||
}, | ||
{ | ||
filename: 'test.vue', | ||
code: | ||
'<template><div><template v-bind:key="foo"></template></div></template>' | ||
}, | ||
{ | ||
filename: 'test.vue', | ||
code: '<template><div><template :key="foo"></template></div></template>' | ||
}, | ||
{ | ||
filename: 'test.vue', | ||
code: | ||
'<template><template v-slot="item" :key="item.id"><div /></template></template>' | ||
}, | ||
{ | ||
filename: 'test.vue', | ||
code: | ||
'<template><template v-for="item in list"><template :key="item.id"><div /></template></template></template>' | ||
} | ||
], | ||
invalid: [ | ||
{ | ||
filename: 'test.vue', | ||
code: | ||
'<template><template v-for="item in list" :key="item.id"><div /></template></template>', | ||
errors: [ | ||
"'<template v-for>' cannot be keyed. Place the key on real elements instead." | ||
] | ||
}, | ||
{ | ||
filename: 'test.vue', | ||
code: | ||
'<template><template v-for="(item, i) in list" :key="i"><div /></template></template>', | ||
errors: [ | ||
"'<template v-for>' cannot be keyed. Place the key on real elements instead." | ||
] | ||
}, | ||
{ | ||
filename: 'test.vue', | ||
code: | ||
'<template><template v-for="item in list" :key="foo + item.id"><div /></template></template>', | ||
errors: [ | ||
"'<template v-for>' cannot be keyed. Place the key on real elements instead." | ||
] | ||
}, | ||
{ | ||
filename: 'test.vue', | ||
code: | ||
'<template><template v-for="item in list" key="foo"><div /></template></template>', | ||
errors: [ | ||
"'<template v-for>' cannot be keyed. Place the key on real elements instead." | ||
] | ||
} | ||
] | ||
}) |