From bc734e44756adfaddf4af523ed181469c7cbe61d Mon Sep 17 00:00:00 2001 From: Toru Nagashima Date: Sat, 2 Dec 2017 17:14:14 +0900 Subject: [PATCH 1/2] Fix: incorrect errors in invalid EOF cases (fixes #268) --- lib/rules/html-end-tags.js | 19 +++++++++--------- lib/rules/html-self-closing.js | 9 +++++++++ lib/utils/index.js | 13 ++++++++++++ tests/lib/rules/html-end-tags.js | 30 +++++++++++----------------- tests/lib/rules/html-self-closing.js | 6 +++++- 5 files changed, 49 insertions(+), 28 deletions(-) diff --git a/lib/rules/html-end-tags.js b/lib/rules/html-end-tags.js index a4c05eecd..922f34d88 100644 --- a/lib/rules/html-end-tags.js +++ b/lib/rules/html-end-tags.js @@ -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, @@ -48,6 +45,10 @@ function create (context) { }) } } + }, { + Program (node) { + hasInvalidEOF = utils.hasInvalidEOF(node) + } }) } diff --git a/lib/rules/html-self-closing.js b/lib/rules/html-self-closing.js index f311a63f9..23263e2be 100644 --- a/lib/rules/html-self-closing.js +++ b/lib/rules/html-self-closing.js @@ -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] @@ -131,6 +136,10 @@ function create (context) { }) } } + }, { + Program (node) { + hasInvalidEOF = utils.hasInvalidEOF(node) + } }) } diff --git a/lib/utils/index.js b/lib/utils/index.js index 635707b67..77c9a72b5 100644 --- a/lib/utils/index.js +++ b/lib/utils/index.js @@ -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-')) } } diff --git a/tests/lib/rules/html-end-tags.js b/tests/lib/rules/html-end-tags.js index 16cb8fe2f..66aad36d0 100644 --- a/tests/lib/rules/html-end-tags.js +++ b/tests/lib/rules/html-end-tags.js @@ -54,27 +54,21 @@ tester.run('html-end-tags', rule, { { filename: 'test.vue', code: '' + }, + { + filename: 'test.vue', + code: '