From a4128323d3680ddae5d35bca57e62c6c4cde1c6f 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: Sat, 10 Jun 2023 20:21:27 +0800 Subject: [PATCH 1/4] fix(compiler-sfc): don't hoist props and emit closes #7812 --- .../__snapshots__/compileScript.spec.ts.snap | 31 +++++---- .../__tests__/compileScript.spec.ts | 11 +++- .../__snapshots__/defineEmits.spec.ts.snap | 64 +++++++++---------- .../__snapshots__/defineProps.spec.ts.snap | 48 ++++---------- .../definePropsDestructure.spec.ts.snap | 4 +- .../compileScript/defineEmits.spec.ts | 6 +- packages/compiler-sfc/src/compileScript.ts | 55 ++++++++++------ packages/compiler-sfc/src/script/context.ts | 8 ++- .../compiler-sfc/src/script/defineEmits.ts | 6 +- .../compiler-sfc/src/script/defineProps.ts | 55 ++++++++-------- .../src/script/definePropsDestructure.ts | 1 - 11 files changed, 142 insertions(+), 147 deletions(-) diff --git a/packages/compiler-sfc/__tests__/__snapshots__/compileScript.spec.ts.snap b/packages/compiler-sfc/__tests__/__snapshots__/compileScript.spec.ts.snap index 18b5d90eaf6..97150312eb0 100644 --- a/packages/compiler-sfc/__tests__/__snapshots__/compileScript.spec.ts.snap +++ b/packages/compiler-sfc/__tests__/__snapshots__/compileScript.spec.ts.snap @@ -616,12 +616,11 @@ exports[`SFC compile `) assertCode(content) - expect(content).toMatch(`const a = 1;`) // test correct removal + expect(content).toMatch(`const props = __props,`) + expect(content).toMatch(`a = 1,`) + expect(content).toMatch(`emit = __emit;`) expect(content).toMatch(`props: ['item'],`) expect(content).toMatch(`emits: ['a'],`) }) @@ -92,7 +94,9 @@ describe('SFC compile `) assertCode(content) - expect(content).toMatch(`const a = 1;`) // test correct removal + expect(content).toMatch(`const a = 1,`) + expect(content).toMatch(`props = __props,`) + expect(content).toMatch(`emit = __emit;`) expect(content).toMatch(`props: ['item'],`) expect(content).toMatch(`emits: ['a'],`) }) @@ -110,7 +114,8 @@ describe('SFC compile + `) + assertCode(content) + + expect(content).toMatch(`console.log('test')`) + expect(content).toMatch(`const props = __props;`) + expect(content).toMatch(`const emit = __emit;`) + expect(content).toMatch(`(function () {})()`) + }) + test('script setup first, named default export', () => { const { content } = compile(` - `) - assertCode(content) - expect(content).toMatch(`const props = __props,`) - expect(content).toMatch(`a = 1,`) - expect(content).toMatch(`emit = __emit;`) - expect(content).toMatch(`props: ['item'],`) - expect(content).toMatch(`emits: ['a'],`) - }) - - // #6757 - test('defineProps/defineEmits in multi-variable declaration fix #6757 ', () => { - const { content } = compile(` - - `) - assertCode(content) - expect(content).toMatch(`const a = 1,`) - expect(content).toMatch(`props = __props,`) - expect(content).toMatch(`emit = __emit;`) - expect(content).toMatch(`props: ['item'],`) - expect(content).toMatch(`emits: ['a'],`) - }) - - // #7422 - test('defineProps/defineEmits in multi-variable declaration fix #7422', () => { - const { content } = compile(` - - `) - assertCode(content) - expect(content).toMatch(`props: ['item'],`) - expect(content).toMatch(`emits: ['foo'],`) - expect(content).toMatch(`const props = __props,`) - expect(content).toMatch(`a = 0,`) - expect(content).toMatch(`b = 0;`) - }) - - test('defineProps/defineEmits in multi-variable declaration (full removal)', () => { - const { content } = compile(` - - `) - assertCode(content) - expect(content).toMatch(`props: ['item'],`) - expect(content).toMatch(`emits: ['a'],`) - }) - describe(' + `) + assertCode(content) + expect(content).toMatch(`const a = 1;`) + expect(content).toMatch(`props: ['item'],`) + }) + + // #6757 + test('multi-variable declaration fix #6757 ', () => { + const { content } = compile(` + + `) + assertCode(content) + expect(content).toMatch(`const a = 1;`) + expect(content).toMatch(`props: ['item'],`) + }) + + // #7422 + test('multi-variable declaration fix #7422', () => { + const { content } = compile(` + + `) + assertCode(content) + expect(content).toMatch(`const a = 0,`) + expect(content).toMatch(`b = 0;`) + expect(content).toMatch(`props: ['item'],`) + }) + + test('defineProps/defineEmits in multi-variable declaration (full removal)', () => { + const { content } = compile(` + + `) + assertCode(content) + expect(content).toMatch(`props: ['item'],`) + expect(content).toMatch(`emits: ['a'],`) + }) + describe('errors', () => { test('should error on deep destructure', () => { expect(() =>