From ed8c3fba531629b2761b683703c9d23f4cc014fc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=89=E5=92=B2=E6=99=BA=E5=AD=90=20Kevin=20Deng?= Date: Wed, 5 Apr 2023 00:12:59 +0800 Subject: [PATCH] wip --- .../__snapshots__/compileScript.spec.ts.snap | 86 +++++++ .../__tests__/compileScript.spec.ts | 107 ++++++++ packages/compiler-sfc/src/compileScript.ts | 237 +++++++++++++----- .../__tests__/apiSetupHelpers.spec.ts | 44 +++- packages/runtime-core/src/apiSetupHelpers.ts | 83 +++++- packages/runtime-core/src/index.ts | 1 + packages/sfc-playground/src/App.vue | 3 +- 7 files changed, 491 insertions(+), 70 deletions(-) diff --git a/packages/compiler-sfc/__tests__/__snapshots__/compileScript.spec.ts.snap b/packages/compiler-sfc/__tests__/__snapshots__/compileScript.spec.ts.snap index ee00977c131..a1d37621345 100644 --- a/packages/compiler-sfc/__tests__/__snapshots__/compileScript.spec.ts.snap +++ b/packages/compiler-sfc/__tests__/__snapshots__/compileScript.spec.ts.snap @@ -653,6 +653,48 @@ return { } }" `; +exports[`SFC compile + `, + { defineModel: true } + ) + assertCode(content) + expect(content).toMatch('props: {\n "modelValue": { required: true }') + expect(content).toMatch('"count": {},') + expect(content).toMatch('emits: ["update:modelValue", "update:count"],') + expect(content).toMatch( + `const modelValue = _useModel("modelValue", { required: true })` + ) + expect(content).toMatch(`const c = _useModel("count")`) + expect(content).toMatch(`return { modelValue, c }`) + expect(content).not.toMatch('defineModel') + + expect(bindings).toStrictEqual({ + modelValue: BindingTypes.SETUP_REF, + count: BindingTypes.PROPS, + c: BindingTypes.SETUP_REF + }) + }) + + test('w/ defineProps and defineEmits', () => { + const { content, bindings } = compile( + ` + + `, + { defineModel: true } + ) + assertCode(content) + expect(content).toMatch(`props: _mergeModels({ foo: String }`) + expect(content).toMatch(`"modelValue": { default: 0 }`) + expect(content).toMatch( + `const count = _useModel("modelValue", { default: 0 })` + ) + expect(content).not.toMatch('defineModel') + expect(bindings).toStrictEqual({ + count: BindingTypes.SETUP_REF, + foo: BindingTypes.PROPS, + modelValue: BindingTypes.PROPS + }) + }) + }) + test(' + `, + { defineModel: true } + ) + assertCode(content) + expect(content).toMatch('"modelValue": [Boolean, String]') + expect(content).toMatch('"count": Number') + expect(content).toMatch('emits: ["update:modelValue", "update:count"]') + expect(content).toMatch(`const modelValue = _useModel("modelValue")`) + expect(content).toMatch(`const count = _useModel("count")`) + expect(bindings).toStrictEqual({ + modelValue: BindingTypes.SETUP_REF, + count: BindingTypes.SETUP_REF + }) + }) + + test('w/ production mode', () => { + const { content, bindings } = compile( + ` + + `, + { defineModel: true, isProd: true } + ) + assertCode(content) + expect(content).toMatch('"modelValue": Boolean') + expect(content).toMatch('"fn": Function') + expect(content).toMatch('"str": {}') + expect(content).toMatch( + 'emits: ["update:modelValue", "update:fn", "update:str"]' + ) + expect(content).toMatch(`const modelValue = _useModel("modelValue")`) + expect(content).toMatch(`const fn = _useModel("fn")`) + expect(content).toMatch(`const str = _useModel("str")`) + expect(bindings).toStrictEqual({ + modelValue: BindingTypes.SETUP_REF, + fn: BindingTypes.SETUP_REF, + str: BindingTypes.SETUP_REF + }) + }) + }) + test('runtime Enum', () => { const { content, bindings } = compile( `