Skip to content

Commit

Permalink
Support template literal component names in vue/no-undef-components (
Browse files Browse the repository at this point in the history
…#1782)

* Move `isStringLiteral` function to utils

* Support template literal component names in `vue/no-undef-components`
  • Loading branch information
FloEdelmann committed Jan 27, 2022
1 parent 45a7650 commit 56bcdb4
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 19 deletions.
7 changes: 4 additions & 3 deletions lib/rules/no-undef-components.js
Expand Up @@ -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)
}
}

Expand Down
21 changes: 5 additions & 16 deletions lib/rules/prefer-separate-static-class.js
Expand Up @@ -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
Expand Down
11 changes: 11 additions & 0 deletions lib/utils/index.js
Expand Up @@ -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.
Expand Down
13 changes: 13 additions & 0 deletions tests/lib/rules/no-undef-components.js
Expand Up @@ -504,6 +504,19 @@ tester.run('no-undef-components', rule, {
</script>
`
},
{
filename: 'test.vue',
code: `
<template>
<custom-component />
</template>
<script>
export default {
name: \`CustomComponent\`
}
</script>
`
},
{
filename: 'test.vue',
code: `
Expand Down

0 comments on commit 56bcdb4

Please sign in to comment.