From f7f4624191bbdc09600dbb0eb048b947c3a4f761 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: Thu, 6 Apr 2023 17:19:00 +0800 Subject: [PATCH] fix(compiler-sfc): fix binding type for constants when hoistStatic is disabled (#8029) --- .../compileScriptHoistStatic.spec.ts.snap | 12 +++++++ .../__tests__/compileScript.spec.ts | 2 +- .../compileScriptHoistStatic.spec.ts | 17 ++++++++- packages/compiler-sfc/src/compileScript.ts | 35 +++++++++++++++---- 4 files changed, 58 insertions(+), 8 deletions(-) diff --git a/packages/compiler-sfc/__tests__/__snapshots__/compileScriptHoistStatic.spec.ts.snap b/packages/compiler-sfc/__tests__/__snapshots__/compileScriptHoistStatic.spec.ts.snap index ed5301432..3b94955f2 100644 --- a/packages/compiler-sfc/__tests__/__snapshots__/compileScriptHoistStatic.spec.ts.snap +++ b/packages/compiler-sfc/__tests__/__snapshots__/compileScriptHoistStatic.spec.ts.snap @@ -130,3 +130,15 @@ return () => {} }" `; + +exports[`sfc hoist static > should not hoist when disabled 1`] = ` +"export default { + setup(__props) { + + const foo = 'bar' + +return () => {} +} + +}" +`; diff --git a/packages/compiler-sfc/__tests__/compileScript.spec.ts b/packages/compiler-sfc/__tests__/compileScript.spec.ts index 1d8d20ec1..2da79cf83 100644 --- a/packages/compiler-sfc/__tests__/compileScript.spec.ts +++ b/packages/compiler-sfc/__tests__/compileScript.spec.ts @@ -39,7 +39,7 @@ describe('SFC compile `) expect(bindings).toStrictEqual({ - foo: BindingTypes.LITERAL_CONST + foo: BindingTypes.SETUP_CONST + }) + assertCode(content) + }) + + test('should not hoist when disabled', () => { + const { content, bindings } = compile( + ` + + `, + { hoistStatic: false } + ) + expect(bindings).toStrictEqual({ + foo: BindingTypes.SETUP_CONST }) assertCode(content) }) diff --git a/packages/compiler-sfc/src/compileScript.ts b/packages/compiler-sfc/src/compileScript.ts index 31114c52d..b00c17799 100644 --- a/packages/compiler-sfc/src/compileScript.ts +++ b/packages/compiler-sfc/src/compileScript.ts @@ -803,7 +803,7 @@ export function compileScript( if (!node) return walkIdentifiers(node, id => { const binding = setupBindings[id.name] - if (binding && (binding !== BindingTypes.LITERAL_CONST || !hoistStatic)) { + if (binding && binding !== BindingTypes.LITERAL_CONST) { error( `\`${method}()\` in