Skip to content

Commit 5d25850

Browse files
xesxenautofix-ci[bot]haoqunjiang
authoredMay 30, 2024··
fix(compiler-core): emit TS-compatible function declaration when requested (#9363)
Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com> Co-authored-by: Haoqun Jiang <haoqunjiang@gmail.com>
1 parent 3ea9644 commit 5d25850

File tree

3 files changed

+43
-1
lines changed

3 files changed

+43
-1
lines changed
 

‎packages/compiler-core/__tests__/__snapshots__/scopeId.spec.ts.snap

+16
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,22 @@ export function render(_ctx, _cache) {
1616
}"
1717
`;
1818

19+
exports[`scopeId compiler support > should push typescript-compatible scopeId for hoisted nodes 1`] = `
20+
"import { createElementVNode as _createElementVNode, toDisplayString as _toDisplayString, createTextVNode as _createTextVNode, openBlock as _openBlock, createElementBlock as _createElementBlock, pushScopeId as _pushScopeId, popScopeId as _popScopeId } from "vue"
21+
22+
const _withScopeId = (n: any) => (_pushScopeId("test"),n=n(),_popScopeId(),n)
23+
const _hoisted_1 = /*#__PURE__*/ _withScopeId(() => /*#__PURE__*/_createElementVNode("div", null, "hello", -1 /* HOISTED */))
24+
const _hoisted_2 = /*#__PURE__*/ _withScopeId(() => /*#__PURE__*/_createElementVNode("div", null, "world", -1 /* HOISTED */))
25+
26+
export function render(_ctx: any,_cache: any) {
27+
return (_openBlock(), _createElementBlock("div", null, [
28+
_hoisted_1,
29+
_createTextVNode(_toDisplayString(_ctx.foo), 1 /* TEXT */),
30+
_hoisted_2
31+
]))
32+
}"
33+
`;
34+
1935
exports[`scopeId compiler support > should wrap default slot 1`] = `
2036
"import { createElementVNode as _createElementVNode, resolveComponent as _resolveComponent, withCtx as _withCtx, openBlock as _openBlock, createBlock as _createBlock } from "vue"
2137

‎packages/compiler-core/__tests__/scopeId.spec.ts

+25
Original file line numberDiff line numberDiff line change
@@ -81,4 +81,29 @@ describe('scopeId compiler support', () => {
8181
].forEach(c => expect(code).toMatch(c))
8282
expect(code).toMatchSnapshot()
8383
})
84+
85+
test('should push typescript-compatible scopeId for hoisted nodes', () => {
86+
const { ast, code } = baseCompile(
87+
`<div><div>hello</div>{{ foo }}<div>world</div></div>`,
88+
{
89+
mode: 'module',
90+
scopeId: 'test',
91+
hoistStatic: true,
92+
isTS: true,
93+
},
94+
)
95+
expect(ast.helpers).toContain(PUSH_SCOPE_ID)
96+
expect(ast.helpers).toContain(POP_SCOPE_ID)
97+
expect(ast.hoists.length).toBe(2)
98+
;[
99+
`const _withScopeId = (n: any) => (_pushScopeId("test"),n=n(),_popScopeId(),n)`,
100+
`const _hoisted_1 = /*#__PURE__*/ _withScopeId(() => /*#__PURE__*/_createElementVNode("div", null, "hello", ${genFlagText(
101+
PatchFlags.HOISTED,
102+
)}))`,
103+
`const _hoisted_2 = /*#__PURE__*/ _withScopeId(() => /*#__PURE__*/_createElementVNode("div", null, "world", ${genFlagText(
104+
PatchFlags.HOISTED,
105+
)}))`,
106+
].forEach(c => expect(code).toMatch(c))
107+
expect(code).toMatchSnapshot()
108+
})
84109
})

‎packages/compiler-core/src/codegen.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -572,8 +572,9 @@ function genHoists(hoists: (JSChildNode | null)[], context: CodegenContext) {
572572

573573
// generate inlined withScopeId helper
574574
if (genScopeId) {
575+
const param = context.isTS ? '(n: any)' : 'n'
575576
push(
576-
`const _withScopeId = n => (${helper(
577+
`const _withScopeId = ${param} => (${helper(
577578
PUSH_SCOPE_ID,
578579
)}("${scopeId}"),n=n(),${helper(POP_SCOPE_ID)}(),n)`,
579580
)

0 commit comments

Comments
 (0)
Please sign in to comment.