Skip to content

Commit 56bcdb4

Browse files
authored
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`
1 parent 45a7650 commit 56bcdb4

File tree

4 files changed

+33
-19
lines changed

4 files changed

+33
-19
lines changed

lib/rules/no-undef-components.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -202,9 +202,10 @@ module.exports = {
202202

203203
const nameProperty = utils.findProperty(obj, 'name')
204204

205-
if (nameProperty) {
206-
if (nameProperty.value.type === 'Literal') {
207-
registeredComponentNames.push(`${nameProperty.value.value}`)
205+
if (nameProperty && utils.isStringLiteral(nameProperty.value)) {
206+
const name = utils.getStringLiteralValue(nameProperty.value)
207+
if (name) {
208+
registeredComponentNames.push(name)
208209
}
209210
}
210211

lib/rules/prefer-separate-static-class.js

+5-16
Original file line numberDiff line numberDiff line change
@@ -8,22 +8,11 @@
88
// Requirements
99
// ------------------------------------------------------------------------------
1010

11-
const { defineTemplateBodyVisitor, getStringLiteralValue } = require('../utils')
12-
13-
// ------------------------------------------------------------------------------
14-
// Helpers
15-
// ------------------------------------------------------------------------------
16-
17-
/**
18-
* @param {ASTNode} node
19-
* @returns {node is Literal | TemplateLiteral}
20-
*/
21-
function isStringLiteral(node) {
22-
return (
23-
(node.type === 'Literal' && typeof node.value === 'string') ||
24-
(node.type === 'TemplateLiteral' && node.expressions.length === 0)
25-
)
26-
}
11+
const {
12+
defineTemplateBodyVisitor,
13+
isStringLiteral,
14+
getStringLiteralValue
15+
} = require('../utils')
2716

2817
/**
2918
* @param {Expression | VForExpression | VOnExpression | VSlotScopeExpression | VFilterSequenceExpression} expressionNode

lib/utils/index.js

+11
Original file line numberDiff line numberDiff line change
@@ -758,6 +758,17 @@ module.exports = {
758758
}
759759
},
760760

761+
/**
762+
* @param {ASTNode} node
763+
* @returns {node is Literal | TemplateLiteral}
764+
*/
765+
isStringLiteral(node) {
766+
return (
767+
(node.type === 'Literal' && typeof node.value === 'string') ||
768+
(node.type === 'TemplateLiteral' && node.expressions.length === 0)
769+
)
770+
},
771+
761772
/**
762773
* Check whether the given node is a custom component or not.
763774
* @param {VElement} node The start tag node to check.

tests/lib/rules/no-undef-components.js

+13
Original file line numberDiff line numberDiff line change
@@ -504,6 +504,19 @@ tester.run('no-undef-components', rule, {
504504
</script>
505505
`
506506
},
507+
{
508+
filename: 'test.vue',
509+
code: `
510+
<template>
511+
<custom-component />
512+
</template>
513+
<script>
514+
export default {
515+
name: \`CustomComponent\`
516+
}
517+
</script>
518+
`
519+
},
507520
{
508521
filename: 'test.vue',
509522
code: `

0 commit comments

Comments
 (0)