Skip to content

fix(no-dupe-keys): detect props destructure rename #2731

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions lib/rules/no-dupe-keys.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,33 @@ function isInsideInitializer(node, references) {
)
}

/**
* Collects all renamed props from a pattern
* @param {Pattern | null} pattern - The destructuring pattern
* @returns {Set<string>} - Set of prop names that have been renamed
*/
function collectRenamedProps(pattern) {
const renamedProps = new Set()

if (!pattern || pattern.type !== 'ObjectPattern') {
return renamedProps
}

for (const prop of pattern.properties) {
if (prop.type !== 'Property') continue

if (
prop.key.type === 'Identifier' &&
prop.value.type === 'Identifier' &&
prop.key.name !== prop.value.name
) {
renamedProps.add(prop.key.name)
}
}

return renamedProps
}

module.exports = {
meta: {
type: 'problem',
Expand Down Expand Up @@ -115,9 +142,15 @@ module.exports = {
node
]

const renamedProps = collectRenamedProps(propsNode)

for (const prop of props) {
if (!prop.propName) continue

if (renamedProps.has(prop.propName)) {
continue
}

const variable = findVariable(
utils.getScope(context, node),
prop.propName
Expand Down
33 changes: 31 additions & 2 deletions tests/lib/rules/no-dupe-keys.js
Original file line number Diff line number Diff line change
Expand Up @@ -466,7 +466,7 @@ ruleTester.run('no-dupe-keys', rule, {
{
filename: 'test.vue',
code: `
<script setup></script>
<script setup>
const {foo,bar} = defineProps(['foo', 'bar'])
</script>
`,
Expand All @@ -475,7 +475,7 @@ ruleTester.run('no-dupe-keys', rule, {
{
filename: 'test.vue',
code: `
<script setup></script>
<script setup>
const {foo=42,bar='abc'} = defineProps(['foo', 'bar'])
</script>
`,
Expand All @@ -500,6 +500,17 @@ ruleTester.run('no-dupe-keys', rule, {
parser: require('vue-eslint-parser'),
parserOptions: { parser: require.resolve('@typescript-eslint/parser') }
}
},
{
filename: 'test.vue',
code: `
<script setup>
const { foo: renamedFoo, bar: renamedBar } = defineProps(['foo', 'bar'])
const foo = 42
const bar = 'hello'
</script>
`,
languageOptions: { parser: require('vue-eslint-parser') }
}
],

Expand Down Expand Up @@ -1105,6 +1116,24 @@ ruleTester.run('no-dupe-keys', rule, {
line: 5
}
]
},
{
filename: 'test.vue',
code: `
<script setup>
const { foo: renamedFoo } = defineProps(['foo', 'bar'])
const foo = 'foo'
const bar = 'bar'
</script>
`,
languageOptions: { parser: require('vue-eslint-parser') },
errors: [
{
message:
"Duplicate key 'bar'. May cause name collision in script or template tag.",
line: 5
}
]
}
]
})
Loading