Skip to content

Commit 838bc19

Browse files
committed
fix(no-dupe-keys): detect props destructure rename
1 parent 41e0192 commit 838bc19

File tree

2 files changed

+62
-2
lines changed

2 files changed

+62
-2
lines changed

lib/rules/no-dupe-keys.js

+31
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,33 @@ function isInsideInitializer(node, references) {
5858
)
5959
}
6060

61+
/**
62+
* Find if the prop is renamed in the pattern
63+
* @param {Pattern} pattern - The destructuring pattern
64+
* @param {string} propName - The original prop name
65+
* @returns {boolean} - True if the prop is renamed
66+
*/
67+
function findRenamedProp(pattern, propName) {
68+
if (!pattern || pattern.type !== 'ObjectPattern') {
69+
return false
70+
}
71+
72+
for (const prop of pattern.properties) {
73+
if (prop.type !== 'Property') continue
74+
75+
if (
76+
prop.key.type === 'Identifier' &&
77+
prop.key.name === propName &&
78+
prop.value.type === 'Identifier' &&
79+
prop.value.name !== propName
80+
) {
81+
return true
82+
}
83+
}
84+
85+
return false
86+
}
87+
6188
module.exports = {
6289
meta: {
6390
type: 'problem',
@@ -118,6 +145,10 @@ module.exports = {
118145
for (const prop of props) {
119146
if (!prop.propName) continue
120147

148+
if (propsNode && findRenamedProp(propsNode, prop.propName)) {
149+
continue
150+
}
151+
121152
const variable = findVariable(
122153
utils.getScope(context, node),
123154
prop.propName

tests/lib/rules/no-dupe-keys.js

+31-2
Original file line numberDiff line numberDiff line change
@@ -466,7 +466,7 @@ ruleTester.run('no-dupe-keys', rule, {
466466
{
467467
filename: 'test.vue',
468468
code: `
469-
<script setup></script>
469+
<script setup>
470470
const {foo,bar} = defineProps(['foo', 'bar'])
471471
</script>
472472
`,
@@ -475,7 +475,7 @@ ruleTester.run('no-dupe-keys', rule, {
475475
{
476476
filename: 'test.vue',
477477
code: `
478-
<script setup></script>
478+
<script setup>
479479
const {foo=42,bar='abc'} = defineProps(['foo', 'bar'])
480480
</script>
481481
`,
@@ -500,6 +500,17 @@ ruleTester.run('no-dupe-keys', rule, {
500500
parser: require('vue-eslint-parser'),
501501
parserOptions: { parser: require.resolve('@typescript-eslint/parser') }
502502
}
503+
},
504+
{
505+
filename: 'test.vue',
506+
code: `
507+
<script setup>
508+
const { foo: renamedFoo, bar: renamedBar } = defineProps(['foo', 'bar'])
509+
const foo = 42
510+
const bar = 'hello'
511+
</script>
512+
`,
513+
languageOptions: { parser: require('vue-eslint-parser') }
503514
}
504515
],
505516

@@ -1105,6 +1116,24 @@ ruleTester.run('no-dupe-keys', rule, {
11051116
line: 5
11061117
}
11071118
]
1119+
},
1120+
{
1121+
filename: 'test.vue',
1122+
code: `
1123+
<script setup>
1124+
const { foo: renamedFoo } = defineProps(['foo', 'bar'])
1125+
const foo = 'foo'
1126+
const bar = 'bar'
1127+
</script>
1128+
`,
1129+
languageOptions: { parser: require('vue-eslint-parser') },
1130+
errors: [
1131+
{
1132+
message:
1133+
"Duplicate key 'bar'. May cause name collision in script or template tag.",
1134+
line: 5
1135+
}
1136+
]
11081137
}
11091138
]
11101139
})

0 commit comments

Comments
 (0)