Skip to content

Commit

Permalink
fix(compiler-sfc): fix missing ReadonlyArray, ReadonlySet, ReadonlyMa…
Browse files Browse the repository at this point in the history
…p and UnknownType
  • Loading branch information
NathanFreeman committed Apr 20, 2024
1 parent 6d066dd commit 6808b11
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,46 @@ return { modelValue }
})"
`;

exports[`defineModel() > w/ ReadonlyArray types 1`] = `
"import { useModel as _useModel, defineComponent as _defineComponent } from 'vue'
export default /*#__PURE__*/_defineComponent({
props: {
"modelValue": { type: [Boolean, String, ReadonlyArray, Number, Object] },
"modelModifiers": {},
},
emits: ["update:modelValue"],
setup(__props, { expose: __expose }) {
__expose();
const modelValue = _useModel<boolean | string | ReadonlyArray<string> | number | Readonly<Record>>(__props, "modelValue")
return { modelValue }
}
})"
`;

exports[`defineModel() > w/ ReadonlyArray, ReadonlySet, ReadonlyMap and UnknownType 1`] = `
"import { useModel as _useModel, defineComponent as _defineComponent } from 'vue'
export default /*#__PURE__*/_defineComponent({
props: {
"modelValue": { type: [String, ReadonlyArray, ReadonlySet, ReadonlyMap], skipCheck: true },
"modelModifiers": {},
},
emits: ["update:modelValue"],
setup(__props, { expose: __expose }) {
__expose();
const modelValue = _useModel<UnknownType | string | ReadonlyArray<string> | ReadonlySet<string> | ReadonlyMap<string>>(__props, "modelValue")
return { modelValue }
}
})"
`;

exports[`defineModel() > w/ array props 1`] = `
"import { useModel as _useModel, mergeModels as _mergeModels } from 'vue'
Expand Down
20 changes: 20 additions & 0 deletions packages/compiler-sfc/__tests__/compileScript/defineModel.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -241,4 +241,24 @@ describe('defineModel()', () => {
modelValue: BindingTypes.SETUP_REF,
})
})

test('w/ ReadonlyArray, ReadonlySet, ReadonlyMap and UnknownType', () => {
const { content, bindings } = compile(
`
<script setup lang="ts">
const modelValue = defineModel<UnknownType | string | ReadonlyArray<string> | ReadonlySet<string> | ReadonlyMap<string>>()
</script>
`,
)
assertCode(content)
expect(content).toMatch(
' "modelValue": { type: [String, ReadonlyArray, ReadonlySet, ReadonlyMap], skipCheck: true }',
)
expect(content).toMatch(
`const modelValue = _useModel<UnknownType | string | ReadonlyArray<string> | ReadonlySet<string> | ReadonlyMap<string>>(__props, "modelValue")`,
)
expect(bindings).toStrictEqual({
modelValue: BindingTypes.SETUP_REF,
})
})
})
17 changes: 11 additions & 6 deletions packages/compiler-sfc/src/script/defineModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,12 +133,17 @@ export function genModelProps(ctx: ScriptCompileContext) {
const hasUnknownType = runtimeTypes.includes(UNKNOWN_TYPE)

if (isProd || hasUnknownType) {
runtimeTypes = runtimeTypes.filter(
t =>
t === 'Boolean' ||
(hasBoolean && t === 'String') ||
(t === 'Function' && options),
)
runtimeTypes = runtimeTypes.filter(t => {
if (isProd) {
return (
t === 'Boolean' ||
(hasBoolean && t === 'String') ||
(t === 'Function' && options)
)
}

return t !== UNKNOWN_TYPE
})

skipCheck = !isProd && hasUnknownType && runtimeTypes.length > 0
}
Expand Down
3 changes: 3 additions & 0 deletions packages/compiler-sfc/src/script/resolveType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1517,10 +1517,13 @@ export function inferRuntimeType(
if (node.typeName.type === 'Identifier') {
switch (node.typeName.name) {
case 'Array':
case 'ReadonlyArray':
case 'Function':
case 'Object':
case 'Set':
case 'ReadonlySet':
case 'Map':
case 'ReadonlyMap':
case 'WeakSet':
case 'WeakMap':
case 'Date':
Expand Down

0 comments on commit 6808b11

Please sign in to comment.