Skip to content

Commit

Permalink
Fix: incorrect errors in invalid EOF cases (fixes #268) (#274)
Browse files Browse the repository at this point in the history
  • Loading branch information
mysticatea authored and michalsnik committed Dec 3, 2017
1 parent 3da5cfc commit a93a234
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 28 deletions.
19 changes: 10 additions & 9 deletions lib/rules/html-end-tags.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,22 +22,19 @@ const utils = require('../utils')
* @returns {Object} AST event handlers.
*/
function create (context) {
let hasInvalidEOF = false

return utils.defineTemplateBodyVisitor(context, {
VElement (node) {
if (hasInvalidEOF) {
return
}

const name = node.name
const isVoid = utils.isHtmlVoidElementName(name)
const isSelfClosing = node.startTag.selfClosing
const hasEndTag = node.endTag != null

if (isVoid && hasEndTag) {
context.report({
node: node.endTag,
loc: node.endTag.loc,
message: "'<{{name}}>' should not have end tag.",
data: { name },
fix: (fixer) => fixer.remove(node.endTag)
})
}
if (!isVoid && !hasEndTag && !isSelfClosing) {
context.report({
node: node.startTag,
Expand All @@ -48,6 +45,10 @@ function create (context) {
})
}
}
}, {
Program (node) {
hasInvalidEOF = utils.hasInvalidEOF(node)
}
})
}

Expand Down
9 changes: 9 additions & 0 deletions lib/rules/html-self-closing.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,14 @@ function isEmpty (node, sourceCode) {
function create (context) {
const sourceCode = context.getSourceCode()
const options = parseOptions(context.options[0])
let hasInvalidEOF = false

return utils.defineTemplateBodyVisitor(context, {
'VElement' (node) {
if (hasInvalidEOF) {
return
}

const elementType = getElementType(node)
const mode = options[elementType]

Expand Down Expand Up @@ -131,6 +136,10 @@ function create (context) {
})
}
}
}, {
Program (node) {
hasInvalidEOF = utils.hasInvalidEOF(node)
}
})
}

Expand Down
13 changes: 13 additions & 0 deletions lib/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -601,5 +601,18 @@ module.exports = {
*/
isSingleLine (node) {
return node.loc.start.line === node.loc.end.line
},

/**
* Check whether the templateBody of the program has invalid EOF or not.
* @param {Program} node The program node to check.
* @returns {boolean} `true` if it has invalid EOF.
*/
hasInvalidEOF (node) {
const body = node.templateBody
if (body == null || body.errors == null) {
return
}
return body.errors.some(error => typeof error.code === 'string' && error.code.startsWith('eof-'))
}
}
30 changes: 12 additions & 18 deletions tests/lib/rules/html-end-tags.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,27 +54,21 @@ tester.run('html-end-tags', rule, {
{
filename: 'test.vue',
code: '<template><div><div/></div></template>'
},
{
filename: 'test.vue',
code: '<template><div a="b>test</div></template>'
},
{
filename: 'test.vue',
code: '<template><div><!--</div></template>'
},
{
filename: 'test.vue',
code: '<template><div><svg><![CDATA[test</svg></div></template>'
}
],
invalid: [
// {
// filename: 'test.vue',
// code: '<template><div><hr></hr></div></template>',
// output: '<template><div><hr></template>',
// errors: ["'<hr>' should not have end tag."]
// },
// {
// filename: 'test.vue',
// code: '<template><div><img></img></div></template>',
// output: '<template><div><img></template>',
// errors: ["'<img>' should not have end tag."]
// },
// {
// filename: 'test.vue',
// code: '<template><div><input></input></div></template>',
// output: '<template><div><input></template>',
// errors: ["'<input>' should not have end tag."]
// },
{
filename: 'test.vue',
code: '<template><div><div></div></template>',
Expand Down
6 changes: 5 additions & 1 deletion tests/lib/rules/html-self-closing.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,11 @@ tester.run('html-self-closing', rule, {
code: '<template><div><!-- comment --></div></template>',
output: null,
options: [{ html: { normal: 'always' }}]
}
},

// Invalid EOF
'<template><div a=">test</div></template>',
'<template><div><!--test</div></template>'

// other cases are in `invalid` tests.
],
Expand Down

0 comments on commit a93a234

Please sign in to comment.