From 5e55e6daa8f68592366e89969de1d126e86ff88c Mon Sep 17 00:00:00 2001 From: Xesxen Date: Tue, 10 Oct 2023 04:25:26 +0200 Subject: [PATCH 1/5] fix(compiler-core): emit TS-compatible function declaration when requested --- .../__snapshots__/scopeId.spec.ts.snap | 16 ++++++++++++ .../compiler-core/__tests__/scopeId.spec.ts | 25 +++++++++++++++++++ packages/compiler-core/src/codegen.ts | 3 ++- 3 files changed, 43 insertions(+), 1 deletion(-) diff --git a/packages/compiler-core/__tests__/__snapshots__/scopeId.spec.ts.snap b/packages/compiler-core/__tests__/__snapshots__/scopeId.spec.ts.snap index 445d3fd13ed..ea1910d39f2 100644 --- a/packages/compiler-core/__tests__/__snapshots__/scopeId.spec.ts.snap +++ b/packages/compiler-core/__tests__/__snapshots__/scopeId.spec.ts.snap @@ -16,6 +16,22 @@ export function render(_ctx, _cache) { }" `; +exports[`scopeId compiler support > should push typescript-compatible scopeId for hoisted nodes 1`] = ` +"import { createElementVNode as _createElementVNode, toDisplayString as _toDisplayString, createTextVNode as _createTextVNode, openBlock as _openBlock, createElementBlock as _createElementBlock, pushScopeId as _pushScopeId, popScopeId as _popScopeId } from \\"vue\\" + +const _withScopeId = (n: () => any) => (_pushScopeId(\\"test\\"),n=n(),_popScopeId(),n) +const _hoisted_1 = /*#__PURE__*/ _withScopeId(() => /*#__PURE__*/_createElementVNode(\\"div\\", null, \\"hello\\", -1 /* HOISTED */)) +const _hoisted_2 = /*#__PURE__*/ _withScopeId(() => /*#__PURE__*/_createElementVNode(\\"div\\", null, \\"world\\", -1 /* HOISTED */)) + +export function render(_ctx: any,_cache: any) { + return (_openBlock(), _createElementBlock(\\"div\\", null, [ + _hoisted_1, + _createTextVNode(_toDisplayString(_ctx.foo), 1 /* TEXT */), + _hoisted_2 + ])) +}" +`; + exports[`scopeId compiler support > should wrap default slot 1`] = ` "import { createElementVNode as _createElementVNode, resolveComponent as _resolveComponent, withCtx as _withCtx, openBlock as _openBlock, createBlock as _createBlock } from "vue" diff --git a/packages/compiler-core/__tests__/scopeId.spec.ts b/packages/compiler-core/__tests__/scopeId.spec.ts index c7a21df7a78..b73381b9b7d 100644 --- a/packages/compiler-core/__tests__/scopeId.spec.ts +++ b/packages/compiler-core/__tests__/scopeId.spec.ts @@ -81,4 +81,29 @@ describe('scopeId compiler support', () => { ].forEach(c => expect(code).toMatch(c)) expect(code).toMatchSnapshot() }) + + test('should push typescript-compatible scopeId for hoisted nodes', () => { + const { ast, code } = baseCompile( + `
hello
{{ foo }}
world
`, + { + mode: 'module', + scopeId: 'test', + hoistStatic: true, + isTS: true + } + ) + expect(ast.helpers).toContain(PUSH_SCOPE_ID) + expect(ast.helpers).toContain(POP_SCOPE_ID) + expect(ast.hoists.length).toBe(2) + ;[ + `const _withScopeId = (n: () => any) => (_pushScopeId("test"),n=n(),_popScopeId(),n)`, + `const _hoisted_1 = /*#__PURE__*/ _withScopeId(() => /*#__PURE__*/_createElementVNode("div", null, "hello", ${genFlagText( + PatchFlags.HOISTED + )}))`, + `const _hoisted_2 = /*#__PURE__*/ _withScopeId(() => /*#__PURE__*/_createElementVNode("div", null, "world", ${genFlagText( + PatchFlags.HOISTED + )}))` + ].forEach(c => expect(code).toMatch(c)) + expect(code).toMatchSnapshot() + }) }) diff --git a/packages/compiler-core/src/codegen.ts b/packages/compiler-core/src/codegen.ts index 4447e67aafa..d7abaff9ebe 100644 --- a/packages/compiler-core/src/codegen.ts +++ b/packages/compiler-core/src/codegen.ts @@ -533,8 +533,9 @@ function genHoists(hoists: (JSChildNode | null)[], context: CodegenContext) { // generate inlined withScopeId helper if (genScopeId) { + const functionDeclaration = context.isTS ? '(n: () => any)' : 'n' push( - `const _withScopeId = n => (${helper( + `const _withScopeId = ${functionDeclaration} => (${helper( PUSH_SCOPE_ID, )}("${scopeId}"),n=n(),${helper(POP_SCOPE_ID)}(),n)`, ) From 85e514b49b844fa817601fe91520bfb61b749238 Mon Sep 17 00:00:00 2001 From: "autofix-ci[bot]" <114827586+autofix-ci[bot]@users.noreply.github.com> Date: Sun, 4 Feb 2024 08:50:30 +0000 Subject: [PATCH 2/5] [autofix.ci] apply automated fixes --- packages/compiler-core/__tests__/scopeId.spec.ts | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/packages/compiler-core/__tests__/scopeId.spec.ts b/packages/compiler-core/__tests__/scopeId.spec.ts index b73381b9b7d..19cec048d87 100644 --- a/packages/compiler-core/__tests__/scopeId.spec.ts +++ b/packages/compiler-core/__tests__/scopeId.spec.ts @@ -89,8 +89,8 @@ describe('scopeId compiler support', () => { mode: 'module', scopeId: 'test', hoistStatic: true, - isTS: true - } + isTS: true, + }, ) expect(ast.helpers).toContain(PUSH_SCOPE_ID) expect(ast.helpers).toContain(POP_SCOPE_ID) @@ -98,11 +98,11 @@ describe('scopeId compiler support', () => { ;[ `const _withScopeId = (n: () => any) => (_pushScopeId("test"),n=n(),_popScopeId(),n)`, `const _hoisted_1 = /*#__PURE__*/ _withScopeId(() => /*#__PURE__*/_createElementVNode("div", null, "hello", ${genFlagText( - PatchFlags.HOISTED + PatchFlags.HOISTED, )}))`, `const _hoisted_2 = /*#__PURE__*/ _withScopeId(() => /*#__PURE__*/_createElementVNode("div", null, "world", ${genFlagText( - PatchFlags.HOISTED - )}))` + PatchFlags.HOISTED, + )}))`, ].forEach(c => expect(code).toMatch(c)) expect(code).toMatchSnapshot() }) From 3e91faf5885472ad7712849e5ca1833c302aa95a Mon Sep 17 00:00:00 2001 From: Haoqun Jiang Date: Thu, 30 May 2024 17:28:34 +0800 Subject: [PATCH 3/5] Update packages/compiler-core/src/codegen.ts --- packages/compiler-core/src/codegen.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/compiler-core/src/codegen.ts b/packages/compiler-core/src/codegen.ts index d7abaff9ebe..fdc21f3ccc8 100644 --- a/packages/compiler-core/src/codegen.ts +++ b/packages/compiler-core/src/codegen.ts @@ -533,7 +533,7 @@ function genHoists(hoists: (JSChildNode | null)[], context: CodegenContext) { // generate inlined withScopeId helper if (genScopeId) { - const functionDeclaration = context.isTS ? '(n: () => any)' : 'n' + const functionDeclaration = context.isTS ? '(n: any)' : 'n' push( `const _withScopeId = ${functionDeclaration} => (${helper( PUSH_SCOPE_ID, From f03637046bee20efaf75f59767ffe36101beafc2 Mon Sep 17 00:00:00 2001 From: Haoqun Jiang Date: Thu, 30 May 2024 17:40:43 +0800 Subject: [PATCH 4/5] test: update snapshot --- .../__tests__/__snapshots__/scopeId.spec.ts.snap | 10 +++++----- packages/compiler-core/__tests__/scopeId.spec.ts | 2 +- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/packages/compiler-core/__tests__/__snapshots__/scopeId.spec.ts.snap b/packages/compiler-core/__tests__/__snapshots__/scopeId.spec.ts.snap index ea1910d39f2..7267ba6b041 100644 --- a/packages/compiler-core/__tests__/__snapshots__/scopeId.spec.ts.snap +++ b/packages/compiler-core/__tests__/__snapshots__/scopeId.spec.ts.snap @@ -17,14 +17,14 @@ export function render(_ctx, _cache) { `; exports[`scopeId compiler support > should push typescript-compatible scopeId for hoisted nodes 1`] = ` -"import { createElementVNode as _createElementVNode, toDisplayString as _toDisplayString, createTextVNode as _createTextVNode, openBlock as _openBlock, createElementBlock as _createElementBlock, pushScopeId as _pushScopeId, popScopeId as _popScopeId } from \\"vue\\" +"import { createElementVNode as _createElementVNode, toDisplayString as _toDisplayString, createTextVNode as _createTextVNode, openBlock as _openBlock, createElementBlock as _createElementBlock, pushScopeId as _pushScopeId, popScopeId as _popScopeId } from "vue" -const _withScopeId = (n: () => any) => (_pushScopeId(\\"test\\"),n=n(),_popScopeId(),n) -const _hoisted_1 = /*#__PURE__*/ _withScopeId(() => /*#__PURE__*/_createElementVNode(\\"div\\", null, \\"hello\\", -1 /* HOISTED */)) -const _hoisted_2 = /*#__PURE__*/ _withScopeId(() => /*#__PURE__*/_createElementVNode(\\"div\\", null, \\"world\\", -1 /* HOISTED */)) +const _withScopeId = (n: any) => (_pushScopeId("test"),n=n(),_popScopeId(),n) +const _hoisted_1 = /*#__PURE__*/ _withScopeId(() => /*#__PURE__*/_createElementVNode("div", null, "hello", -1 /* HOISTED */)) +const _hoisted_2 = /*#__PURE__*/ _withScopeId(() => /*#__PURE__*/_createElementVNode("div", null, "world", -1 /* HOISTED */)) export function render(_ctx: any,_cache: any) { - return (_openBlock(), _createElementBlock(\\"div\\", null, [ + return (_openBlock(), _createElementBlock("div", null, [ _hoisted_1, _createTextVNode(_toDisplayString(_ctx.foo), 1 /* TEXT */), _hoisted_2 diff --git a/packages/compiler-core/__tests__/scopeId.spec.ts b/packages/compiler-core/__tests__/scopeId.spec.ts index 19cec048d87..21e7327f2eb 100644 --- a/packages/compiler-core/__tests__/scopeId.spec.ts +++ b/packages/compiler-core/__tests__/scopeId.spec.ts @@ -96,7 +96,7 @@ describe('scopeId compiler support', () => { expect(ast.helpers).toContain(POP_SCOPE_ID) expect(ast.hoists.length).toBe(2) ;[ - `const _withScopeId = (n: () => any) => (_pushScopeId("test"),n=n(),_popScopeId(),n)`, + `const _withScopeId = (n: any) => (_pushScopeId("test"),n=n(),_popScopeId(),n)`, `const _hoisted_1 = /*#__PURE__*/ _withScopeId(() => /*#__PURE__*/_createElementVNode("div", null, "hello", ${genFlagText( PatchFlags.HOISTED, )}))`, From 4980517e2543378730044b8ee6b89b22779d646d Mon Sep 17 00:00:00 2001 From: Haoqun Jiang Date: Thu, 30 May 2024 17:53:10 +0800 Subject: [PATCH 5/5] chore: functionDeclaration -> param --- packages/compiler-core/src/codegen.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/compiler-core/src/codegen.ts b/packages/compiler-core/src/codegen.ts index aa0438934f6..39170bac5a0 100644 --- a/packages/compiler-core/src/codegen.ts +++ b/packages/compiler-core/src/codegen.ts @@ -572,9 +572,9 @@ function genHoists(hoists: (JSChildNode | null)[], context: CodegenContext) { // generate inlined withScopeId helper if (genScopeId) { - const functionDeclaration = context.isTS ? '(n: any)' : 'n' + const param = context.isTS ? '(n: any)' : 'n' push( - `const _withScopeId = ${functionDeclaration} => (${helper( + `const _withScopeId = ${param} => (${helper( PUSH_SCOPE_ID, )}("${scopeId}"),n=n(),${helper(POP_SCOPE_ID)}(),n)`, )