Skip to content
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

Extend vue/no-dupe-keys to support <script setup> #2185

Merged
merged 2 commits into from May 22, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
56 changes: 39 additions & 17 deletions lib/rules/no-dupe-keys.js
Expand Up @@ -4,10 +4,13 @@
*/
'use strict'

const { findVariable } = require('@eslint-community/eslint-utils')
const utils = require('../utils')

/**
* @typedef {import('../utils').GroupName} GroupName
* @typedef {import('eslint').Scope.Variable} Variable
* @typedef {import('../utils').ComponentProp} ComponentProp
*/

/** @type {GroupName[]} */
Expand Down Expand Up @@ -39,24 +42,43 @@ module.exports = {
const options = context.options[0] || {}
const groups = new Set([...GROUP_NAMES, ...(options.groups || [])])

return utils.executeOnVue(context, (obj) => {
/** @type {Set<string>} */
const usedNames = new Set()
const properties = utils.iterateProperties(obj, groups)

for (const o of properties) {
if (usedNames.has(o.name)) {
context.report({
node: o.node,
message: "Duplicated key '{{name}}'.",
data: {
name: o.name
}
})
return utils.compositingVisitors(
utils.executeOnVue(context, (obj) => {
const properties = utils.iterateProperties(obj, groups)
/** @type {Set<string>} */
const usedNames = new Set()
for (const o of properties) {
if (usedNames.has(o.name)) {
context.report({
node: o.node,
message: "Duplicated key '{{name}}'.",
data: {
name: o.name
}
})
}

usedNames.add(o.name)
}
}),
utils.defineScriptSetupVisitor(context, {
onDefinePropsEnter(node, props) {
for (const prop of props) {
if (!prop.propName) continue

usedNames.add(o.name)
}
})
const variable = findVariable(context.getScope(), prop.propName)
if (!variable || variable.defs.length === 0) continue

context.report({
node: variable.defs[0].node,
message: "Duplicated key '{{name}}'.",
data: {
name: prop.propName
}
})
}
}
})
)
}
}
105 changes: 105 additions & 0 deletions tests/lib/rules/no-dupe-keys.js
Expand Up @@ -390,6 +390,32 @@ ruleTester.run('no-dupe-keys', rule, {
},
}
`
},
{
filename: 'test.vue',
code: `
<script setup>
defineProps({
foo: String,
})
const bar = 0
</script>
`,
parser: require.resolve('vue-eslint-parser')
},
{
filename: 'test.vue',
code: `
<script setup lang="ts">
defineProps<{
foo: string;
}>();

const bar = 0
</script>
`,
parser: require.resolve('vue-eslint-parser'),
parserOptions: { parser: require.resolve('@typescript-eslint/parser') }
}
],

Expand Down Expand Up @@ -861,6 +887,85 @@ ruleTester.run('no-dupe-keys', rule, {
line: 7
}
]
},
{
filename: 'test.vue',
code: `
<script setup>
defineProps({
foo: String,
})
const foo = 0
</script>
`,
parser: require.resolve('vue-eslint-parser'),
errors: [
{
message: "Duplicated key 'foo'.",
line: 6
}
]
},
{
filename: 'test.vue',
code: `
<script setup>
import { Foo } from './Foo.vue';
import baz from './baz';

defineProps({
foo: String,
bar: String,
baz: String,
});

function foo() {
const baz = 'baz';
}
const bar = () => 'bar';
</script>
`,
parser: require.resolve('vue-eslint-parser'),
errors: [
{
message: "Duplicated key 'baz'.",
line: 4
},
{
message: "Duplicated key 'foo'.",
line: 12
},
{
message: "Duplicated key 'bar'.",
line: 15
}
]
},
{
filename: 'test.vue',
code: `
<script setup lang="ts">
defineProps<{
foo: string;
bar: string;
}>();

const foo = 'foo';
const bar = 'bar';
</script>
`,
parser: require.resolve('vue-eslint-parser'),
parserOptions: { parser: require.resolve('@typescript-eslint/parser') },
errors: [
{
message: "Duplicated key 'foo'.",
line: 8
},
{
message: "Duplicated key 'bar'.",
line: 9
}
]
}
]
})