Skip to content

Commit 62c60d7

Browse files
mysticateamichalsnik
authored andcommitted
Fix: no-invalid-template-root has false positive about empty templates (fixes #41) (#44)
* Fix: `no-invalid-template-root` has false positive about empty templates (fixes #41) * revive warnings of empty * update docs * fix for src attributes * fix error message * `if` → `in`
1 parent c44b155 commit 62c60d7

17 files changed

+55
-39
lines changed

Diff for: docs/rules/no-confusing-v-for-v-if.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ So when they exist on the same node, `v-if` directive should use the reference w
88

99
## 📖 Rule Details
1010

11-
This rule reports the elements which have both `v-for` and `v-if` directives if the following cases:
11+
This rule reports the elements which have both `v-for` and `v-if` directives in the following cases:
1212

1313
- The `v-if` directive does not use the reference which is to the variables which are defined by the `v-for` directives.
1414

Diff for: docs/rules/no-invalid-template-root.md

+11-6
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,21 @@ This rule checks whether every template root is valid.
44

55
## 📖 Rule Details
66

7-
This rule reports the template root if the following cases:
7+
This rule reports the template root in the following cases:
88

9+
- The root is nothing. E.g. `<template></template>`.
910
- The root is text. E.g. `<template>hello</template>`.
1011
- The root is multiple elements. E.g. `<template><div>one</div><div>two</div></template>`.
1112
- The root element has `v-for` directives. E.g. `<template><div v-for="x in list">{{x}}</div></template>`.
12-
- The root element has `v-if`/`v-else-if` directives without `v-else` elements. E.g. `<template><div v-if="foo">hello</div></template>`.
1313
- The root element is `<template>` or `<slot>` elements. E.g. `<template><template>hello</template></template>`.
1414

1515
👎 Examples of **incorrect** code for this rule:
1616

17+
```html
18+
<template>
19+
</template>
20+
```
21+
1722
```html
1823
<template>
1924
<div>hello</div>
@@ -33,17 +38,17 @@ This rule reports the template root if the following cases:
3338
</template>
3439
```
3540

41+
👍 Examples of **correct** code for this rule:
42+
3643
```html
3744
<template>
38-
<div v-if="foo"></div>
45+
<div>abc</div>
3946
</template>
4047
```
4148

42-
👍 Examples of **correct** code for this rule:
43-
4449
```html
4550
<template>
46-
<div>abc</div>
51+
<div v-if="foo"></div>
4752
</template>
4853
```
4954

Diff for: docs/rules/no-invalid-v-bind.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ This rule checks whether every `v-bind` directive is valid.
44

55
## 📖 Rule Details
66

7-
This rule reports `v-bind` directives if the following cases:
7+
This rule reports `v-bind` directives in the following cases:
88

99
- The directive does not have that attribute value. E.g. `<div v-bind:aaa></div>`
1010
- The directive has invalid modifiers. E.g. `<div v-bind:aaa.bbb="ccc"></div>`

Diff for: docs/rules/no-invalid-v-cloak.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ This rule checks whether every `v-cloak` directive is valid.
44

55
## 📖 Rule Details
66

7-
This rule reports `v-cloak` directives if the following cases:
7+
This rule reports `v-cloak` directives in the following cases:
88

99
- The directive has that argument. E.g. `<div v-cloak:aaa></div>`
1010
- The directive has that modifier. E.g. `<div v-cloak.bbb></div>`

Diff for: docs/rules/no-invalid-v-else-if.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ This rule checks whether every `v-else-if` directive is valid.
44

55
## 📖 Rule Details
66

7-
This rule reports `v-else-if` directives if the following cases:
7+
This rule reports `v-else-if` directives in the following cases:
88

99
- The directive has that argument. E.g. `<div v-if="foo"></div><div v-else-if:aaa="bar"></div>`
1010
- The directive has that modifier. E.g. `<div v-if="foo"></div><div v-else-if.bbb="bar"></div>`

Diff for: docs/rules/no-invalid-v-else.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ This rule checks whether every `v-else` directive is valid.
44

55
## 📖 Rule Details
66

7-
This rule reports `v-else` directives if the following cases:
7+
This rule reports `v-else` directives in the following cases:
88

99
- The directive has that argument. E.g. `<div v-if="foo"></div><div v-else:aaa></div>`
1010
- The directive has that modifier. E.g. `<div v-if="foo"></div><div v-else.bbb></div>`

Diff for: docs/rules/no-invalid-v-for.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ This rule checks whether every `v-for` directive is valid.
44

55
## 📖 Rule Details
66

7-
This rule reports `v-for` directives if the following cases:
7+
This rule reports `v-for` directives in the following cases:
88

99
- The directive has that argument. E.g. `<div v-for:aaa></div>`
1010
- The directive has that modifier. E.g. `<div v-for.bbb></div>`

Diff for: docs/rules/no-invalid-v-html.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ This rule checks whether every `v-html` directive is valid.
44

55
## 📖 Rule Details
66

7-
This rule reports `v-html` directives if the following cases:
7+
This rule reports `v-html` directives in the following cases:
88

99
- The directive has that argument. E.g. `<div v-html:aaa></div>`
1010
- The directive has that modifier. E.g. `<div v-html.bbb></div>`

Diff for: docs/rules/no-invalid-v-if.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ This rule checks whether every `v-if` directive is valid.
44

55
## 📖 Rule Details
66

7-
This rule reports `v-if` directives if the following cases:
7+
This rule reports `v-if` directives in the following cases:
88

99
- The directive has that argument. E.g. `<div v-if:aaa="foo"></div>`
1010
- The directive has that modifier. E.g. `<div v-if.bbb="foo"></div>`

Diff for: docs/rules/no-invalid-v-model.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ This rule checks whether every `v-model` directive is valid.
44

55
## 📖 Rule Details
66

7-
This rule reports `v-model` directives if the following cases:
7+
This rule reports `v-model` directives in the following cases:
88

99
- The directive has that argument. E.g. `<input v-model:aaa="foo">`
1010
- The directive has the modifiers which are not supported. E.g. `<input v-model.bbb="foo">`

Diff for: docs/rules/no-invalid-v-on.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ This rule checks whether every `v-on` directive is valid.
44

55
## 📖 Rule Details
66

7-
This rule reports `v-on` directives if the following cases:
7+
This rule reports `v-on` directives in the following cases:
88

99
- The directive does not have that event name. E.g. `<div v-on="foo"></div>`
1010
- The directive has invalid modifiers. E.g. `<div v-on:click.bbb="foo"></div>`

Diff for: docs/rules/no-invalid-v-once.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ This rule checks whether every `v-once` directive is valid.
44

55
## 📖 Rule Details
66

7-
This rule reports `v-once` directives if the following cases:
7+
This rule reports `v-once` directives in the following cases:
88

99
- The directive has that argument. E.g. `<div v-once:aaa></div>`
1010
- The directive has that modifier. E.g. `<div v-once.bbb></div>`

Diff for: docs/rules/no-invalid-v-pre.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ This rule checks whether every `v-pre` directive is valid.
44

55
## 📖 Rule Details
66

7-
This rule reports `v-pre` directives if the following cases:
7+
This rule reports `v-pre` directives in the following cases:
88

99
- The directive has that argument. E.g. `<div v-pre:aaa></div>`
1010
- The directive has that modifier. E.g. `<div v-pre.bbb></div>`

Diff for: docs/rules/no-invalid-v-show.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ This rule checks whether every `v-show` directive is valid.
44

55
## 📖 Rule Details
66

7-
This rule reports `v-show` directives if the following cases:
7+
This rule reports `v-show` directives in the following cases:
88

99
- The directive has that argument. E.g. `<div v-show:aaa></div>`
1010
- The directive has that modifier. E.g. `<div v-show.bbb></div>`

Diff for: docs/rules/no-invalid-v-text.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ This rule checks whether every `v-text` directive is valid.
44

55
## 📖 Rule Details
66

7-
This rule reports `v-text` directives if the following cases:
7+
This rule reports `v-text` directives in the following cases:
88

99
- The directive has that argument. E.g. `<div v-text:aaa></div>`
1010
- The directive has that modifier. E.g. `<div v-text.bbb></div>`

Diff for: lib/rules/no-invalid-template-root.js

+10-11
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,14 @@ function create (context) {
3131
return
3232
}
3333

34+
const hasSrc = utils.hasAttribute(node.startTag, 'src')
3435
const rootElements = []
3536
let extraText = null
3637
let extraElement = null
3738
let vIf = false
3839
for (const child of node.children) {
3940
if (child.type === 'VElement') {
40-
if (rootElements.length === 0) {
41+
if (rootElements.length === 0 && !hasSrc) {
4142
rootElements.push(child)
4243
vIf = utils.hasDirective(child.startTag, 'if')
4344
} else if (vIf && utils.hasDirective(child.startTag, 'else-if')) {
@@ -53,7 +54,13 @@ function create (context) {
5354
}
5455
}
5556

56-
if (extraText != null) {
57+
if (hasSrc && (extraText != null || extraElement != null)) {
58+
context.report({
59+
node: extraText || extraElement,
60+
loc: (extraText || extraElement).loc,
61+
message: "The template root with 'src' attribute is required to be empty."
62+
})
63+
} else if (extraText != null) {
5764
context.report({
5865
node: extraText,
5966
loc: extraText.loc,
@@ -65,7 +72,7 @@ function create (context) {
6572
loc: extraElement.loc,
6673
message: 'The template root requires exactly one element.'
6774
})
68-
} else if (rootElements.length === 0) {
75+
} else if (rootElements.length === 0 && !hasSrc) {
6976
context.report({
7077
node,
7178
loc: node.loc,
@@ -92,14 +99,6 @@ function create (context) {
9299
})
93100
}
94101
}
95-
if (vIf) {
96-
const tag = rootElements[0].startTag
97-
context.report({
98-
node: tag,
99-
loc: tag.loc,
100-
message: "The template root requires the next element which has 'v-else' directives if it has 'v-if' directives."
101-
})
102-
}
103102
}
104103
}
105104
}

Diff for: tests/lib/rules/no-invalid-template-root.js

+20-8
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,18 @@ tester.run('no-invalid-template-root', rule, {
5050
{
5151
filename: 'test.vue',
5252
code: `<template>\n <c1 v-if="1" />\n <c2 v-else-if="1" />\n <c3 v-else />\n</template>`
53+
},
54+
{
55+
filename: 'test.vue',
56+
code: '<template><div v-if="foo"></div></template>'
57+
},
58+
{
59+
filename: 'test.vue',
60+
code: '<template><div v-if="foo"></div><div v-else-if="bar"></div></template>'
61+
},
62+
{
63+
filename: 'test.vue',
64+
code: '<template src="foo.html"></template>'
5365
}
5466
],
5567
invalid: [
@@ -90,23 +102,23 @@ tester.run('no-invalid-template-root', rule, {
90102
},
91103
{
92104
filename: 'test.vue',
93-
code: '<template><div v-if="foo"></div></template>',
94-
errors: ["The template root requires the next element which has 'v-else' directives if it has 'v-if' directives."]
105+
code: '<template><slot></slot></template>',
106+
errors: ["The template root disallows '<slot>' elements."]
95107
},
96108
{
97109
filename: 'test.vue',
98-
code: '<template><div v-if="foo"></div><div v-else-if="bar"></div></template>',
99-
errors: ["The template root requires the next element which has 'v-else' directives if it has 'v-if' directives."]
110+
code: '<template><template></template></template>',
111+
errors: ["The template root disallows '<template>' elements."]
100112
},
101113
{
102114
filename: 'test.vue',
103-
code: '<template><slot></slot></template>',
104-
errors: ["The template root disallows '<slot>' elements."]
115+
code: '<template src="foo.html">abc</template>',
116+
errors: ["The template root with 'src' attribute is required to be empty."]
105117
},
106118
{
107119
filename: 'test.vue',
108-
code: '<template><template></template></template>',
109-
errors: ["The template root disallows '<template>' elements."]
120+
code: '<template src="foo.html"><div></div></template>',
121+
errors: ["The template root with 'src' attribute is required to be empty."]
110122
}
111123
]
112124
})

0 commit comments

Comments
 (0)