Skip to content

Commit a93a234

Browse files
mysticateamichalsnik
authored andcommitted
Fix: incorrect errors in invalid EOF cases (fixes #268) (#274)
1 parent 3da5cfc commit a93a234

File tree

5 files changed

+49
-28
lines changed

5 files changed

+49
-28
lines changed

lib/rules/html-end-tags.js

+10-9
Original file line numberDiff line numberDiff line change
@@ -22,22 +22,19 @@ const utils = require('../utils')
2222
* @returns {Object} AST event handlers.
2323
*/
2424
function create (context) {
25+
let hasInvalidEOF = false
26+
2527
return utils.defineTemplateBodyVisitor(context, {
2628
VElement (node) {
29+
if (hasInvalidEOF) {
30+
return
31+
}
32+
2733
const name = node.name
2834
const isVoid = utils.isHtmlVoidElementName(name)
2935
const isSelfClosing = node.startTag.selfClosing
3036
const hasEndTag = node.endTag != null
3137

32-
if (isVoid && hasEndTag) {
33-
context.report({
34-
node: node.endTag,
35-
loc: node.endTag.loc,
36-
message: "'<{{name}}>' should not have end tag.",
37-
data: { name },
38-
fix: (fixer) => fixer.remove(node.endTag)
39-
})
40-
}
4138
if (!isVoid && !hasEndTag && !isSelfClosing) {
4239
context.report({
4340
node: node.startTag,
@@ -48,6 +45,10 @@ function create (context) {
4845
})
4946
}
5047
}
48+
}, {
49+
Program (node) {
50+
hasInvalidEOF = utils.hasInvalidEOF(node)
51+
}
5152
})
5253
}
5354

lib/rules/html-self-closing.js

+9
Original file line numberDiff line numberDiff line change
@@ -88,9 +88,14 @@ function isEmpty (node, sourceCode) {
8888
function create (context) {
8989
const sourceCode = context.getSourceCode()
9090
const options = parseOptions(context.options[0])
91+
let hasInvalidEOF = false
9192

9293
return utils.defineTemplateBodyVisitor(context, {
9394
'VElement' (node) {
95+
if (hasInvalidEOF) {
96+
return
97+
}
98+
9499
const elementType = getElementType(node)
95100
const mode = options[elementType]
96101

@@ -131,6 +136,10 @@ function create (context) {
131136
})
132137
}
133138
}
139+
}, {
140+
Program (node) {
141+
hasInvalidEOF = utils.hasInvalidEOF(node)
142+
}
134143
})
135144
}
136145

lib/utils/index.js

+13
Original file line numberDiff line numberDiff line change
@@ -601,5 +601,18 @@ module.exports = {
601601
*/
602602
isSingleLine (node) {
603603
return node.loc.start.line === node.loc.end.line
604+
},
605+
606+
/**
607+
* Check whether the templateBody of the program has invalid EOF or not.
608+
* @param {Program} node The program node to check.
609+
* @returns {boolean} `true` if it has invalid EOF.
610+
*/
611+
hasInvalidEOF (node) {
612+
const body = node.templateBody
613+
if (body == null || body.errors == null) {
614+
return
615+
}
616+
return body.errors.some(error => typeof error.code === 'string' && error.code.startsWith('eof-'))
604617
}
605618
}

tests/lib/rules/html-end-tags.js

+12-18
Original file line numberDiff line numberDiff line change
@@ -54,27 +54,21 @@ tester.run('html-end-tags', rule, {
5454
{
5555
filename: 'test.vue',
5656
code: '<template><div><div/></div></template>'
57+
},
58+
{
59+
filename: 'test.vue',
60+
code: '<template><div a="b>test</div></template>'
61+
},
62+
{
63+
filename: 'test.vue',
64+
code: '<template><div><!--</div></template>'
65+
},
66+
{
67+
filename: 'test.vue',
68+
code: '<template><div><svg><![CDATA[test</svg></div></template>'
5769
}
5870
],
5971
invalid: [
60-
// {
61-
// filename: 'test.vue',
62-
// code: '<template><div><hr></hr></div></template>',
63-
// output: '<template><div><hr></template>',
64-
// errors: ["'<hr>' should not have end tag."]
65-
// },
66-
// {
67-
// filename: 'test.vue',
68-
// code: '<template><div><img></img></div></template>',
69-
// output: '<template><div><img></template>',
70-
// errors: ["'<img>' should not have end tag."]
71-
// },
72-
// {
73-
// filename: 'test.vue',
74-
// code: '<template><div><input></input></div></template>',
75-
// output: '<template><div><input></template>',
76-
// errors: ["'<input>' should not have end tag."]
77-
// },
7872
{
7973
filename: 'test.vue',
8074
code: '<template><div><div></div></template>',

tests/lib/rules/html-self-closing.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,11 @@ tester.run('html-self-closing', rule, {
6565
code: '<template><div><!-- comment --></div></template>',
6666
output: null,
6767
options: [{ html: { normal: 'always' }}]
68-
}
68+
},
69+
70+
// Invalid EOF
71+
'<template><div a=">test</div></template>',
72+
'<template><div><!--test</div></template>'
6973

7074
// other cases are in `invalid` tests.
7175
],

0 commit comments

Comments
 (0)