Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support template literal component names in vue/no-undef-components #1782

Merged
merged 2 commits into from Jan 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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