Skip to content

Commit 2713fb2

Browse files
rzzfwaynzh
andauthored
fix(define-props-destructuring): imported props declaration does not work (#2995)
Co-authored-by: waynzh <waynzh19@gmail.com>
1 parent e13bfc7 commit 2713fb2

File tree

3 files changed

+145
-2
lines changed

3 files changed

+145
-2
lines changed

.changeset/lemon-turtles-visit.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'eslint-plugin-vue': patch
3+
---
4+
5+
Fixed false positives in `vue/define-props-destructuring` rule when imported types are passed to `defineProps`

lib/rules/define-props-destructuring.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,10 @@ module.exports = {
4040
return utils.compositingVisitors(
4141
utils.defineScriptSetupVisitor(context, {
4242
onDefinePropsEnter(node, props) {
43-
const hasNoArgs = props.filter((prop) => prop.propName).length === 0
44-
if (hasNoArgs) {
43+
const hasArgs = props.some(
44+
(prop) => prop.propName || (prop.type === 'unknown' && prop.node)
45+
)
46+
if (!hasArgs) {
4547
return
4648
}
4749

tests/lib/rules/define-props-destructuring.js

Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,30 @@ tester.run('define-props-destructuring', rule, {
4444
parserOptions: { parser: require.resolve('@typescript-eslint/parser') }
4545
}
4646
},
47+
{
48+
filename: 'test.vue',
49+
code: `
50+
<script setup lang="ts">
51+
import type { Props } from './type'
52+
const { foo } = defineProps<Props>()
53+
</script>
54+
`,
55+
languageOptions: {
56+
parserOptions: { parser: require.resolve('@typescript-eslint/parser') }
57+
}
58+
},
59+
{
60+
filename: 'test.vue',
61+
code: `
62+
<script setup lang="ts">
63+
import { propsObj } from './type'
64+
const { foo } = defineProps(propsObj)
65+
</script>
66+
`,
67+
languageOptions: {
68+
parserOptions: { parser: require.resolve('@typescript-eslint/parser') }
69+
}
70+
},
4771
{
4872
filename: 'test.vue',
4973
code: `
@@ -73,6 +97,32 @@ tester.run('define-props-destructuring', rule, {
7397
languageOptions: {
7498
parserOptions: { parser: require.resolve('@typescript-eslint/parser') }
7599
}
100+
},
101+
{
102+
filename: 'test.vue',
103+
code: `
104+
<script setup lang="ts">
105+
import type { Props } from './type'
106+
const props = defineProps<Props>()
107+
</script>
108+
`,
109+
options: [{ destructure: 'never' }],
110+
languageOptions: {
111+
parserOptions: { parser: require.resolve('@typescript-eslint/parser') }
112+
}
113+
},
114+
{
115+
filename: 'test.vue',
116+
code: `
117+
<script setup lang="ts">
118+
import { propsObj } from './type'
119+
const props = defineProps(propsObj)
120+
</script>
121+
`,
122+
options: [{ destructure: 'never' }],
123+
languageOptions: {
124+
parserOptions: { parser: require.resolve('@typescript-eslint/parser') }
125+
}
76126
}
77127
],
78128
invalid: [
@@ -167,6 +217,48 @@ tester.run('define-props-destructuring', rule, {
167217
}
168218
]
169219
},
220+
{
221+
filename: 'test.vue',
222+
code: `
223+
<script setup lang="ts">
224+
import type { Props } from './type'
225+
const props = defineProps<Props>()
226+
</script>
227+
`,
228+
languageOptions: {
229+
parserOptions: { parser: require.resolve('@typescript-eslint/parser') }
230+
},
231+
errors: [
232+
{
233+
messageId: 'preferDestructuring',
234+
line: 4,
235+
column: 21,
236+
endLine: 4,
237+
endColumn: 41
238+
}
239+
]
240+
},
241+
{
242+
filename: 'test.vue',
243+
code: `
244+
<script setup lang="ts">
245+
import { propsObj } from './type'
246+
const props = defineProps(propsObj)
247+
</script>
248+
`,
249+
languageOptions: {
250+
parserOptions: { parser: require.resolve('@typescript-eslint/parser') }
251+
},
252+
errors: [
253+
{
254+
messageId: 'preferDestructuring',
255+
line: 4,
256+
column: 21,
257+
endLine: 4,
258+
endColumn: 42
259+
}
260+
]
261+
},
170262
{
171263
filename: 'test.vue',
172264
code: `
@@ -223,6 +315,50 @@ tester.run('define-props-destructuring', rule, {
223315
endColumn: 54
224316
}
225317
]
318+
},
319+
{
320+
filename: 'test.vue',
321+
code: `
322+
<script setup lang="ts">
323+
import type { Props } from './type'
324+
const { foo } = defineProps<Props>()
325+
</script>
326+
`,
327+
options: [{ destructure: 'never' }],
328+
languageOptions: {
329+
parserOptions: { parser: require.resolve('@typescript-eslint/parser') }
330+
},
331+
errors: [
332+
{
333+
messageId: 'avoidDestructuring',
334+
line: 4,
335+
column: 23,
336+
endLine: 4,
337+
endColumn: 43
338+
}
339+
]
340+
},
341+
{
342+
filename: 'test.vue',
343+
code: `
344+
<script setup lang="ts">
345+
import { propsObj } from './type'
346+
const { foo } = defineProps(propsObj)
347+
</script>
348+
`,
349+
options: [{ destructure: 'never' }],
350+
languageOptions: {
351+
parserOptions: { parser: require.resolve('@typescript-eslint/parser') }
352+
},
353+
errors: [
354+
{
355+
messageId: 'avoidDestructuring',
356+
line: 4,
357+
column: 23,
358+
endLine: 4,
359+
endColumn: 44
360+
}
361+
]
226362
}
227363
]
228364
})

0 commit comments

Comments
 (0)