Skip to content

Commit f3be1fa

Browse files
author
perrysong
committed
fix: update rule logic and doc
1 parent 80352b7 commit f3be1fa

File tree

4 files changed

+15
-42
lines changed

4 files changed

+15
-42
lines changed

docs/rules/no-root-v-if.md

+4-6
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@
22
pageClass: rule-details
33
sidebarDepth: 0
44
title: vue/no-root-v-if
5-
description: enforce valid `v-if` directives on root element
5+
description: disallow `v-if` directives on root element
66
---
7+
78
# vue/no-root-v-if
89

9-
> enforce valid `v-if` directives on root element
10+
> disallow `v-if` directives on root element
1011
11-
- :exclamation: <badge text="This rule has not been released yet." vertical="middle" type="error"> ***This rule has not been released yet.*** </badge>
12+
- :exclamation: <badge text="This rule has not been released yet." vertical="middle" type="error"> **_This rule has not been released yet._** </badge>
1213

1314
This rule checks whether every template root is valid.
1415

@@ -19,12 +20,9 @@ This rule reports the template root in the following cases:
1920
<eslint-code-block :rules="{'vue/no-root-v-if': ['error']}">
2021

2122
```vue
22-
<!-- `v-if` should not be used on root element without `v-else` -->
2323
<template>
2424
<div v-if="foo"></div>
2525
</template>
26-
27-
<template><custom-component v-if="shouldShow" /></template>
2826
```
2927

3028
</eslint-code-block>

lib/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ module.exports = {
124124
'no-restricted-static-attribute': require('./rules/no-restricted-static-attribute'),
125125
'no-restricted-syntax': require('./rules/no-restricted-syntax'),
126126
'no-restricted-v-bind': require('./rules/no-restricted-v-bind'),
127+
'no-root-v-if': require('./rules/no-root-v-if'),
127128
'no-setup-props-destructure': require('./rules/no-setup-props-destructure'),
128129
'no-shared-component-data': require('./rules/no-shared-component-data'),
129130
'no-side-effects-in-computed-properties': require('./rules/no-side-effects-in-computed-properties'),
@@ -220,7 +221,6 @@ module.exports = {
220221
'valid-v-else': require('./rules/valid-v-else'),
221222
'valid-v-for': require('./rules/valid-v-for'),
222223
'valid-v-html': require('./rules/valid-v-html'),
223-
'no-root-v-if': require('./rules/no-root-v-if'),
224224
'valid-v-if': require('./rules/valid-v-if'),
225225
'valid-v-is': require('./rules/valid-v-is'),
226226
'valid-v-memo': require('./rules/valid-v-memo'),

lib/rules/no-root-v-if.js

+6-30
Original file line numberDiff line numberDiff line change
@@ -7,35 +7,11 @@
77

88
const utils = require('../utils')
99

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-
3410
module.exports = {
3511
meta: {
36-
type: 'problem',
12+
type: 'suggestion',
3713
docs: {
38-
description: 'enforce valid `v-if` directives on root element',
14+
description: 'disallow `v-if` directives on root element',
3915
categories: undefined,
4016
url: 'https://eslint.vuejs.org/rules/no-root-v-if.html'
4117
},
@@ -53,10 +29,10 @@ module.exports = {
5329
}
5430

5531
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) {
32+
if (
33+
rootElements.length === 1 &&
34+
utils.hasDirective(rootElements[0], 'if')
35+
) {
6036
context.report({
6137
node: element,
6238
loc: element.loc,

tests/lib/rules/no-root-v-if.js

+4-5
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,10 @@ tester.run('no-root-v-if', rule, {
9494
{
9595
filename: 'test.vue',
9696
code: '<template> <div v-if="mode === \'a\'"></div><div v-if="mode === \'b\'"></div></template>'
97+
},
98+
{
99+
filename: 'test.vue',
100+
code: '<template><div /><div v-if="foo" /></template>'
97101
}
98102
],
99103
invalid: [
@@ -106,11 +110,6 @@ tester.run('no-root-v-if', rule, {
106110
filename: 'test.vue',
107111
code: '<template><div v-if="foo"></div></template>',
108112
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`.']
114113
}
115114
]
116115
})

0 commit comments

Comments
 (0)