Skip to content

Commit

Permalink
Fix false positives for namespace component in `vue/script-setup-uses…
Browse files Browse the repository at this point in the history
…-vars` rule (#1602)
  • Loading branch information
ota-meshi committed Aug 10, 2021
1 parent 59db655 commit 628f409
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 5 deletions.
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

0 comments on commit 628f409

Please sign in to comment.