diff --git a/docs/rules/no-template-shadow.md b/docs/rules/no-template-shadow.md index 5bddba45c..0dcfb2171 100644 --- a/docs/rules/no-template-shadow.md +++ b/docs/rules/no-template-shadow.md @@ -36,13 +36,13 @@ This rule aims to eliminate shadowed variable declarations of v-for directives o ``` @@ -50,7 +50,37 @@ This rule aims to eliminate shadowed variable declarations of v-for directives o ## :wrench: Options -Nothing. +This rule takes one optional object option, with the property `"allow"`. + +```json +{ + "no-template-shadow": ["error", { "allow": [] }] +} +``` + +- `"allow"` (`[string]`) Array of identifier names for which shadowing is allowed. + +Examples of correct code for the `{ "allow": ["i"] }` option: + + + +```vue + + + +``` + + ## :rocket: Version diff --git a/lib/rules/no-template-shadow.js b/lib/rules/no-template-shadow.js index 4b7528a0b..8be04b9cc 100644 --- a/lib/rules/no-template-shadow.js +++ b/lib/rules/no-template-shadow.js @@ -20,6 +20,13 @@ const GROUP_NAMES = [ 'setup' ] +function isAllowedVarName(context, variableName) { + if (context.options[0] && context.options[0].allow) { + return context.options[0].allow.includes(variableName) + } + return false +} + module.exports = { meta: { type: 'suggestion', @@ -30,7 +37,21 @@ module.exports = { url: 'https://eslint.vuejs.org/rules/no-template-shadow.html' }, fixable: null, - schema: [], + schema: [ + { + type: 'object', + properties: { + allow: { + type: 'array', + items: { + type: 'string' + }, + uniqueItems: true + } + }, + additionalProperties: false + } + ], messages: { alreadyDeclaredInUpperScope: "Variable '{{name}}' is already declared in the upper scope." @@ -102,6 +123,11 @@ module.exports = { for (const variable of node.variables) { const varNode = variable.id const name = varNode.name + + if (isAllowedVarName(context, name)) { + continue + } + if ( scopeStack.nodes.some((node) => node.name === name) || jsVars.has(name) diff --git a/tests/lib/rules/no-template-shadow.js b/tests/lib/rules/no-template-shadow.js index e2ea096b7..6844b5f9f 100644 --- a/tests/lib/rules/no-template-shadow.js +++ b/tests/lib/rules/no-template-shadow.js @@ -155,6 +155,21 @@ ruleTester.run('no-template-shadow', rule, { defineProps({k:Number}) ` + }, + { + filename: 'test.vue', + code: ` + + + `, + options: [{ allow: ['i'] }] } ],