@@ -10,23 +10,34 @@ const utils = require('../utils')
10
10
/**
11
11
* Get the number of root element directive
12
12
* @param {VNode[] } rootElements The start tag node to check.
13
- * @param {string } directiveName The directive name to check.
14
13
*/
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
+ }
21
32
}
22
33
23
34
module . exports = {
24
35
meta : {
25
36
type : 'problem' ,
26
37
docs : {
27
38
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'
30
41
} ,
31
42
fixable : null ,
32
43
schema : [ ]
@@ -49,16 +60,10 @@ module.exports = {
49
60
rootElements . push ( child )
50
61
}
51
62
}
52
-
53
63
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 ) {
62
67
context . report ( {
63
68
node : element ,
64
69
loc : element . loc ,
0 commit comments