From 47d73c23e18b3951d299497990081aca696814c4 Mon Sep 17 00:00:00 2001 From: Evan You Date: Tue, 24 Nov 2020 15:12:59 -0500 Subject: [PATCH] wip: defineOptions -> defineProps + defineEmit + useContext --- .../__snapshots__/compileScript.spec.ts.snap | 152 ++++--- .../__snapshots__/cssVars.spec.ts.snap | 4 +- .../__tests__/compileScript.spec.ts | 231 ++++++----- .../compiler-sfc/__tests__/cssVars.spec.ts | 8 +- packages/compiler-sfc/src/compileScript.ts | 372 ++++++++++-------- packages/runtime-core/src/apiDefineOptions.ts | 91 ----- packages/runtime-core/src/apiSetupHelpers.ts | 60 +++ packages/runtime-core/src/component.ts | 6 +- packages/runtime-core/src/componentOptions.ts | 2 +- .../runtime-core/src/componentRenderUtils.ts | 3 +- packages/runtime-core/src/index.ts | 2 +- test-dts/defineOptions.test-d.ts | 96 ----- test-dts/setupHelpers.test-d.ts | 89 +++++ 13 files changed, 593 insertions(+), 523 deletions(-) delete mode 100644 packages/runtime-core/src/apiDefineOptions.ts create mode 100644 packages/runtime-core/src/apiSetupHelpers.ts delete mode 100644 test-dts/defineOptions.test-d.ts create mode 100644 test-dts/setupHelpers.test-d.ts diff --git a/packages/compiler-sfc/__tests__/__snapshots__/compileScript.spec.ts.snap b/packages/compiler-sfc/__tests__/__snapshots__/compileScript.spec.ts.snap index 9d05d643..11658523 100644 --- a/packages/compiler-sfc/__tests__/__snapshots__/compileScript.spec.ts.snap +++ b/packages/compiler-sfc/__tests__/__snapshots__/compileScript.spec.ts.snap @@ -33,38 +33,55 @@ return { x } export const n = 1" `; -exports[`SFC compile `) @@ -36,21 +32,42 @@ const bar = 1 expect(bindings).toStrictEqual({ foo: BindingTypes.PROPS, bar: BindingTypes.SETUP_CONST, - props: BindingTypes.SETUP_CONST, - emit: BindingTypes.SETUP_CONST + props: BindingTypes.SETUP_CONST }) // should remove defineOptions import and call - expect(content).not.toMatch('defineOptions') + expect(content).not.toMatch('defineProps') // should generate correct setup signature - expect(content).toMatch(`setup(__props, { props, emit }) {`) + expect(content).toMatch(`setup(__props) {`) + // should assign user identifier to it + expect(content).toMatch(`const props = __props`) // should include context options in default export expect(content).toMatch(`export default { expose: [], props: { - foo: String - }, - emit: ['a', 'b'],`) + foo: String +},`) + }) + + test('defineEmit()', () => { + const { content, bindings } = compile(` + + `) + assertCode(content) + expect(bindings).toStrictEqual({ + myEmit: BindingTypes.SETUP_CONST + }) + // should remove defineOptions import and call + expect(content).not.toMatch('defineEmit') + // should generate correct setup signature + expect(content).toMatch(`setup(__props, { emit: myEmit }) {`) + // should include context options in default export + expect(content).toMatch(`export default { + expose: [], + emits: ['foo', 'bar'],`) }) describe(' `) assertCode(content) @@ -375,42 +390,40 @@ const { props, emit } = defineOptions({ expose: [], props: { foo: String }, emits: ['a', 'b'], - setup(__props, { props, emit }) {`) + setup(__props, { emit }) {`) }) - test('defineOptions w/ type / extract props', () => { + test('defineProps w/ type', () => { const { content, bindings } = compile(` `) assertCode(content) @@ -466,33 +479,28 @@ const { props, emit } = defineOptions({ }) }) - test('defineOptions w/ type / extract emits', () => { + test('defineEmit w/ type', () => { const { content } = compile(` `) assertCode(content) - expect(content).toMatch(`props: {},\n emit: (e: 'foo' | 'bar') => void,`) + expect(content).toMatch(`emit: ((e: 'foo' | 'bar') => void),`) expect(content).toMatch(`emits: ["foo", "bar"] as unknown as undefined`) }) - test('defineOptions w/ type / extract emits (union)', () => { + test('defineEmit w/ type (union)', () => { + const type = `((e: 'foo' | 'bar') => void) | ((e: 'baz', id: number) => void)` const { content } = compile(` `) assertCode(content) - expect(content).toMatch( - `props: {},\n emit: ((e: 'foo' | 'bar') => void) | ((e: 'baz', id: number) => void),` - ) + expect(content).toMatch(`emit: (${type}),`) expect(content).toMatch( `emits: ["foo", "bar", "baz"] as unknown as undefined` ) @@ -774,71 +782,96 @@ const { props, emit } = defineOptions({ ).toThrow(`ref: statements can only contain assignment expressions`) }) - test('defineOptions() w/ both type and non-type args', () => { + test('defineProps/Emit() w/ both type and non-type args', () => { expect(() => { compile(``) + }).toThrow(`cannot accept both type and non-type arguments`) + + expect(() => { + compile(``) }).toThrow(`cannot accept both type and non-type arguments`) }) - test('defineOptions() referencing local var', () => { + test('defineProps/Emit() referencing local var', () => { expect(() => compile(``) ).toThrow(`cannot reference locally declared variables`) - }) - test('defineOptions() referencing ref declarations', () => { expect(() => compile(``) + ).toThrow(`cannot reference locally declared variables`) + }) + + test('defineProps/Emit() referencing ref declarations', () => { + expect(() => + compile(``) + ).toThrow(`cannot reference locally declared variables`) + + expect(() => + compile(``) ).toThrow(`cannot reference locally declared variables`) }) - test('should allow defineOptions() referencing scope var', () => { + test('should allow defineProps/Emit() referencing scope var', () => { assertCode( compile(``).content ) }) - test('should allow defineOptions() referencing imported binding', () => { + test('should allow defineProps/Emit() referencing imported binding', () => { assertCode( compile(``).content ) }) @@ -1063,11 +1096,9 @@ describe('SFC analyze \n` + `