Skip to content

Commit 3916413

Browse files
author
perrysong
committed
feat: change the rule name to no-root-v-if
1 parent 118568b commit 3916413

File tree

7 files changed

+37
-29
lines changed

7 files changed

+37
-29
lines changed

docs/rules/index.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ Rules in this category are enabled for all presets provided by eslint-plugin-vue
118118
| [vue/valid-v-else](./valid-v-else.md) | enforce valid `v-else` directives | | :three::two::warning: |
119119
| [vue/valid-v-for](./valid-v-for.md) | enforce valid `v-for` directives | | :three::two::warning: |
120120
| [vue/valid-v-html](./valid-v-html.md) | enforce valid `v-html` directives | | :three::two::warning: |
121-
| [vue/valid-v-if-template-root](./valid-v-if-template-root.md) | enforce valid `v-if` directives on root element | | :three::two::warning: |
121+
| [vue/no-root-v-if](./no-root-v-if.md) | enforce valid `v-if` directives on root element | | :three::two::warning: |
122122
| [vue/valid-v-if](./valid-v-if.md) | enforce valid `v-if` directives | | :three::two::warning: |
123123
| [vue/valid-v-is](./valid-v-is.md) | enforce valid `v-is` directives | | :three::warning: |
124124
| [vue/valid-v-memo](./valid-v-memo.md) | enforce valid `v-memo` directives | | :three::warning: |

docs/rules/valid-v-if-template-root.md renamed to docs/rules/no-root-v-if.md

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
---
22
pageClass: rule-details
33
sidebarDepth: 0
4-
title: vue/valid-v-if-template-root
4+
title: vue/no-root-v-if
55
description: enforce valid `v-if` directives on root element
66
---
7-
# vue/valid-v-if-template-root
7+
# vue/no-root-v-if
88

99
> enforce valid `v-if` directives on root element
1010
@@ -17,7 +17,7 @@ This rule checks whether every template root is valid.
1717

1818
This rule reports the template root in the following cases:
1919

20-
<eslint-code-block :rules="{'vue/valid-v-if-template-root': ['error']}">
20+
<eslint-code-block :rules="{'vue/no-root-v-if': ['error']}">
2121

2222
```vue
2323
<!-- `v-if` should not be used on root element without `v-else` -->
@@ -36,5 +36,5 @@ Nothing.
3636

3737
## :mag: Implementation
3838

39-
- [Rule source](https://github.com/vuejs/eslint-plugin-vue/blob/master/lib/rules/valid-v-if-template-root.js)
40-
- [Test source](https://github.com/vuejs/eslint-plugin-vue/blob/master/tests/lib/rules/valid-v-if-template-root.js)
39+
- [Rule source](https://github.com/vuejs/eslint-plugin-vue/blob/master/lib/rules/no-root-v-if.js)
40+
- [Test source](https://github.com/vuejs/eslint-plugin-vue/blob/master/tests/lib/rules/no-root-v-if.js)

lib/configs/essential.js

-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,6 @@ module.exports = {
6262
'vue/valid-v-else': 'error',
6363
'vue/valid-v-for': 'error',
6464
'vue/valid-v-html': 'error',
65-
'vue/valid-v-if-template-root': 'error',
6665
'vue/valid-v-if': 'error',
6766
'vue/valid-v-model': 'error',
6867
'vue/valid-v-on': 'error',

lib/configs/vue3-essential.js

-1
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,6 @@ module.exports = {
7777
'vue/valid-v-else': 'error',
7878
'vue/valid-v-for': 'error',
7979
'vue/valid-v-html': 'error',
80-
'vue/valid-v-if-template-root': 'error',
8180
'vue/valid-v-if': 'error',
8281
'vue/valid-v-is': 'error',
8382
'vue/valid-v-memo': 'error',

lib/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ module.exports = {
219219
'valid-v-else': require('./rules/valid-v-else'),
220220
'valid-v-for': require('./rules/valid-v-for'),
221221
'valid-v-html': require('./rules/valid-v-html'),
222-
'valid-v-if-template-root': require('./rules/valid-v-if-template-root'),
222+
'no-root-v-if': require('./rules/no-root-v-if'),
223223
'valid-v-if': require('./rules/valid-v-if'),
224224
'valid-v-is': require('./rules/valid-v-is'),
225225
'valid-v-memo': require('./rules/valid-v-memo'),

lib/rules/valid-v-if-template-root.js renamed to lib/rules/no-root-v-if.js

+23-18
Original file line numberDiff line numberDiff line change
@@ -10,23 +10,34 @@ const utils = require('../utils')
1010
/**
1111
* Get the number of root element directive
1212
* @param {VNode[]} rootElements The start tag node to check.
13-
* @param {string} directiveName The directive name to check.
1413
*/
15-
function getDirectiveLength(rootElements, directiveName) {
16-
if (!directiveName) return 0
17-
return rootElements.filter(
18-
(element) =>
19-
element.type === 'VElement' && utils.hasDirective(element, directiveName)
20-
).length
14+
function getDirectivesLength(rootElements) {
15+
let ifLength = 0
16+
let elseLength = 0
17+
let elseIfLength = 0
18+
19+
for (const element of rootElements) {
20+
if (element.type === 'VElement') {
21+
if (utils.hasDirective(element, 'if')) ifLength += 1
22+
if (utils.hasDirective(element, 'else')) elseLength += 1
23+
if (utils.hasDirective(element, 'else-if')) elseIfLength += 1
24+
}
25+
}
26+
27+
return {
28+
ifLength,
29+
elseLength,
30+
elseIfLength
31+
}
2132
}
2233

2334
module.exports = {
2435
meta: {
2536
type: 'problem',
2637
docs: {
2738
description: 'enforce valid `v-if` directives on root element',
28-
categories: ['vue3-essential', 'essential'],
29-
url: 'https://eslint.vuejs.org/rules/valid-v-if-template-root.html'
39+
categories: undefined,
40+
url: 'https://eslint.vuejs.org/rules/no-root-v-if.html'
3041
},
3142
fixable: null,
3243
schema: []
@@ -49,16 +60,10 @@ module.exports = {
4960
rootElements.push(child)
5061
}
5162
}
52-
5363
if (rootElements.length === 0) return
54-
const hasRootVIfLength = getDirectiveLength(rootElements, 'if')
55-
const hasRootVElseLength = getDirectiveLength(rootElements, 'else')
56-
const hasRootVElseIfLength = getDirectiveLength(rootElements, 'else-if')
57-
if (
58-
hasRootVIfLength === 1 &&
59-
hasRootVElseLength === 0 &&
60-
hasRootVElseIfLength === 0
61-
) {
64+
const { ifLength, elseLength, elseIfLength } =
65+
getDirectivesLength(rootElements)
66+
if (ifLength === 1 && elseLength === 0 && elseIfLength === 0) {
6267
context.report({
6368
node: element,
6469
loc: element.loc,

tests/lib/rules/valid-v-if-template-root.js renamed to tests/lib/rules/no-root-v-if.js

+7-2
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@
66
'use strict'
77

88
const RuleTester = require('eslint').RuleTester
9-
const rule = require('../../../lib/rules/valid-v-if-template-root')
9+
const rule = require('../../../lib/rules/no-root-v-if')
1010

1111
const tester = new RuleTester({
1212
parser: require.resolve('vue-eslint-parser'),
1313
parserOptions: { ecmaVersion: 2015 }
1414
})
1515

16-
tester.run('valid-v-if-template-root', rule, {
16+
tester.run('no-root-v-if', rule, {
1717
valid: [
1818
{
1919
filename: 'test.vue',
@@ -106,6 +106,11 @@ tester.run('valid-v-if-template-root', rule, {
106106
filename: 'test.vue',
107107
code: '<template><div v-if="foo"></div></template>',
108108
errors: ['`v-if` should not be used on root element without `v-else`.']
109+
},
110+
{
111+
filename: 'test.vue',
112+
code: '<template><div /><div v-if="foo" /></template>',
113+
errors: ['`v-if` should not be used on root element without `v-else`.']
109114
}
110115
]
111116
})

0 commit comments

Comments
 (0)