From 4610748e33237abb9359ecbcc2d26908c96ec130 Mon Sep 17 00:00:00 2001 From: Nolan Lawson Date: Mon, 2 Dec 2024 13:35:02 -0800 Subject: [PATCH 1/4] refactor(ssr): share node types in traversal --- .../@lwc/ssr-compiler/src/transmogrify.ts | 49 +++++++++---------- 1 file changed, 24 insertions(+), 25 deletions(-) diff --git a/packages/@lwc/ssr-compiler/src/transmogrify.ts b/packages/@lwc/ssr-compiler/src/transmogrify.ts index 952095992c..c049845970 100644 --- a/packages/@lwc/ssr-compiler/src/transmogrify.ts +++ b/packages/@lwc/ssr-compiler/src/transmogrify.ts @@ -42,32 +42,31 @@ const isWithinFn = (pattern: RegExp, nodePath: NodePath): boolean => { return false; }; -function transformFunction( - path: NodePath, - state: TransmogrificationState -): undefined { - const { node } = path; - if (!node?.async || !node?.generator) { - return; - } - - // Component authors might conceivably use async generator functions in their own code. Therefore, - // when traversing & transforming written+generated code, we need to disambiguate generated async - // generator functions from those that were written by the component author. - if ( - !isWithinFn(GEN_MARKUP_OR_GEN_SLOTTED_CONTENT_PATTERN, path) && - !isWithinFn(TMPL_FN_PATTERN, path) - ) { - return; - } - node.generator = false; - node.async = state.mode === 'async'; - node.params.unshift(EMIT_IDENT); -} - const visitors: Visitors = { - FunctionDeclaration: transformFunction, - FunctionExpression: transformFunction, + // @ts-expect-error types for `traverse` do not support sharing a visitor between node types: + // https://estree-toolkit.netlify.app/traversal#sharing-a-visitor-function-between-two-node-types + 'FunctionDeclaration|FunctionExpression': ( + path: NodePath, + state: TransmogrificationState + ) => { + const { node } = path; + if (!node?.async || !node?.generator) { + return; + } + + // Component authors might conceivably use async generator functions in their own code. Therefore, + // when traversing & transforming written+generated code, we need to disambiguate generated async + // generator functions from those that were written by the component author. + if ( + !isWithinFn(GEN_MARKUP_OR_GEN_SLOTTED_CONTENT_PATTERN, path) && + !isWithinFn(TMPL_FN_PATTERN, path) + ) { + return; + } + node.generator = false; + node.async = state.mode === 'async'; + node.params.unshift(EMIT_IDENT); + }, YieldExpression(path, state) { const { node } = path; if (!node) { From 67f2d17d12c0e46b5d0d43e927159c941e1290f8 Mon Sep 17 00:00:00 2001 From: Nolan Lawson Date: Mon, 2 Dec 2024 13:53:54 -0800 Subject: [PATCH 2/4] Update packages/@lwc/ssr-compiler/src/transmogrify.ts --- packages/@lwc/ssr-compiler/src/transmogrify.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/@lwc/ssr-compiler/src/transmogrify.ts b/packages/@lwc/ssr-compiler/src/transmogrify.ts index c049845970..35403102b8 100644 --- a/packages/@lwc/ssr-compiler/src/transmogrify.ts +++ b/packages/@lwc/ssr-compiler/src/transmogrify.ts @@ -44,7 +44,7 @@ const isWithinFn = (pattern: RegExp, nodePath: NodePath): boolean => { const visitors: Visitors = { // @ts-expect-error types for `traverse` do not support sharing a visitor between node types: - // https://estree-toolkit.netlify.app/traversal#sharing-a-visitor-function-between-two-node-types + // https://github.com/sarsamurmu/estree-toolkit/issues/20 'FunctionDeclaration|FunctionExpression': ( path: NodePath, state: TransmogrificationState From 829905bc0d368a52bebd06bc4b793b61d9df6866 Mon Sep 17 00:00:00 2001 From: Will Harney <62956339+wjhsf@users.noreply.github.com> Date: Tue, 3 Dec 2024 12:25:18 -0500 Subject: [PATCH 3/4] Update packages/@lwc/ssr-compiler/src/transmogrify.ts --- packages/@lwc/ssr-compiler/src/transmogrify.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/@lwc/ssr-compiler/src/transmogrify.ts b/packages/@lwc/ssr-compiler/src/transmogrify.ts index 35403102b8..7c957f4473 100644 --- a/packages/@lwc/ssr-compiler/src/transmogrify.ts +++ b/packages/@lwc/ssr-compiler/src/transmogrify.ts @@ -45,10 +45,10 @@ const isWithinFn = (pattern: RegExp, nodePath: NodePath): boolean => { const visitors: Visitors = { // @ts-expect-error types for `traverse` do not support sharing a visitor between node types: // https://github.com/sarsamurmu/estree-toolkit/issues/20 - 'FunctionDeclaration|FunctionExpression': ( + 'FunctionDeclaration|FunctionExpression' ( path: NodePath, state: TransmogrificationState - ) => { + ) { const { node } = path; if (!node?.async || !node?.generator) { return; From 7f54955cac0cf0a8a8acd7a882beb5d4c47ad5b9 Mon Sep 17 00:00:00 2001 From: Will Harney <62956339+wjhsf@users.noreply.github.com> Date: Tue, 3 Dec 2024 12:26:11 -0500 Subject: [PATCH 4/4] Update packages/@lwc/ssr-compiler/src/transmogrify.ts --- packages/@lwc/ssr-compiler/src/transmogrify.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/@lwc/ssr-compiler/src/transmogrify.ts b/packages/@lwc/ssr-compiler/src/transmogrify.ts index 7c957f4473..6a3a89a579 100644 --- a/packages/@lwc/ssr-compiler/src/transmogrify.ts +++ b/packages/@lwc/ssr-compiler/src/transmogrify.ts @@ -45,7 +45,7 @@ const isWithinFn = (pattern: RegExp, nodePath: NodePath): boolean => { const visitors: Visitors = { // @ts-expect-error types for `traverse` do not support sharing a visitor between node types: // https://github.com/sarsamurmu/estree-toolkit/issues/20 - 'FunctionDeclaration|FunctionExpression' ( + 'FunctionDeclaration|FunctionExpression'( path: NodePath, state: TransmogrificationState ) {