From 52b023def6e28ba1004a62ace15bf16cf33acc96 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Przemys=C5=82aw=20Fa=C5=82owski?= Date: Tue, 21 Jan 2020 20:54:02 +0100 Subject: [PATCH 1/2] feat: update valid-template-root to support Vue 3 requirements --- docs/rules/valid-template-root.md | 38 +------------- lib/rules/valid-template-root.js | 69 +++++--------------------- tests/lib/rules/valid-template-root.js | 36 ++++++-------- 3 files changed, 28 insertions(+), 115 deletions(-) diff --git a/docs/rules/valid-template-root.md b/docs/rules/valid-template-root.md index 777a674d7..7d6e07782 100644 --- a/docs/rules/valid-template-root.md +++ b/docs/rules/valid-template-root.md @@ -27,42 +27,8 @@ This rule reports the template root in the following cases: ```vue - - -``` - - - - - -```vue - - -``` - - - - - -```vue - - -``` - - - - - -```vue - - + + ``` diff --git a/lib/rules/valid-template-root.js b/lib/rules/valid-template-root.js index 2932435df..860b550d4 100644 --- a/lib/rules/valid-template-root.js +++ b/lib/rules/valid-template-root.js @@ -39,72 +39,27 @@ module.exports = { const hasSrc = utils.hasAttribute(element, 'src') const rootElements = [] - let extraText = null - let extraElement = null - let vIf = false + for (const child of element.children) { - if (child.type === 'VElement') { - if (rootElements.length === 0 && !hasSrc) { - rootElements.push(child) - vIf = utils.hasDirective(child, 'if') - } else if (vIf && utils.hasDirective(child, 'else-if')) { - rootElements.push(child) - } else if (vIf && utils.hasDirective(child, 'else')) { - rootElements.push(child) - vIf = false - } else { - extraElement = child - } - } else if (sourceCode.getText(child).trim() !== '') { - extraText = child + if (sourceCode.getText(child).trim() !== '') { + rootElements.push(child) } } - if (hasSrc && (extraText != null || extraElement != null)) { - context.report({ - node: extraText || extraElement, - loc: (extraText || extraElement).loc, - message: "The template root with 'src' attribute is required to be empty." - }) - } else if (extraText != null) { - context.report({ - node: extraText, - loc: extraText.loc, - message: 'The template root requires an element rather than texts.' - }) - } else if (extraElement != null) { - context.report({ - node: extraElement, - loc: extraElement.loc, - message: 'The template root requires exactly one element.' - }) + if (hasSrc && rootElements.length) { + for (const element of rootElements) { + context.report({ + node: element, + loc: element.loc, + message: "The template root with 'src' attribute is required to be empty." + }) + } } else if (rootElements.length === 0 && !hasSrc) { context.report({ node: element, loc: element.loc, - message: 'The template root requires exactly one element.' + message: 'The template requires child element.' }) - } else { - for (const element of rootElements) { - const tag = element.startTag - const name = element.name - - if (name === 'template' || name === 'slot') { - context.report({ - node: tag, - loc: tag.loc, - message: "The template root disallows '<{{name}}>' elements.", - data: { name } - }) - } - if (utils.hasDirective(element, 'for')) { - context.report({ - node: tag, - loc: tag.loc, - message: "The template root disallows 'v-for' directives." - }) - } - } } } } diff --git a/tests/lib/rules/valid-template-root.js b/tests/lib/rules/valid-template-root.js index 2fd9dbc05..033188cca 100644 --- a/tests/lib/rules/valid-template-root.js +++ b/tests/lib/rules/valid-template-root.js @@ -74,53 +74,45 @@ tester.run('valid-template-root', rule, { { filename: 'test.vue', code: '' - } - ], - invalid: [ + }, { filename: 'test.vue', - code: '', - errors: ['The template root requires exactly one element.'] + code: '' }, { filename: 'test.vue', - code: '', - errors: ['The template root requires exactly one element.'] + code: '' }, { filename: 'test.vue', - code: '', - errors: ['The template root requires exactly one element.'] + code: '' }, { filename: 'test.vue', - code: '', - errors: ['The template root requires an element rather than texts.'] + code: '' }, { filename: 'test.vue', - code: '', - errors: ['The template root requires an element rather than texts.'] + code: '' }, { filename: 'test.vue', - code: '', - errors: ['The template root requires an element rather than texts.'] + code: '' }, { filename: 'test.vue', - code: '', - errors: ["The template root disallows 'v-for' directives."] + code: '' }, { filename: 'test.vue', - code: '', - errors: ["The template root disallows '' elements."] - }, + code: '' + } + ], + invalid: [ { filename: 'test.vue', - code: '', - errors: ["The template root disallows '