diff --git a/packages/compiler-sfc/__tests__/__snapshots__/compileScript.spec.ts.snap b/packages/compiler-sfc/__tests__/__snapshots__/compileScript.spec.ts.snap index 9d05d643a..11658523e 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` + `