Skip to content

fix(compiler-core): emit TS-compatible function declaration when requested #9363

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
May 30, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down
25 changes: 25 additions & 0 deletions packages/compiler-core/__tests__/scopeId.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,4 +81,29 @@
].forEach(c => expect(code).toMatch(c))
expect(code).toMatchSnapshot()
})

test('should push typescript-compatible scopeId for hoisted nodes', () => {
const { ast, code } = baseCompile(
`<div><div>hello</div>{{ foo }}<div>world</div></div>`,
{
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))

Check failure on line 106 in packages/compiler-core/__tests__/scopeId.spec.ts

View workflow job for this annotation

GitHub Actions / unit-test-windows

packages/compiler-core/__tests__/scopeId.spec.ts > scopeId compiler support > should push typescript-compatible scopeId for hoisted nodes

AssertionError: expected 'import { createElementVNode as _creat…' to match 'const _withScopeId = (n: () => any) =…' - Expected + Received - const _withScopeId = (n: () => any) => (_pushScopeId("test"),n=n(),_popScopeId(),n) + 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 + ])) + } ❯ packages/compiler-core/__tests__/scopeId.spec.ts:106:33 ❯ packages/compiler-core/__tests__/scopeId.spec.ts:106:7

Check failure on line 106 in packages/compiler-core/__tests__/scopeId.spec.ts

View workflow job for this annotation

GitHub Actions / unit-test

packages/compiler-core/__tests__/scopeId.spec.ts > scopeId compiler support > should push typescript-compatible scopeId for hoisted nodes

AssertionError: expected 'import { createElementVNode as _creat…' to match 'const _withScopeId = (n: () => any) =…' - Expected + Received - const _withScopeId = (n: () => any) => (_pushScopeId("test"),n=n(),_popScopeId(),n) + 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 + ])) + } ❯ packages/compiler-core/__tests__/scopeId.spec.ts:106:33 ❯ packages/compiler-core/__tests__/scopeId.spec.ts:106:7
expect(code).toMatchSnapshot()
})
})
3 changes: 2 additions & 1 deletion packages/compiler-core/src/codegen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -572,8 +572,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)`,
)
Expand Down
Loading