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

Fix false positives for namespace component in vue/script-setup-uses-vars rule #1602

Merged
merged 1 commit into from Aug 10, 2021
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
21 changes: 16 additions & 5 deletions lib/rules/script-setup-uses-vars.js
Expand Up @@ -53,28 +53,32 @@ module.exports = {
/**
* `casing.camelCase()` converts the beginning to lowercase,
* but does not convert the case of the beginning character when converting with Vue3.
* @see https://github.com/vuejs/vue-next/blob/1ffd48a2f5fd3eead3ea29dae668b7ed1c6f6130/packages/shared/src/index.ts#L116
* @see https://github.com/vuejs/vue-next/blob/2749c15170ad4913e6530a257db485d4e7ed2283/packages/shared/src/index.ts#L116
* @param {string} str
*/
function camelize(str) {
return str.replace(/-(\w)/g, (_, c) => (c ? c.toUpperCase() : ''))
}
/**
* @see https://github.com/vuejs/vue-next/blob/1ffd48a2f5fd3eead3ea29dae668b7ed1c6f6130/packages/compiler-core/src/transforms/transformElement.ts#L321
* @see https://github.com/vuejs/vue-next/blob/2749c15170ad4913e6530a257db485d4e7ed2283/packages/compiler-core/src/transforms/transformElement.ts#L333
* @param {string} name
*/
function markElementVariableAsUsed(name) {
function markSetupReferenceVariableAsUsed(name) {
if (scriptVariableNames.has(name)) {
context.markVariableAsUsed(name)
return true
}
const camelName = camelize(name)
if (scriptVariableNames.has(camelName)) {
context.markVariableAsUsed(camelName)
return true
}
const pascalName = casing.capitalize(camelName)
if (scriptVariableNames.has(pascalName)) {
context.markVariableAsUsed(pascalName)
return true
}
return false
}

return utils.defineTemplateBodyVisitor(
Expand All @@ -97,14 +101,21 @@ module.exports = {
) {
return
}
markElementVariableAsUsed(node.rawName)
if (!markSetupReferenceVariableAsUsed(node.rawName)) {
// Check namespace
// https://github.com/vuejs/vue-next/blob/2749c15170ad4913e6530a257db485d4e7ed2283/packages/compiler-core/src/transforms/transformElement.ts#L304
const dotIndex = node.rawName.indexOf('.')
if (dotIndex > 0) {
markSetupReferenceVariableAsUsed(node.rawName.slice(0, dotIndex))
}
}
},
/** @param {VDirective} node */
'VAttribute[directive=true]'(node) {
if (utils.isBuiltInDirectiveName(node.key.name.name)) {
return
}
markElementVariableAsUsed(`v-${node.key.name.rawName}`)
markSetupReferenceVariableAsUsed(`v-${node.key.name.rawName}`)
},
/** @param {VAttribute} node */
'VAttribute[directive=false]'(node) {
Expand Down
1 change: 1 addition & 0 deletions lib/utils/index.js
Expand Up @@ -704,6 +704,7 @@ module.exports = {
name === 'pre' ||
name === 'cloak' ||
name === 'once' ||
name === 'memo' ||
name === 'is'
)
},
Expand Down
16 changes: 16 additions & 0 deletions tests/lib/rules/script-setup-uses-vars.js
Expand Up @@ -198,6 +198,22 @@ describe('script-setup-uses-vars', () => {
}
</style>
`
},
// ns
{
filename: 'test.vue',
code: `
<script setup>
/* eslint script-setup-uses-vars: 1 */
import * as Form from './form-components'
</script>

<template>
<Form.Input>
<Form.Label>label</Form.Label>
</Form.Input>
</template>
`
}
],

Expand Down