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 union type
  • Loading branch information
mpiniarski committed May 28, 2024
1 parent 207477e commit d08bed1
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
10 changes: 10 additions & 0 deletions lib/rules/define-props-declaration.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,16 @@ function optionGetType(node, sourceCode) {

return sourceCode.getText(typeArgument)
}
case 'LogicalExpression': {
if (node.operator === '||') {
const left = optionGetType(node.left, sourceCode)
const right = optionGetType(node.right, sourceCode)
if (left && right) {
return `${left} | ${right}`
}
}
return sourceCode.getText(node)
}
default: {
return sourceCode.getText(node)
}
Expand Down
24 changes: 24 additions & 0 deletions tests/lib/rules/define-props-declaration.js
Original file line number Diff line number Diff line change
Expand Up @@ -588,6 +588,30 @@ tester.run('define-props-declaration', rule, {
}
]
},
// Union type (Number || String)
{
filename: 'test.vue',
code: `
<script setup lang="ts">
const props = defineProps({
kind: {
type: Number || String
}
})
</script>
`,
output: `
<script setup lang="ts">
const props = defineProps<{ kind: number | string }>()
</script>
`,
errors: [
{
message: 'Use type-based declaration instead of runtime declaration.',
line: 3
}
]
},
// Some unhandled expression type
{
filename: 'test.vue',
Expand Down

0 comments on commit d08bed1

Please sign in to comment.