Skip to content

Commit e1621ca

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

File tree

8 files changed

+84
-84
lines changed

8 files changed

+84
-84
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-6
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,22 @@
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
1111
- :exclamation: <badge text="This rule has not been released yet." vertical="middle" type="error"> ***This rule has not been released yet.*** </badge>
12-
- :gear: This rule is included in all of `"plugin:vue/vue3-essential"`, `"plugin:vue/essential"`, `"plugin:vue/vue3-strongly-recommended"`, `"plugin:vue/strongly-recommended"`, `"plugin:vue/vue3-recommended"` and `"plugin:vue/recommended"`.
1312

1413
This rule checks whether every template root is valid.
1514

1615
## :book: Rule Details
1716

1817
This rule reports the template root in the following cases:
1918

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

2221
```vue
2322
<!-- `v-if` should not be used on root element without `v-else` -->
@@ -36,5 +35,5 @@ Nothing.
3635

3736
## :mag: Implementation
3837

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)
38+
- [Rule source](https://github.com/vuejs/eslint-plugin-vue/blob/master/lib/rules/no-root-v-if.js)
39+
- [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/no-root-v-if.js

+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/**
2+
* @author Perry Song
3+
* @copyright 2023 Perry Song. All rights reserved.
4+
* See LICENSE file in root directory for full license.
5+
*/
6+
'use strict'
7+
8+
const utils = require('../utils')
9+
10+
/**
11+
* Get the number of root element directive
12+
* @param {VNode[]} rootElements The start tag node to check.
13+
*/
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+
}
32+
}
33+
34+
module.exports = {
35+
meta: {
36+
type: 'problem',
37+
docs: {
38+
description: 'enforce valid `v-if` directives on root element',
39+
categories: undefined,
40+
url: 'https://eslint.vuejs.org/rules/no-root-v-if.html'
41+
},
42+
fixable: null,
43+
schema: []
44+
},
45+
/** @param {RuleContext} context */
46+
create(context) {
47+
return {
48+
/** @param {Program} program */
49+
Program(program) {
50+
const element = program.templateBody
51+
if (element == null) {
52+
return
53+
}
54+
55+
const rootElements = element.children.filter(utils.isVElement)
56+
if (rootElements.length === 0) return
57+
const { ifLength, elseLength, elseIfLength } =
58+
getDirectivesLength(rootElements)
59+
if (ifLength === 1 && elseLength === 0 && elseIfLength === 0) {
60+
context.report({
61+
node: element,
62+
loc: element.loc,
63+
message:
64+
'`v-if` should not be used on root element without `v-else`.'
65+
})
66+
}
67+
}
68+
}
69+
}
70+
}

lib/rules/valid-v-if-template-root.js

-72
This file was deleted.

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)