Skip to content

Commit

Permalink
fix(compiler-sfc): fixed the type compilation of defineModel in the prod
Browse files Browse the repository at this point in the history
  • Loading branch information
RicardoErii committed Nov 14, 2023
1 parent f18a174 commit a82a56e
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,31 @@ return { modelValue, c, toString }
}"
`;

exports[`defineModel() > w/ Boolean And Function types, production mode 1`] = `
"import { useModel as _useModel, defineComponent as _defineComponent } from 'vue'
export default /*#__PURE__*/_defineComponent({
props: {
\\"modelValue\\": { type: [Boolean, String] },
\\"fn\\": {},
\\"fnWithDefault\\": { type: Function, ...{ default: () => null } },
\\"optional\\": { required: false },
},
emits: [\\"update:modelValue\\", \\"update:fn\\", \\"update:fnWithDefault\\", \\"update:optional\\"],
setup(__props, { expose: __expose }) {
__expose();
const modelValue = _useModel(__props, \\"modelValue\\")
const fn = _useModel(__props, \\"fn\\")
const fnWithDefault = _useModel(__props, \\"fnWithDefault\\")
const optional = _useModel(__props, \\"optional\\")
return { modelValue, fn, fnWithDefault, optional }
}
})"
`;

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

test('w/ Boolean And Function types, production mode', () => {
const { content, bindings } = compile(
`
<script setup lang="ts">
const modelValue = defineModel<boolean | string>()
const fn = defineModel<() => void>('fn')
const fnWithDefault = defineModel<() => void>('fnWithDefault', { default: () => null })
const optional = defineModel<string>('optional', { required: false })
</script>
`,
{ defineModel: true, isProd: true }
)
assertCode(content)
expect(content).toMatch('"modelValue": { type: [Boolean, String] }')
expect(content).toMatch('"fn": {}')
expect(content).toMatch(
'"fnWithDefault": { type: Function, ...{ default: () => null } },'
)
expect(content).toMatch('"optional": { required: false }')
expect(content).toMatch(
'emits: ["update:modelValue", "update:fn", "update:fnWithDefault", "update:optional"]'
)
expect(content).toMatch(
`const modelValue = _useModel(__props, "modelValue")`
)
expect(content).toMatch(`const fn = _useModel(__props, "fn")`)
expect(bindings).toStrictEqual({
modelValue: BindingTypes.SETUP_REF,
fn: BindingTypes.SETUP_REF,
fnWithDefault: BindingTypes.SETUP_REF,
optional: BindingTypes.SETUP_REF
})
})
})
16 changes: 10 additions & 6 deletions packages/compiler-sfc/src/script/defineModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,12 +120,16 @@ export function genModelProps(ctx: ScriptCompileContext) {
if (runtimeTypes) {
const hasUnknownType = runtimeTypes.includes(UNKNOWN_TYPE)

runtimeTypes = runtimeTypes.filter(el => {
if (el === UNKNOWN_TYPE) return false
return isProd
? el === 'Boolean' || (el === 'Function' && options)
: true
})
runtimeTypes = runtimeTypes.filter(el => el !== UNKNOWN_TYPE)

if (
isProd &&
!runtimeTypes.some(
el => el === 'Boolean' || (el === 'Function' && options)
)
) {
runtimeTypes = []
}
skipCheck = !isProd && hasUnknownType && runtimeTypes.length > 0
}

Expand Down

0 comments on commit a82a56e

Please sign in to comment.