-
-
Notifications
You must be signed in to change notification settings - Fork 681
⭐️New: Add vue/no-use-v-if-with-v-for
rule
#406
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
michalsnik
merged 23 commits into
vuejs:master
from
ota-meshi:add-no-use-v-if-with-v-for
Jul 11, 2018
Merged
Changes from all commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
98dbf8a
Add Vue.extend support, add missing info about Vue.mixin check in readme
michalsnik 7c4a1d2
Docs: fixes wording in docs (#372)
samturrell cd22a28
Fix: fix script-indent to prevent removing <script> tag (fixes #367) …
mysticatea ee5cc0a
[Update] Make `vue/max-attributes-per-line` fixable (#380)
ota-meshi 6861c81
Update: make `vue/order-in-components` fixable (#381)
ota-meshi 5890808
Fix: no-async-in-computed-properties crash (fixes #390)
mysticatea e79dd8b
Fix: valid-v-on false positive (fixes #351)
mysticatea 733172c
Fix: indent rules false positive (fixes #375)
mysticatea e887a8d
[New] Add `prop-name-casing`
22a3a75
fix message and category
a846dd1
fix category
b1a7cca
fix test message
17d8c2b
Set category to undefined
michalsnik 789a2fc
Add more tests and fix edge case scenario
michalsnik e804ca2
attribute order linting
abbde6f
updating docs, tests and category
60a36c2
[New] Add rule `vue/no-use-v-if-with-v-for` (#2)
ota-meshi 9105525
[fix] `meta.docs.description` should not end with `.` consistent-doc…
ota-meshi 00c319c
Merge branch 'master' into add-no-use-v-if-with-v-for
ota-meshi c4bcc5c
[fix] lint error caused by merging the master for conflict resolution
ota-meshi 342c049
[fix] That `vue/attributes-order` got duplicated when merging README
ota-meshi 2767798
[fixed] document `correct` and `incorrect` the contrary stated
ota-meshi 6709702
[fixed] error message
ota-meshi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,98 @@ | ||
# disallow use v-if on the same element as v-for. (vue/no-use-v-if-with-v-for) | ||
|
||
> Never use `v-if` on the same element as `v-for`. | ||
> | ||
> There are two common cases where this can be tempting: | ||
> | ||
> * To filter items in a list (e.g. `v-for="user in users" v-if="user.isActive"`). In these cases, replace `users` with a new computed property that returns your filtered list (e.g. `activeUsers`). | ||
> | ||
> * To avoid rendering a list if it should be hidden (e.g. `v-for="user in users" v-if="shouldShowUsers"`). In these cases, move the `v-if` to a container element (e.g. `ul`, `ol`). | ||
> | ||
> https://vuejs.org/v2/style-guide/#Avoid-v-if-with-v-for-essential | ||
|
||
|
||
## :book: Rule Details | ||
|
||
:-1: Examples of **incorrect** code for this rule: | ||
|
||
```html | ||
<TodoItem | ||
v-if="complete" | ||
v-for="todo in todos" | ||
:todo="todo" | ||
/> | ||
``` | ||
|
||
In this case, the `v-if` should be written on the wrapper element. | ||
|
||
|
||
```html | ||
<TodoItem | ||
v-for="todo in todos" | ||
v-if="todo.shown" | ||
:todo="todo" | ||
/> | ||
``` | ||
|
||
In this case, the `v-for` list variable should be replace with a computed property that returns your filtered list. | ||
|
||
|
||
:+1: Examples of **correct** code for this rule: | ||
|
||
|
||
```html | ||
<ul v-if="complete"> | ||
<TodoItem | ||
v-for="todo in todos" | ||
:todo="todo" | ||
/> | ||
</ul> | ||
``` | ||
|
||
|
||
|
||
```html | ||
<TodoItem | ||
v-for="todo in shownTodos" | ||
:todo="todo" | ||
/> | ||
``` | ||
|
||
```js | ||
computed: { | ||
shownTodos() { | ||
return this.todos.filter((todo) => todo.shown) | ||
} | ||
} | ||
``` | ||
|
||
## :wrench: Options | ||
|
||
`allowUsingIterationVar` - Enables The `v-if` directive use the reference which is to the variables which are defined by the `v-for` directives. | ||
|
||
```js | ||
'vue/no-use-v-if-with-v-for': ['error', { | ||
allowUsingIterationVar: true // default: false | ||
}] | ||
``` | ||
|
||
:+1: Examples of additional **correct** code for this rule with sample `"allowUsingIterationVar": true` options: | ||
|
||
```html | ||
<TodoItem | ||
v-for="todo in todos" | ||
v-if="todo.shown" | ||
:todo="todo" | ||
/> | ||
``` | ||
|
||
:-1: Examples of additional **incorrect** code for this rule with sample `"allowUsingIterationVar": true` options: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. And this correct |
||
|
||
```html | ||
<TodoItem | ||
v-if="complete" | ||
v-for="todo in todos" | ||
:todo="todo" | ||
/> | ||
``` | ||
|
This file contains hidden or 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 hidden or 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,99 @@ | ||
/** | ||
* @author Yosuke Ota | ||
* | ||
* issue https://github.com/vuejs/eslint-plugin-vue/issues/403 | ||
* Style guide: https://vuejs.org/v2/style-guide/#Avoid-v-if-with-v-for-essential | ||
* | ||
* I implemented it with reference to `no-confusing-v-for-v-if` | ||
*/ | ||
'use strict' | ||
|
||
// ------------------------------------------------------------------------------ | ||
// Requirements | ||
// ------------------------------------------------------------------------------ | ||
|
||
const utils = require('../utils') | ||
|
||
// ------------------------------------------------------------------------------ | ||
// Helpers | ||
// ------------------------------------------------------------------------------ | ||
|
||
/** | ||
* Check whether the given `v-if` node is using the variable which is defined by the `v-for` directive. | ||
* @param {ASTNode} vIf The `v-if` attribute node to check. | ||
* @returns {boolean} `true` if the `v-if` is using the variable which is defined by the `v-for` directive. | ||
*/ | ||
function isUsingIterationVar (vIf) { | ||
return !!getVForUsingIterationVar(vIf) | ||
} | ||
|
||
function getVForUsingIterationVar (vIf) { | ||
const element = vIf.parent.parent | ||
for (var i = 0; i < vIf.value.references.length; i++) { | ||
const reference = vIf.value.references[i] | ||
|
||
const targetVFor = element.variables.find(variable => | ||
variable.id.name === reference.id.name && | ||
variable.kind === 'v-for' | ||
) | ||
if (targetVFor) { | ||
return targetVFor.id.parent | ||
} | ||
} | ||
return undefined | ||
} | ||
|
||
// ------------------------------------------------------------------------------ | ||
// Rule Definition | ||
// ------------------------------------------------------------------------------ | ||
|
||
module.exports = { | ||
meta: { | ||
docs: { | ||
description: 'disallow use v-if on the same element as v-for', | ||
category: undefined, | ||
url: 'https://github.com/vuejs/eslint-plugin-vue/blob/v4.3.0/docs/rules/no-use-v-if-with-v-for.md' | ||
}, | ||
fixable: null, | ||
schema: [{ | ||
type: 'object', | ||
properties: { | ||
allowUsingIterationVar: { | ||
type: 'boolean' | ||
} | ||
} | ||
}] | ||
}, | ||
|
||
create (context) { | ||
const options = context.options[0] || {} | ||
const allowUsingIterationVar = options.allowUsingIterationVar === true // default false | ||
return utils.defineTemplateBodyVisitor(context, { | ||
"VAttribute[directive=true][key.name='if']" (node) { | ||
const element = node.parent.parent | ||
|
||
if (utils.hasDirective(element, 'for')) { | ||
if (isUsingIterationVar(node)) { | ||
if (!allowUsingIterationVar) { | ||
const vFor = getVForUsingIterationVar(node) | ||
context.report({ | ||
node, | ||
loc: node.loc, | ||
message: "The '{{iteratorName}}' variable inside 'v-for' directive should be replaced with a computed property that returns filtered array instead. You should not mix 'v-for' with 'v-if'.", | ||
data: { | ||
iteratorName: vFor.right.name | ||
} | ||
}) | ||
} | ||
} else { | ||
context.report({ | ||
node, | ||
loc: node.loc, | ||
message: "This 'v-if' should be moved to the wrapper element." | ||
}) | ||
} | ||
} | ||
} | ||
}) | ||
} | ||
} |
This file contains hidden or 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,144 @@ | ||
/** | ||
* @author Yosuke Ota | ||
*/ | ||
'use strict' | ||
|
||
// ------------------------------------------------------------------------------ | ||
// Requirements | ||
// ------------------------------------------------------------------------------ | ||
|
||
const RuleTester = require('eslint').RuleTester | ||
const rule = require('../../../lib/rules/no-use-v-if-with-v-for') | ||
|
||
// ------------------------------------------------------------------------------ | ||
// Tests | ||
// ------------------------------------------------------------------------------ | ||
|
||
const tester = new RuleTester({ | ||
parser: 'vue-eslint-parser', | ||
parserOptions: { ecmaVersion: 2015 } | ||
}) | ||
|
||
tester.run('no-use-v-if-with-v-for', rule, { | ||
valid: [ | ||
{ | ||
filename: 'test.vue', | ||
code: '' | ||
}, | ||
{ | ||
filename: 'test.vue', | ||
code: '<template><div><div v-for="x in list" v-if="x"></div></div></template>', | ||
options: [{ allowUsingIterationVar: true }] | ||
}, | ||
{ | ||
filename: 'test.vue', | ||
code: '<template><div><div v-for="x in list" v-if="x.foo"></div></div></template>', | ||
options: [{ allowUsingIterationVar: true }] | ||
}, | ||
{ | ||
filename: 'test.vue', | ||
code: '<template><div><div v-for="(x,i) in list" v-if="i%2==0"></div></div></template>', | ||
options: [{ allowUsingIterationVar: true }] | ||
}, | ||
{ | ||
filename: 'test.vue', | ||
code: '<template><div v-if="shown"><div v-for="(x,i) in list"></div></div></template>' | ||
}, | ||
{ | ||
filename: 'test.vue', | ||
code: ` | ||
<template> | ||
<ul> | ||
<li | ||
v-for="user in activeUsers" | ||
:key="user.id" | ||
> | ||
{{ user.name }} | ||
<li> | ||
</ul> | ||
</template> | ||
` | ||
}, | ||
{ | ||
filename: 'test.vue', | ||
code: ` | ||
<template> | ||
<ul v-if="shouldShowUsers"> | ||
<li | ||
v-for="user in users" | ||
:key="user.id" | ||
> | ||
{{ user.name }} | ||
<li> | ||
</ul> | ||
</template> | ||
` | ||
} | ||
], | ||
invalid: [ | ||
{ | ||
filename: 'test.vue', | ||
code: '<template><div><div v-for="x in list" v-if="shown"></div></div></template>', | ||
errors: [{ | ||
message: "This 'v-if' should be moved to the wrapper element.", | ||
line: 1 | ||
}] | ||
}, | ||
{ | ||
filename: 'test.vue', | ||
code: '<template><div><div v-for="x in list" v-if="list.length>0"></div></div></template>', | ||
errors: [{ | ||
message: "This 'v-if' should be moved to the wrapper element.", | ||
line: 1 | ||
}] | ||
}, | ||
{ | ||
filename: 'test.vue', | ||
code: '<template><div><div v-for="x in list" v-if="x.isActive"></div></div></template>', | ||
errors: [{ | ||
message: "The 'list' variable inside 'v-for' directive should be replaced with a computed property that returns filtered array instead. You should not mix 'v-for' with 'v-if'.", | ||
line: 1 | ||
}] | ||
}, | ||
{ | ||
filename: 'test.vue', | ||
code: ` | ||
<template> | ||
<ul> | ||
<li | ||
v-for="user in users" | ||
v-if="user.isActive" | ||
:key="user.id" | ||
> | ||
{{ user.name }} | ||
<li> | ||
</ul> | ||
</template> | ||
`, | ||
errors: [{ | ||
message: "The 'users' variable inside 'v-for' directive should be replaced with a computed property that returns filtered array instead. You should not mix 'v-for' with 'v-if'.", | ||
line: 6 | ||
}] | ||
}, | ||
{ | ||
filename: 'test.vue', | ||
code: ` | ||
<template> | ||
<ul> | ||
<li | ||
v-for="user in users" | ||
v-if="shouldShowUsers" | ||
:key="user.id" | ||
> | ||
{{ user.name }} | ||
<li> | ||
</ul> | ||
</template> | ||
`, | ||
errors: [{ | ||
message: "This 'v-if' should be moved to the wrapper element.", | ||
line: 6 | ||
}] | ||
} | ||
] | ||
}) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess this is supposed to be
incorrect
right?