From 545057715635e07ea138a4fc6fa94726964702a6 Mon Sep 17 00:00:00 2001 From: HcySunYang Date: Sat, 10 Apr 2021 16:15:40 +0800 Subject: [PATCH 1/2] fix(compiler-sfc): correctly remove parens used for wrapping --- packages/compiler-sfc/src/compileScript.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/compiler-sfc/src/compileScript.ts b/packages/compiler-sfc/src/compileScript.ts index 5bb8a2461a9..a4bdbe59737 100644 --- a/packages/compiler-sfc/src/compileScript.ts +++ b/packages/compiler-sfc/src/compileScript.ts @@ -348,7 +348,7 @@ export function compileScript( break } } - for (let i = left.end!; i > 0; i++) { + for (let i = right.end!; i > 0; i++) { const char = source[i + startOffset] if (char === ')') { s.remove(i + startOffset, i + startOffset + 1) From 958c430366b58c821451a8aae1d37447fe53efb5 Mon Sep 17 00:00:00 2001 From: HcySunYang Date: Sat, 10 Apr 2021 21:35:10 +0800 Subject: [PATCH 2/2] test: add test case --- .../__snapshots__/compileScript.spec.ts.snap | 6 ++++-- .../compiler-sfc/__tests__/compileScript.spec.ts | 12 ++++++++---- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/packages/compiler-sfc/__tests__/__snapshots__/compileScript.spec.ts.snap b/packages/compiler-sfc/__tests__/__snapshots__/compileScript.spec.ts.snap index 7397228fb3b..9887d8943d2 100644 --- a/packages/compiler-sfc/__tests__/__snapshots__/compileScript.spec.ts.snap +++ b/packages/compiler-sfc/__tests__/__snapshots__/compileScript.spec.ts.snap @@ -622,9 +622,11 @@ const c = _ref(__c); const d = _ref(__d); const f = _ref(__f); const g = _ref(__g); - console.log(n.value, a.value, c.value, d.value, f.value, g.value) + const { foo: __foo } = useSomthing(() => 1); +const foo = _ref(__foo); + console.log(n.value, a.value, c.value, d.value, f.value, g.value, foo.value) -return { n, a, c, d, f, g } +return { n, a, c, d, f, g, foo } } }" diff --git a/packages/compiler-sfc/__tests__/compileScript.spec.ts b/packages/compiler-sfc/__tests__/compileScript.spec.ts index d74d7448aa9..de578dd90fd 100644 --- a/packages/compiler-sfc/__tests__/compileScript.spec.ts +++ b/packages/compiler-sfc/__tests__/compileScript.spec.ts @@ -777,11 +777,13 @@ const emit = defineEmit(['a', 'b']) test('object destructure', () => { const { content, bindings } = compile(``) expect(content).toMatch( `const n = _ref(1), { a: __a, b: __c, d: __d = 1, e: __f = 2, ...__g } = useFoo()` ) + expect(content).toMatch(`const { foo: __foo } = useSomthing(() => 1)`) expect(content).toMatch(`\nconst a = _ref(__a);`) expect(content).not.toMatch(`\nconst b = _ref(__b);`) expect(content).toMatch(`\nconst c = _ref(__c);`) @@ -789,17 +791,19 @@ const emit = defineEmit(['a', 'b']) expect(content).not.toMatch(`\nconst e = _ref(__e);`) expect(content).toMatch(`\nconst f = _ref(__f);`) expect(content).toMatch(`\nconst g = _ref(__g);`) + expect(content).toMatch(`\nconst foo = _ref(__foo);`) expect(content).toMatch( - `console.log(n.value, a.value, c.value, d.value, f.value, g.value)` + `console.log(n.value, a.value, c.value, d.value, f.value, g.value, foo.value)` ) - expect(content).toMatch(`return { n, a, c, d, f, g }`) + expect(content).toMatch(`return { n, a, c, d, f, g, foo }`) expect(bindings).toStrictEqual({ n: BindingTypes.SETUP_REF, a: BindingTypes.SETUP_REF, c: BindingTypes.SETUP_REF, d: BindingTypes.SETUP_REF, f: BindingTypes.SETUP_REF, - g: BindingTypes.SETUP_REF + g: BindingTypes.SETUP_REF, + foo: BindingTypes.SETUP_REF }) assertCode(content) })