Skip to content

Commit ed8c3fb

Browse files
committed
wip
1 parent 980842e commit ed8c3fb

File tree

7 files changed

+491
-70
lines changed

7 files changed

+491
-70
lines changed

packages/compiler-sfc/__tests__/__snapshots__/compileScript.spec.ts.snap

+86
Original file line numberDiff line numberDiff line change
@@ -653,6 +653,48 @@ return { }
653653
}"
654654
`;
655655

656+
exports[`SFC compile <script setup> > defineModel() > basic usage 1`] = `
657+
"import { useModel as _useModel } from 'vue'
658+
659+
export default {
660+
props: {
661+
\\"modelValue\\": { required: true },
662+
\\"count\\": {},
663+
},
664+
emits: [\\"update:modelValue\\", \\"update:count\\"],
665+
setup(__props, { expose: __expose }) {
666+
__expose();
667+
668+
const modelValue = _useModel(\\"modelValue\\", { required: true })
669+
const c = _useModel(\\"count\\")
670+
671+
return { modelValue, c }
672+
}
673+
674+
}"
675+
`;
676+
677+
exports[`SFC compile <script setup> > defineModel() > w/ defineProps and defineEmits 1`] = `
678+
"import { useModel as _useModel, mergeModels as _mergeModels } from 'vue'
679+
680+
export default {
681+
props: _mergeModels({ foo: String }, {
682+
\\"modelValue\\": { default: 0 },
683+
}),
684+
emits: merge(['change'], [\\"update:modelValue\\"]),
685+
setup(__props, { expose: __expose }) {
686+
__expose();
687+
688+
689+
690+
const count = _useModel(\\"modelValue\\", { default: 0 })
691+
692+
return { count }
693+
}
694+
695+
}"
696+
`;
697+
656698
exports[`SFC compile <script setup> > defineOptions() > basic usage 1`] = `
657699
"export default /*#__PURE__*/Object.assign({ name: 'FooApp' }, {
658700
setup(__props, { expose: __expose }) {
@@ -1567,6 +1609,50 @@ return { emit }
15671609
})"
15681610
`;
15691611

1612+
exports[`SFC compile <script setup> > with TypeScript > defineModel > basic usage 1`] = `
1613+
"import { useModel as _useModel, defineComponent as _defineComponent } from 'vue'
1614+
1615+
export default /*#__PURE__*/_defineComponent({
1616+
props: {
1617+
\\"modelValue\\": [Boolean, String],
1618+
\\"count\\": Number,
1619+
},
1620+
emits: [\\"update:modelValue\\", \\"update:count\\"],
1621+
setup(__props, { expose: __expose }) {
1622+
__expose();
1623+
1624+
const modelValue = _useModel(\\"modelValue\\")
1625+
const count = _useModel(\\"count\\")
1626+
1627+
return { modelValue, count }
1628+
}
1629+
1630+
})"
1631+
`;
1632+
1633+
exports[`SFC compile <script setup> > with TypeScript > defineModel > w/ production mode 1`] = `
1634+
"import { useModel as _useModel, defineComponent as _defineComponent } from 'vue'
1635+
1636+
export default /*#__PURE__*/_defineComponent({
1637+
props: {
1638+
\\"modelValue\\": Boolean,
1639+
\\"fn\\": Function,
1640+
\\"str\\": {},
1641+
},
1642+
emits: [\\"update:modelValue\\", \\"update:fn\\", \\"update:str\\"],
1643+
setup(__props, { expose: __expose }) {
1644+
__expose();
1645+
1646+
const modelValue = _useModel(\\"modelValue\\")
1647+
const fn = _useModel(\\"fn\\")
1648+
const str = _useModel(\\"str\\")
1649+
1650+
return { modelValue, fn, str }
1651+
}
1652+
1653+
})"
1654+
`;
1655+
15701656
exports[`SFC compile <script setup> > with TypeScript > defineProps w/ TS assertion 1`] = `
15711657
"import { defineComponent as _defineComponent } from 'vue'
15721658

packages/compiler-sfc/__tests__/compileScript.spec.ts

+107
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,61 @@ defineExpose({ foo: 123 })
321321
expect(content).toMatch(/\b__expose\(\{ foo: 123 \}\)/)
322322
})
323323

324+
describe('defineModel()', () => {
325+
test('basic usage', () => {
326+
const { content, bindings } = compile(
327+
`
328+
<script setup>
329+
const modelValue = defineModel({ required: true })
330+
const c = defineModel('count')
331+
</script>
332+
`,
333+
{ defineModel: true }
334+
)
335+
assertCode(content)
336+
expect(content).toMatch('props: {\n "modelValue": { required: true }')
337+
expect(content).toMatch('"count": {},')
338+
expect(content).toMatch('emits: ["update:modelValue", "update:count"],')
339+
expect(content).toMatch(
340+
`const modelValue = _useModel("modelValue", { required: true })`
341+
)
342+
expect(content).toMatch(`const c = _useModel("count")`)
343+
expect(content).toMatch(`return { modelValue, c }`)
344+
expect(content).not.toMatch('defineModel')
345+
346+
expect(bindings).toStrictEqual({
347+
modelValue: BindingTypes.SETUP_REF,
348+
count: BindingTypes.PROPS,
349+
c: BindingTypes.SETUP_REF
350+
})
351+
})
352+
353+
test('w/ defineProps and defineEmits', () => {
354+
const { content, bindings } = compile(
355+
`
356+
<script setup>
357+
defineProps({ foo: String })
358+
defineEmits(['change'])
359+
const count = defineModel({ default: 0 })
360+
</script>
361+
`,
362+
{ defineModel: true }
363+
)
364+
assertCode(content)
365+
expect(content).toMatch(`props: _mergeModels({ foo: String }`)
366+
expect(content).toMatch(`"modelValue": { default: 0 }`)
367+
expect(content).toMatch(
368+
`const count = _useModel("modelValue", { default: 0 })`
369+
)
370+
expect(content).not.toMatch('defineModel')
371+
expect(bindings).toStrictEqual({
372+
count: BindingTypes.SETUP_REF,
373+
foo: BindingTypes.PROPS,
374+
modelValue: BindingTypes.PROPS
375+
})
376+
})
377+
})
378+
324379
test('<script> after <script setup> the script content not end with `\\n`', () => {
325380
const { content } = compile(`
326381
<script setup>
@@ -1666,6 +1721,58 @@ const emit = defineEmits(['a', 'b'])
16661721
})
16671722
})
16681723

1724+
describe('defineModel', () => {
1725+
test('basic usage', () => {
1726+
const { content, bindings } = compile(
1727+
`
1728+
<script setup lang="ts">
1729+
const modelValue = defineModel<boolean | string>()
1730+
const count = defineModel<number>('count')
1731+
</script>
1732+
`,
1733+
{ defineModel: true }
1734+
)
1735+
assertCode(content)
1736+
expect(content).toMatch('"modelValue": [Boolean, String]')
1737+
expect(content).toMatch('"count": Number')
1738+
expect(content).toMatch('emits: ["update:modelValue", "update:count"]')
1739+
expect(content).toMatch(`const modelValue = _useModel("modelValue")`)
1740+
expect(content).toMatch(`const count = _useModel("count")`)
1741+
expect(bindings).toStrictEqual({
1742+
modelValue: BindingTypes.SETUP_REF,
1743+
count: BindingTypes.SETUP_REF
1744+
})
1745+
})
1746+
1747+
test('w/ production mode', () => {
1748+
const { content, bindings } = compile(
1749+
`
1750+
<script setup lang="ts">
1751+
const modelValue = defineModel<boolean>()
1752+
const fn = defineModel<() => void>('fn')
1753+
const str = defineModel<string>('str')
1754+
</script>
1755+
`,
1756+
{ defineModel: true, isProd: true }
1757+
)
1758+
assertCode(content)
1759+
expect(content).toMatch('"modelValue": Boolean')
1760+
expect(content).toMatch('"fn": Function')
1761+
expect(content).toMatch('"str": {}')
1762+
expect(content).toMatch(
1763+
'emits: ["update:modelValue", "update:fn", "update:str"]'
1764+
)
1765+
expect(content).toMatch(`const modelValue = _useModel("modelValue")`)
1766+
expect(content).toMatch(`const fn = _useModel("fn")`)
1767+
expect(content).toMatch(`const str = _useModel("str")`)
1768+
expect(bindings).toStrictEqual({
1769+
modelValue: BindingTypes.SETUP_REF,
1770+
fn: BindingTypes.SETUP_REF,
1771+
str: BindingTypes.SETUP_REF
1772+
})
1773+
})
1774+
})
1775+
16691776
test('runtime Enum', () => {
16701777
const { content, bindings } = compile(
16711778
`<script setup lang="ts">

0 commit comments

Comments
 (0)