-
-
Notifications
You must be signed in to change notification settings - Fork 669
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
vue/script-setup-uses-vars
rule (#1529)
* Add `vue/script-setup-uses-vars` rule * upgrade parser * Update * update test
- Loading branch information
Showing
16 changed files
with
742 additions
and
144 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
--- | ||
pageClass: rule-details | ||
sidebarDepth: 0 | ||
title: vue/script-setup-uses-vars | ||
description: prevent `<script setup>` variables used in `<template>` to be marked as unused | ||
--- | ||
# vue/script-setup-uses-vars | ||
|
||
> prevent `<script setup>` variables used in `<template>` to be marked as unused | ||
- :exclamation: <badge text="This rule has not been released yet." vertical="middle" type="error"> ***This rule has not been released yet.*** </badge> | ||
- :gear: This rule is included in all of `"plugin:vue/base"`, `"plugin:vue/essential"`, `"plugin:vue/vue3-essential"`, `"plugin:vue/strongly-recommended"`, `"plugin:vue/vue3-strongly-recommended"`, `"plugin:vue/recommended"` and `"plugin:vue/vue3-recommended"`. | ||
|
||
ESLint `no-unused-vars` rule does not detect variables in `<script setup>` used in `<template>`. | ||
This rule will find variables in `<script setup>` used in `<template>` and mark them as used. | ||
|
||
This rule only has an effect when the `no-unused-vars` rule is enabled. | ||
|
||
## :book: Rule Details | ||
|
||
Without this rule this code triggers warning: | ||
|
||
<eslint-code-block :rules="{'vue/script-setup-uses-vars': ['error'], 'no-unused-vars': ['error']}"> | ||
|
||
```vue | ||
<script setup> | ||
// imported components are also directly usable in template | ||
import Foo from './Foo.vue' | ||
import { ref } from 'vue' | ||
// write Composition API code just like in a normal setup() | ||
// but no need to manually return everything | ||
const count = ref(0) | ||
const inc = () => { | ||
count.value++ | ||
} | ||
</script> | ||
<template> | ||
<Foo :count="count" @click="inc" /> | ||
</template> | ||
``` | ||
|
||
</eslint-code-block> | ||
|
||
After turning on, `Foo` is being marked as used and `no-unused-vars` rule doesn't report an issue. | ||
|
||
## :mute: When Not To Use It | ||
|
||
If you are not using `<script setup>` or if you do not use the `no-unused-vars` rule then you can disable this rule. | ||
|
||
## :couple: Related Rules | ||
|
||
- [vue/jsx-uses-vars](./jsx-uses-vars.md) | ||
- [no-unused-vars](https://eslint.org/docs/rules/no-unused-vars) | ||
|
||
## :books: Further Reading | ||
|
||
- [Vue RFCs - 0040-script-setup](https://github.com/vuejs/rfcs/blob/master/active-rfcs/0040-script-setup.md) | ||
|
||
## :mag: Implementation | ||
|
||
- [Rule source](https://github.com/vuejs/eslint-plugin-vue/blob/master/lib/rules/script-setup-uses-vars.js) | ||
- [Test source](https://github.com/vuejs/eslint-plugin-vue/blob/master/tests/lib/rules/script-setup-uses-vars.js) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
/** | ||
* @author Yosuke Ota | ||
* See LICENSE file in root directory for full license. | ||
*/ | ||
'use strict' | ||
|
||
// ------------------------------------------------------------------------------ | ||
// Requirements | ||
// ------------------------------------------------------------------------------ | ||
|
||
const utils = require('../utils') | ||
const casing = require('../utils/casing') | ||
|
||
// ------------------------------------------------------------------------------ | ||
// Rule Definition | ||
// ------------------------------------------------------------------------------ | ||
|
||
module.exports = { | ||
meta: { | ||
type: 'problem', | ||
docs: { | ||
description: | ||
'prevent `<script setup>` variables used in `<template>` to be marked as unused', // eslint-disable-line consistent-docs-description | ||
categories: ['base'], | ||
url: 'https://eslint.vuejs.org/rules/script-setup-uses-vars.html' | ||
}, | ||
schema: [] | ||
}, | ||
/** | ||
* @param {RuleContext} context - The rule context. | ||
* @returns {RuleListener} AST event handlers. | ||
*/ | ||
create(context) { | ||
if (!utils.isScriptSetup(context)) { | ||
return {} | ||
} | ||
/** @type {Set<string>} */ | ||
const scriptVariableNames = new Set() | ||
const globalScope = context.getSourceCode().scopeManager.globalScope | ||
if (globalScope) { | ||
for (const variable of globalScope.variables) { | ||
scriptVariableNames.add(variable.name) | ||
} | ||
const moduleScope = globalScope.childScopes.find( | ||
(scope) => scope.type === 'module' | ||
) | ||
for (const variable of (moduleScope && moduleScope.variables) || []) { | ||
scriptVariableNames.add(variable.name) | ||
} | ||
} | ||
|
||
/** | ||
* `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 | ||
* @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 | ||
* @param {string} name | ||
*/ | ||
function markElementVariableAsUsed(name) { | ||
if (scriptVariableNames.has(name)) { | ||
context.markVariableAsUsed(name) | ||
} | ||
const camelName = camelize(name) | ||
if (scriptVariableNames.has(camelName)) { | ||
context.markVariableAsUsed(camelName) | ||
} | ||
const pascalName = casing.capitalize(camelName) | ||
if (scriptVariableNames.has(pascalName)) { | ||
context.markVariableAsUsed(pascalName) | ||
} | ||
} | ||
|
||
return utils.defineTemplateBodyVisitor( | ||
context, | ||
{ | ||
VExpressionContainer(node) { | ||
for (const ref of node.references.filter( | ||
(ref) => ref.variable == null | ||
)) { | ||
context.markVariableAsUsed(ref.id.name) | ||
} | ||
}, | ||
VElement(node) { | ||
if ( | ||
(!utils.isHtmlElementNode(node) && !utils.isSvgElementNode(node)) || | ||
(node.rawName === node.name && | ||
(utils.isHtmlWellKnownElementName(node.rawName) || | ||
utils.isSvgWellKnownElementName(node.rawName))) || | ||
utils.isBuiltInComponentName(node.rawName) | ||
) { | ||
return | ||
} | ||
markElementVariableAsUsed(node.rawName) | ||
}, | ||
/** @param {VDirective} node */ | ||
'VAttribute[directive=true]'(node) { | ||
if (utils.isBuiltInDirectiveName(node.key.name.name)) { | ||
return | ||
} | ||
markElementVariableAsUsed(`v-${node.key.name.rawName}`) | ||
} | ||
}, | ||
undefined, | ||
{ | ||
templateBodyTriggerSelector: 'Program' | ||
} | ||
) | ||
} | ||
} |
Oops, something went wrong.