Skip to content

Commit b770232

Browse files
committed
feat: autofix in define-props-declaration: runtime syntax to type-based syntax (vuejs#2465)
handle PropTypes (e.g. String as PropType<'test'>)
1 parent 4844612 commit b770232

File tree

2 files changed

+116
-0
lines changed

2 files changed

+116
-0
lines changed

Diff for: lib/rules/define-props-declaration.js

+18
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ module.exports = {
6161
},
6262
/** @param {RuleContext} context */
6363
create(context) {
64+
const sourceCode = context.getSourceCode()
6465
/**
6566
* @param {Expression} node
6667
* @returns {string | null}
@@ -76,6 +77,23 @@ module.exports = {
7677
if (typeProperty == null) {
7778
return null
7879
}
80+
if (typeProperty.value.type === 'TSAsExpression') {
81+
if (
82+
typeProperty.value.typeAnnotation.typeName.name !== 'PropType'
83+
) {
84+
return null
85+
}
86+
87+
const typeArgument =
88+
typeProperty.value.typeAnnotation.typeArguments.params[0]
89+
if (typeArgument === undefined) {
90+
return null
91+
}
92+
93+
const text = sourceCode.getText(typeArgument)
94+
95+
return text
96+
}
7997
return optionGetType(typeProperty.value)
8098
}
8199
case 'ArrayExpression': {

Diff for: tests/lib/rules/define-props-declaration.js

+98
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,104 @@ tester.run('define-props-declaration', rule, {
288288
line: 3
289289
}
290290
]
291+
},
292+
// Native Type with PropType
293+
{
294+
filename: 'test.vue',
295+
code: `
296+
<script setup lang="ts">
297+
const props = defineProps({
298+
kind: {
299+
type: String as PropType<'a' | 'b'>,
300+
}
301+
})
302+
</script>
303+
`,
304+
output: `
305+
<script setup lang="ts">
306+
const props = defineProps<{ kind: 'a' | 'b' }>()
307+
</script>
308+
`,
309+
errors: [
310+
{
311+
message: 'Use type-based declaration instead of runtime declaration.',
312+
line: 3
313+
}
314+
]
315+
},
316+
// Object with PropType
317+
{
318+
filename: 'test.vue',
319+
code: `
320+
<script setup lang="ts">
321+
const props = defineProps({
322+
kind: {
323+
type: Object as PropType<{ id: number; name: string }>,
324+
}
325+
})
326+
</script>
327+
`,
328+
output: `
329+
<script setup lang="ts">
330+
const props = defineProps<{ kind: { id: number; name: string } }>()
331+
</script>
332+
`,
333+
errors: [
334+
{
335+
message: 'Use type-based declaration instead of runtime declaration.',
336+
line: 3
337+
}
338+
]
339+
},
340+
// Array with PropType
341+
{
342+
filename: 'test.vue',
343+
code: `
344+
<script setup lang="ts">
345+
const props = defineProps({
346+
kind: {
347+
type: Array as PropType<string[]>,
348+
default: () => []
349+
}
350+
})
351+
</script>
352+
`,
353+
output: `
354+
<script setup lang="ts">
355+
const props = defineProps<{ kind: string[] }>()
356+
</script>
357+
`,
358+
errors: [
359+
{
360+
message: 'Use type-based declaration instead of runtime declaration.',
361+
line: 3
362+
}
363+
]
364+
},
365+
// Function with PropType
366+
{
367+
filename: 'test.vue',
368+
code: `
369+
<script setup lang="ts">
370+
const props = defineProps({
371+
kind: {
372+
type: Function as PropType<(a: number, b: string) => boolean>,
373+
required: true
374+
}
375+
})
376+
</script>
377+
`,
378+
output: `
379+
<script setup lang="ts">
380+
const props = defineProps<{ kind: (a: number, b: string) => boolean }>()
381+
</script>
382+
`,
383+
errors: [
384+
{
385+
message: 'Use type-based declaration instead of runtime declaration.',
386+
line: 3
387+
}
388+
]
291389
}
292390
]
293391
})

0 commit comments

Comments
 (0)