From c549fd88177f1d008b7f48e5f0643e3c1721f0a8 Mon Sep 17 00:00:00 2001 From: Mussin Benarbia Date: Fri, 24 Nov 2023 20:52:00 +0900 Subject: [PATCH 1/6] feat: add allow list to no-template-shadow rule --- lib/rules/no-template-shadow.js | 26 +++++++++++++++++++++++++- tests/lib/rules/no-template-shadow.js | 15 +++++++++++++++ 2 files changed, 40 insertions(+), 1 deletion(-) diff --git a/lib/rules/no-template-shadow.js b/lib/rules/no-template-shadow.js index 4b7528a0b..fd0e4943f 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,19 @@ 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' + } + } + } + } + ], messages: { alreadyDeclaredInUpperScope: "Variable '{{name}}' is already declared in the upper scope." @@ -102,6 +121,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..0007996f4 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']}] } ], From da9e82531943730c0ffeae877cfa2b427671ee76 Mon Sep 17 00:00:00 2001 From: Mussin Benarbia Date: Fri, 24 Nov 2023 21:09:42 +0900 Subject: [PATCH 2/6] style: apply formatter --- tests/lib/rules/no-template-shadow.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/lib/rules/no-template-shadow.js b/tests/lib/rules/no-template-shadow.js index 0007996f4..6844b5f9f 100644 --- a/tests/lib/rules/no-template-shadow.js +++ b/tests/lib/rules/no-template-shadow.js @@ -169,7 +169,7 @@ ruleTester.run('no-template-shadow', rule, { defineProps({i:Number}) `, - options: [{ allow: ['i']}] + options: [{ allow: ['i'] }] } ], From 0f2e07337097c12c301c479ea1ac3e85a0396ec3 Mon Sep 17 00:00:00 2001 From: Mussin Benarbia Date: Mon, 4 Dec 2023 23:54:34 +0900 Subject: [PATCH 3/6] refactor: add missing properties to option definition --- lib/rules/no-template-shadow.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/lib/rules/no-template-shadow.js b/lib/rules/no-template-shadow.js index fd0e4943f..8be04b9cc 100644 --- a/lib/rules/no-template-shadow.js +++ b/lib/rules/no-template-shadow.js @@ -45,9 +45,11 @@ module.exports = { type: 'array', items: { type: 'string' - } + }, + uniqueItems: true } - } + }, + additionalProperties: false } ], messages: { From 3e509a4445c282aa7d1015ce0818e693f85f30e6 Mon Sep 17 00:00:00 2001 From: Mussin Benarbia Date: Tue, 5 Dec 2023 00:10:12 +0900 Subject: [PATCH 4/6] docs: update docs to reflect new option --- docs/rules/no-template-shadow.md | 43 +++++++++++++++++++++++++++----- 1 file changed, 37 insertions(+), 6 deletions(-) diff --git a/docs/rules/no-template-shadow.md b/docs/rules/no-template-shadow.md index 5bddba45c..bb5627db5 100644 --- a/docs/rules/no-template-shadow.md +++ b/docs/rules/no-template-shadow.md @@ -5,6 +5,7 @@ title: vue/no-template-shadow description: disallow variable declarations from shadowing variables declared in the outer scope since: v5.0.0 --- + # vue/no-template-shadow > disallow variable declarations from shadowing variables declared in the outer scope @@ -36,13 +37,13 @@ This rule aims to eliminate shadowed variable declarations of v-for directives o ``` @@ -50,7 +51,37 @@ This rule aims to eliminate shadowed variable declarations of v-for directives o ## :wrench: Options -Nothing. +This rule takes one option, an object, with the property `"allow"`. + +```json +{ + "no-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 From e0beca1d7b945538efea4a2e3ebecc7c6c44eee6 Mon Sep 17 00:00:00 2001 From: Mussin Benarbia Date: Tue, 5 Dec 2023 00:43:25 +0900 Subject: [PATCH 5/6] docs: fix typo in docs --- docs/rules/no-template-shadow.md | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/docs/rules/no-template-shadow.md b/docs/rules/no-template-shadow.md index bb5627db5..e26590092 100644 --- a/docs/rules/no-template-shadow.md +++ b/docs/rules/no-template-shadow.md @@ -5,7 +5,6 @@ title: vue/no-template-shadow description: disallow variable declarations from shadowing variables declared in the outer scope since: v5.0.0 --- - # vue/no-template-shadow > disallow variable declarations from shadowing variables declared in the outer scope @@ -55,13 +54,13 @@ This rule takes one option, an object, with the property `"allow"`. ```json { - "no-shadow": ["error", { "allow": [] }] + "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: +Examples of correct code for the `{ "allow": ["i"] }` option: From e5898c580be0e72e47182be440bb6b96a5d8eab4 Mon Sep 17 00:00:00 2001 From: Mussin Benarbia Date: Tue, 5 Dec 2023 07:43:24 +0900 Subject: [PATCH 6/6] docs: improve clarity of docs --- docs/rules/no-template-shadow.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/rules/no-template-shadow.md b/docs/rules/no-template-shadow.md index e26590092..0dcfb2171 100644 --- a/docs/rules/no-template-shadow.md +++ b/docs/rules/no-template-shadow.md @@ -50,7 +50,7 @@ export default { ## :wrench: Options -This rule takes one option, an object, with the property `"allow"`. +This rule takes one optional object option, with the property `"allow"`. ```json {