Skip to content

Commit

Permalink
feat: autofix in define-props-declaration: runtime syntax to type-b…
Browse files Browse the repository at this point in the history
…ased syntax (vuejs#2465)

handle PropTypes (e.g. String as PropType<'test'>)
  • Loading branch information
mpiniarski committed May 27, 2024
1 parent 4844612 commit b770232
Show file tree
Hide file tree
Showing 2 changed files with 116 additions and 0 deletions.
18 changes: 18 additions & 0 deletions lib/rules/define-props-declaration.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ module.exports = {
},
/** @param {RuleContext} context */
create(context) {
const sourceCode = context.getSourceCode()
/**
* @param {Expression} node
* @returns {string | null}
Expand All @@ -76,6 +77,23 @@ module.exports = {
if (typeProperty == null) {
return null
}
if (typeProperty.value.type === 'TSAsExpression') {
if (
typeProperty.value.typeAnnotation.typeName.name !== 'PropType'
) {
return null
}

const typeArgument =
typeProperty.value.typeAnnotation.typeArguments.params[0]
if (typeArgument === undefined) {
return null
}

const text = sourceCode.getText(typeArgument)

return text
}
return optionGetType(typeProperty.value)
}
case 'ArrayExpression': {
Expand Down
98 changes: 98 additions & 0 deletions tests/lib/rules/define-props-declaration.js
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,104 @@ tester.run('define-props-declaration', rule, {
line: 3
}
]
},
// Native Type with PropType
{
filename: 'test.vue',
code: `
<script setup lang="ts">
const props = defineProps({
kind: {
type: String as PropType<'a' | 'b'>,
}
})
</script>
`,
output: `
<script setup lang="ts">
const props = defineProps<{ kind: 'a' | 'b' }>()
</script>
`,
errors: [
{
message: 'Use type-based declaration instead of runtime declaration.',
line: 3
}
]
},
// Object with PropType
{
filename: 'test.vue',
code: `
<script setup lang="ts">
const props = defineProps({
kind: {
type: Object as PropType<{ id: number; name: string }>,
}
})
</script>
`,
output: `
<script setup lang="ts">
const props = defineProps<{ kind: { id: number; name: string } }>()
</script>
`,
errors: [
{
message: 'Use type-based declaration instead of runtime declaration.',
line: 3
}
]
},
// Array with PropType
{
filename: 'test.vue',
code: `
<script setup lang="ts">
const props = defineProps({
kind: {
type: Array as PropType<string[]>,
default: () => []
}
})
</script>
`,
output: `
<script setup lang="ts">
const props = defineProps<{ kind: string[] }>()
</script>
`,
errors: [
{
message: 'Use type-based declaration instead of runtime declaration.',
line: 3
}
]
},
// Function with PropType
{
filename: 'test.vue',
code: `
<script setup lang="ts">
const props = defineProps({
kind: {
type: Function as PropType<(a: number, b: string) => boolean>,
required: true
}
})
</script>
`,
output: `
<script setup lang="ts">
const props = defineProps<{ kind: (a: number, b: string) => boolean }>()
</script>
`,
errors: [
{
message: 'Use type-based declaration instead of runtime declaration.',
line: 3
}
]
}
]
})

0 comments on commit b770232

Please sign in to comment.