Skip to content

Commit

Permalink
feature: handle array as props list (vuejs#2465)
Browse files Browse the repository at this point in the history
  • Loading branch information
mpiniarski committed Jul 15, 2024
1 parent 0bd915b commit 1d58a2b
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 20 deletions.
46 changes: 30 additions & 16 deletions lib/rules/define-props-declaration.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ function* fixTypeBased(fixer, node, props, context) {
}
)

const componentPropsTypeCode = `{ ${componentPropsTypes.join(PROPS_SEPARATOR)} }`
const componentPropsTypeCode = `{ ${componentPropsTypes.join(
PROPS_SEPARATOR
)} }`

// remove defineProps function parameters
yield fixer.replaceText(node.arguments[0], '')
Expand Down Expand Up @@ -96,22 +98,34 @@ const mapNativeType = (nativeType) => {
* @param {SourceCode} sourceCode
*/
function getComponentPropData(prop, sourceCode) {
if (prop.propName === null) {
throw new Error('Unexpected prop with null name.')
}
if (prop.type !== 'object') {
throw new Error(`Unexpected prop type: ${prop.type}.`)
}
const type = optionGetType(prop.value, sourceCode)
const required = optionGetRequired(prop.value)
const defaultValue = optionGetDefault(prop.value)

return {
name: prop.propName,
type,
required,
defaultValue
if (prop.type === 'array') {
if (prop.node.type !== 'Identifier') {
throw new Error(`Unexpected prop type inside array: ${prop.node.type}`)
}

return {
name: prop.node.name,
type: 'string',
required: false,
defaultValue: undefined
}
} else if (prop.type === 'object') {
if (prop.propName === null) {
throw new Error('Unexpected prop with null name.')
}

const type = optionGetType(prop.value, sourceCode)
const required = optionGetRequired(prop.value)
const defaultValue = optionGetDefault(prop.value)

return {
name: prop.propName,
type,
required,
defaultValue
}
}
throw new Error(`Unexpected prop type: ${prop.type}.`)
}

/**
Expand Down
28 changes: 24 additions & 4 deletions tests/lib/rules/define-props-declaration.js
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,7 @@ tester.run('define-props-declaration', rule, {
code: `
<script setup lang="ts">
interface Kind { id: number; name: string }
const props = defineProps({
kind: {
type: Object as PropType<Kind>,
Expand All @@ -376,7 +376,7 @@ tester.run('define-props-declaration', rule, {
output: `
<script setup lang="ts">
interface Kind { id: number; name: string }
const props = defineProps<{ kind?: Kind }>()
</script>
`,
Expand All @@ -393,7 +393,7 @@ tester.run('define-props-declaration', rule, {
code: `
<script setup lang="ts">
import Kind from 'test'
const props = defineProps({
kind: {
type: Object as PropType<Kind>,
Expand All @@ -404,7 +404,7 @@ tester.run('define-props-declaration', rule, {
output: `
<script setup lang="ts">
import Kind from 'test'
const props = defineProps<{ kind?: Kind }>()
</script>
`,
Expand Down Expand Up @@ -633,6 +633,26 @@ tester.run('define-props-declaration', rule, {
line: 3
}
]
},
// array
{
filename: 'test.vue',
code: `
<script setup lang="ts">
const props = defineProps([kind])
</script>
`,
output: `
<script setup lang="ts">
const props = defineProps<{ kind?: string }>()
</script>
`,
errors: [
{
message: 'Use type-based declaration instead of runtime declaration.',
line: 3
}
]
}
]
})

0 comments on commit 1d58a2b

Please sign in to comment.