Skip to content

Commit 05b1286

Browse files
mussinbenarbiaMussin Benarbia
and
Mussin Benarbia
authored
feat: add allow list to no-template-shadow (#2323)
Co-authored-by: Mussin Benarbia <[email protected]>
1 parent 7cf5c51 commit 05b1286

File tree

3 files changed

+78
-7
lines changed

3 files changed

+78
-7
lines changed

docs/rules/no-template-shadow.md

+36-6
Original file line numberDiff line numberDiff line change
@@ -36,21 +36,51 @@ This rule aims to eliminate shadowed variable declarations of v-for directives o
3636
</template>
3737
3838
<script>
39-
export default {
40-
data () {
41-
return {
42-
l: false
43-
}
39+
export default {
40+
data() {
41+
return {
42+
l: false
4443
}
4544
}
45+
}
4646
</script>
4747
```
4848

4949
</eslint-code-block>
5050

5151
## :wrench: Options
5252

53-
Nothing.
53+
This rule takes one optional object option, with the property `"allow"`.
54+
55+
```json
56+
{
57+
"no-template-shadow": ["error", { "allow": [] }]
58+
}
59+
```
60+
61+
- `"allow"` (`[string]`) Array of identifier names for which shadowing is allowed.
62+
63+
Examples of correct code for the `{ "allow": ["i"] }` option:
64+
65+
<eslint-code-block :rules="{'vue/no-template-shadow': ['error', { allow: ['i'] }]}">
66+
67+
```vue
68+
<template>
69+
<div v-for="i in 5"></div>
70+
</template>
71+
72+
<script>
73+
export default {
74+
data() {
75+
return {
76+
i: 'some value'
77+
}
78+
}
79+
}
80+
</script>
81+
```
82+
83+
</eslint-code-block>
5484

5585
## :rocket: Version
5686

lib/rules/no-template-shadow.js

+27-1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,13 @@ const GROUP_NAMES = [
2020
'setup'
2121
]
2222

23+
function isAllowedVarName(context, variableName) {
24+
if (context.options[0] && context.options[0].allow) {
25+
return context.options[0].allow.includes(variableName)
26+
}
27+
return false
28+
}
29+
2330
module.exports = {
2431
meta: {
2532
type: 'suggestion',
@@ -30,7 +37,21 @@ module.exports = {
3037
url: 'https://eslint.vuejs.org/rules/no-template-shadow.html'
3138
},
3239
fixable: null,
33-
schema: [],
40+
schema: [
41+
{
42+
type: 'object',
43+
properties: {
44+
allow: {
45+
type: 'array',
46+
items: {
47+
type: 'string'
48+
},
49+
uniqueItems: true
50+
}
51+
},
52+
additionalProperties: false
53+
}
54+
],
3455
messages: {
3556
alreadyDeclaredInUpperScope:
3657
"Variable '{{name}}' is already declared in the upper scope."
@@ -102,6 +123,11 @@ module.exports = {
102123
for (const variable of node.variables) {
103124
const varNode = variable.id
104125
const name = varNode.name
126+
127+
if (isAllowedVarName(context, name)) {
128+
continue
129+
}
130+
105131
if (
106132
scopeStack.nodes.some((node) => node.name === name) ||
107133
jsVars.has(name)

tests/lib/rules/no-template-shadow.js

+15
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,21 @@ ruleTester.run('no-template-shadow', rule, {
155155
defineProps({k:Number})
156156
</script>
157157
`
158+
},
159+
{
160+
filename: 'test.vue',
161+
code: `
162+
<template>
163+
<div v-for="i in 5">
164+
<div v-for="i in 5">
165+
</div>
166+
</div>
167+
</template>
168+
<script setup>
169+
defineProps({i:Number})
170+
</script>
171+
`,
172+
options: [{ allow: ['i'] }]
158173
}
159174
],
160175

0 commit comments

Comments
 (0)