Skip to content

Commit f72c1f2

Browse files
committed
wip
1 parent 4607c27 commit f72c1f2

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

Lines changed: 86 additions & 0 deletions
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 }) {
@@ -1580,6 +1622,50 @@ return { emit }
15801622
})"
15811623
`;
15821624

1625+
exports[`SFC compile <script setup> > with TypeScript > defineModel > basic usage 1`] = `
1626+
"import { useModel as _useModel, defineComponent as _defineComponent } from 'vue'
1627+
1628+
export default /*#__PURE__*/_defineComponent({
1629+
props: {
1630+
\\"modelValue\\": [Boolean, String],
1631+
\\"count\\": Number,
1632+
},
1633+
emits: [\\"update:modelValue\\", \\"update:count\\"],
1634+
setup(__props, { expose: __expose }) {
1635+
__expose();
1636+
1637+
const modelValue = _useModel(\\"modelValue\\")
1638+
const count = _useModel(\\"count\\")
1639+
1640+
return { modelValue, count }
1641+
}
1642+
1643+
})"
1644+
`;
1645+
1646+
exports[`SFC compile <script setup> > with TypeScript > defineModel > w/ production mode 1`] = `
1647+
"import { useModel as _useModel, defineComponent as _defineComponent } from 'vue'
1648+
1649+
export default /*#__PURE__*/_defineComponent({
1650+
props: {
1651+
\\"modelValue\\": Boolean,
1652+
\\"fn\\": Function,
1653+
\\"str\\": {},
1654+
},
1655+
emits: [\\"update:modelValue\\", \\"update:fn\\", \\"update:str\\"],
1656+
setup(__props, { expose: __expose }) {
1657+
__expose();
1658+
1659+
const modelValue = _useModel(\\"modelValue\\")
1660+
const fn = _useModel(\\"fn\\")
1661+
const str = _useModel(\\"str\\")
1662+
1663+
return { modelValue, fn, str }
1664+
}
1665+
1666+
})"
1667+
`;
1668+
15831669
exports[`SFC compile <script setup> > with TypeScript > defineProps w/ TS assertion 1`] = `
15841670
"import { defineComponent as _defineComponent } from 'vue'
15851671

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

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,61 @@ defineExpose({ foo: 123 })
365365
expect(content).toMatch(/\b__expose\(\{ foo: 123 \}\)/)
366366
})
367367

368+
describe('defineModel()', () => {
369+
test('basic usage', () => {
370+
const { content, bindings } = compile(
371+
`
372+
<script setup>
373+
const modelValue = defineModel({ required: true })
374+
const c = defineModel('count')
375+
</script>
376+
`,
377+
{ defineModel: true }
378+
)
379+
assertCode(content)
380+
expect(content).toMatch('props: {\n "modelValue": { required: true }')
381+
expect(content).toMatch('"count": {},')
382+
expect(content).toMatch('emits: ["update:modelValue", "update:count"],')
383+
expect(content).toMatch(
384+
`const modelValue = _useModel("modelValue", { required: true })`
385+
)
386+
expect(content).toMatch(`const c = _useModel("count")`)
387+
expect(content).toMatch(`return { modelValue, c }`)
388+
expect(content).not.toMatch('defineModel')
389+
390+
expect(bindings).toStrictEqual({
391+
modelValue: BindingTypes.SETUP_REF,
392+
count: BindingTypes.PROPS,
393+
c: BindingTypes.SETUP_REF
394+
})
395+
})
396+
397+
test('w/ defineProps and defineEmits', () => {
398+
const { content, bindings } = compile(
399+
`
400+
<script setup>
401+
defineProps({ foo: String })
402+
defineEmits(['change'])
403+
const count = defineModel({ default: 0 })
404+
</script>
405+
`,
406+
{ defineModel: true }
407+
)
408+
assertCode(content)
409+
expect(content).toMatch(`props: _mergeModels({ foo: String }`)
410+
expect(content).toMatch(`"modelValue": { default: 0 }`)
411+
expect(content).toMatch(
412+
`const count = _useModel("modelValue", { default: 0 })`
413+
)
414+
expect(content).not.toMatch('defineModel')
415+
expect(bindings).toStrictEqual({
416+
count: BindingTypes.SETUP_REF,
417+
foo: BindingTypes.PROPS,
418+
modelValue: BindingTypes.PROPS
419+
})
420+
})
421+
})
422+
368423
test('<script> after <script setup> the script content not end with `\\n`', () => {
369424
const { content } = compile(`
370425
<script setup>
@@ -1710,6 +1765,58 @@ const emit = defineEmits(['a', 'b'])
17101765
})
17111766
})
17121767

1768+
describe('defineModel', () => {
1769+
test('basic usage', () => {
1770+
const { content, bindings } = compile(
1771+
`
1772+
<script setup lang="ts">
1773+
const modelValue = defineModel<boolean | string>()
1774+
const count = defineModel<number>('count')
1775+
</script>
1776+
`,
1777+
{ defineModel: true }
1778+
)
1779+
assertCode(content)
1780+
expect(content).toMatch('"modelValue": [Boolean, String]')
1781+
expect(content).toMatch('"count": Number')
1782+
expect(content).toMatch('emits: ["update:modelValue", "update:count"]')
1783+
expect(content).toMatch(`const modelValue = _useModel("modelValue")`)
1784+
expect(content).toMatch(`const count = _useModel("count")`)
1785+
expect(bindings).toStrictEqual({
1786+
modelValue: BindingTypes.SETUP_REF,
1787+
count: BindingTypes.SETUP_REF
1788+
})
1789+
})
1790+
1791+
test('w/ production mode', () => {
1792+
const { content, bindings } = compile(
1793+
`
1794+
<script setup lang="ts">
1795+
const modelValue = defineModel<boolean>()
1796+
const fn = defineModel<() => void>('fn')
1797+
const str = defineModel<string>('str')
1798+
</script>
1799+
`,
1800+
{ defineModel: true, isProd: true }
1801+
)
1802+
assertCode(content)
1803+
expect(content).toMatch('"modelValue": Boolean')
1804+
expect(content).toMatch('"fn": Function')
1805+
expect(content).toMatch('"str": {}')
1806+
expect(content).toMatch(
1807+
'emits: ["update:modelValue", "update:fn", "update:str"]'
1808+
)
1809+
expect(content).toMatch(`const modelValue = _useModel("modelValue")`)
1810+
expect(content).toMatch(`const fn = _useModel("fn")`)
1811+
expect(content).toMatch(`const str = _useModel("str")`)
1812+
expect(bindings).toStrictEqual({
1813+
modelValue: BindingTypes.SETUP_REF,
1814+
fn: BindingTypes.SETUP_REF,
1815+
str: BindingTypes.SETUP_REF
1816+
})
1817+
})
1818+
})
1819+
17131820
test('runtime Enum', () => {
17141821
const { content, bindings } = compile(
17151822
`<script setup lang="ts">

0 commit comments

Comments
 (0)