Skip to content

[Fix] Incorrect errors in invalid EOF cases (fixes #268) #274

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 3, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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."]
// },
Copy link
Member Author

@mysticatea mysticatea Dec 2, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Those tests are no longer needed. It's a kind of syntax error if void elements have their extra end tag, so it's reported by vue/no-parsing-error rule.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, then please remove it :)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It has been removed already 😀

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Apparently I need to start wearing glasses 🤓

{
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