From 5a3d45ae29e26938a36e16c7ab9a804bfe4bcb08 Mon Sep 17 00:00:00 2001 From: Evan You Date: Thu, 10 Nov 2022 17:02:45 +0800 Subject: [PATCH] fix(sfc): ensure consistent dev/prod behavior for non-reactive variables declared in ` `) - expect(content).toMatch('return { aa, bb, cc, dd, a, b, c, d, xx, x }') + expect(content).toMatch( + 'return { get aa() { return aa }, bb, cc, dd, get a() { return a }, b, c, d, xx, x }' + ) expect(bindings).toStrictEqual({ x: BindingTypes.SETUP_MAYBE_REF, a: BindingTypes.SETUP_LET, diff --git a/packages/compiler-sfc/__tests__/compileScriptRefTransform.spec.ts b/packages/compiler-sfc/__tests__/compileScriptRefTransform.spec.ts index 88d62f2b478..6d601379749 100644 --- a/packages/compiler-sfc/__tests__/compileScriptRefTransform.spec.ts +++ b/packages/compiler-sfc/__tests__/compileScriptRefTransform.spec.ts @@ -32,7 +32,9 @@ describe('sfc ref transform', () => { // normal declarations left untouched expect(content).toMatch(`let c = () => {}`) expect(content).toMatch(`let d`) - expect(content).toMatch(`return { foo, a, b, c, d, ref, shallowRef }`) + expect(content).toMatch( + `return { foo, a, b, get c() { return c }, get d() { return d }, ref, shallowRef }` + ) assertCode(content) expect(bindings).toStrictEqual({ foo: BindingTypes.SETUP_REF, diff --git a/packages/compiler-sfc/src/compileScript.ts b/packages/compiler-sfc/src/compileScript.ts index e944b764725..74e3dbd7349 100644 --- a/packages/compiler-sfc/src/compileScript.ts +++ b/packages/compiler-sfc/src/compileScript.ts @@ -1484,7 +1484,15 @@ export function compileScript( allBindings[key] = true } } - returned = `{ ${Object.keys(allBindings).join(', ')} }` + returned = `{ ` + for (const key in allBindings) { + if (bindingMetadata[key] === BindingTypes.SETUP_LET) { + returned += `get ${key}() { return ${key} }, ` + } else { + returned += `${key}, ` + } + } + returned = returned.replace(/, $/, '') + ` }` } else { // inline mode if (sfc.template && !sfc.template.src) {