From 56bcdb43c2c99b0d64fba94591c8d814614262b8 Mon Sep 17 00:00:00 2001 From: Flo Edelmann Date: Thu, 27 Jan 2022 13:14:28 +0100 Subject: [PATCH] Support template literal component names in `vue/no-undef-components` (#1782) * Move `isStringLiteral` function to utils * Support template literal component names in `vue/no-undef-components` --- lib/rules/no-undef-components.js | 7 ++++--- lib/rules/prefer-separate-static-class.js | 21 +++++---------------- lib/utils/index.js | 11 +++++++++++ tests/lib/rules/no-undef-components.js | 13 +++++++++++++ 4 files changed, 33 insertions(+), 19 deletions(-) diff --git a/lib/rules/no-undef-components.js b/lib/rules/no-undef-components.js index aa9ddb0dd..e56f0412c 100644 --- a/lib/rules/no-undef-components.js +++ b/lib/rules/no-undef-components.js @@ -202,9 +202,10 @@ module.exports = { const nameProperty = utils.findProperty(obj, 'name') - if (nameProperty) { - if (nameProperty.value.type === 'Literal') { - registeredComponentNames.push(`${nameProperty.value.value}`) + if (nameProperty && utils.isStringLiteral(nameProperty.value)) { + const name = utils.getStringLiteralValue(nameProperty.value) + if (name) { + registeredComponentNames.push(name) } } diff --git a/lib/rules/prefer-separate-static-class.js b/lib/rules/prefer-separate-static-class.js index 504d95d60..0ebeb2451 100644 --- a/lib/rules/prefer-separate-static-class.js +++ b/lib/rules/prefer-separate-static-class.js @@ -8,22 +8,11 @@ // Requirements // ------------------------------------------------------------------------------ -const { defineTemplateBodyVisitor, getStringLiteralValue } = require('../utils') - -// ------------------------------------------------------------------------------ -// Helpers -// ------------------------------------------------------------------------------ - -/** - * @param {ASTNode} node - * @returns {node is Literal | TemplateLiteral} - */ -function isStringLiteral(node) { - return ( - (node.type === 'Literal' && typeof node.value === 'string') || - (node.type === 'TemplateLiteral' && node.expressions.length === 0) - ) -} +const { + defineTemplateBodyVisitor, + isStringLiteral, + getStringLiteralValue +} = require('../utils') /** * @param {Expression | VForExpression | VOnExpression | VSlotScopeExpression | VFilterSequenceExpression} expressionNode diff --git a/lib/utils/index.js b/lib/utils/index.js index bacb892ba..69cd84a31 100644 --- a/lib/utils/index.js +++ b/lib/utils/index.js @@ -758,6 +758,17 @@ module.exports = { } }, + /** + * @param {ASTNode} node + * @returns {node is Literal | TemplateLiteral} + */ + isStringLiteral(node) { + return ( + (node.type === 'Literal' && typeof node.value === 'string') || + (node.type === 'TemplateLiteral' && node.expressions.length === 0) + ) + }, + /** * Check whether the given node is a custom component or not. * @param {VElement} node The start tag node to check. diff --git a/tests/lib/rules/no-undef-components.js b/tests/lib/rules/no-undef-components.js index 8bc637f45..b3a7af106 100644 --- a/tests/lib/rules/no-undef-components.js +++ b/tests/lib/rules/no-undef-components.js @@ -504,6 +504,19 @@ tester.run('no-undef-components', rule, { ` }, + { + filename: 'test.vue', + code: ` + + + ` + }, { filename: 'test.vue', code: `