From d4c04e979934b81a30467aa4b1e717175b9b2d80 Mon Sep 17 00:00:00 2001 From: Evan You Date: Fri, 8 Oct 2021 12:52:48 -0400 Subject: [PATCH] fix(compiler-sfc): fix props codegen w/ leading import fix #4764 --- .../__snapshots__/compileScript.spec.ts.snap | 32 ++++++++++++++++--- .../compileScriptPropsTransform.spec.ts.snap | 1 + .../__tests__/compileScript.spec.ts | 22 ++++++++++--- packages/compiler-sfc/src/compileScript.ts | 10 +++--- 4 files changed, 51 insertions(+), 14 deletions(-) diff --git a/packages/compiler-sfc/__tests__/__snapshots__/compileScript.spec.ts.snap b/packages/compiler-sfc/__tests__/__snapshots__/compileScript.spec.ts.snap index 79d598ae6f3..e0bdd298f4d 100644 --- a/packages/compiler-sfc/__tests__/__snapshots__/compileScript.spec.ts.snap +++ b/packages/compiler-sfc/__tests__/__snapshots__/compileScript.spec.ts.snap @@ -58,7 +58,7 @@ exports[`SFC compile + `) + // props declaration should be inside setup, not moved along with the import + expect(content).not.toMatch(`const props = __props\nimport`) + assertCode(content) + }) + test('defineEmits()', () => { const { content, bindings } = compile(` @@ -181,7 +193,7 @@ defineExpose({ foo: 123 }) const { content } = compile(` @@ -549,7 +561,7 @@ defineExpose({ foo: 123 })
diff --git a/packages/compiler-sfc/src/compileScript.ts b/packages/compiler-sfc/src/compileScript.ts index 7defa480a9f..0e60d0d9e31 100644 --- a/packages/compiler-sfc/src/compileScript.ts +++ b/packages/compiler-sfc/src/compileScript.ts @@ -1205,25 +1205,25 @@ export function compileScript( // we use a default __props so that template expressions referencing props // can use it directly if (propsIdentifier) { - s.prependRight( + s.prependLeft( startOffset, `\nconst ${propsIdentifier} = __props${ propsTypeDecl ? ` as ${genSetupPropsType(propsTypeDecl)}` : `` - }` + }\n` ) } if (propsDestructureRestId) { - s.prependRight( + s.prependLeft( startOffset, `\nconst ${propsDestructureRestId} = ${helper( `createPropsRestProxy` - )}(__props, ${JSON.stringify(Object.keys(propsDestructuredBindings))})` + )}(__props, ${JSON.stringify(Object.keys(propsDestructuredBindings))})\n` ) } // inject temp variables for async context preservation if (hasAwait) { const any = isTS ? `: any` : `` - s.prependRight(startOffset, `\nlet __temp${any}, __restore${any}\n`) + s.prependLeft(startOffset, `\nlet __temp${any}, __restore${any}\n`) } const destructureElements =