diff --git a/packages/@lwc/ssr-compiler/src/transmogrify.ts b/packages/@lwc/ssr-compiler/src/transmogrify.ts index 952095992c..6a3a89a579 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) +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'( + path: NodePath, + state: TransmogrificationState ) { - return; - } - node.generator = false; - node.async = state.mode === 'async'; - node.params.unshift(EMIT_IDENT); -} + const { node } = path; + if (!node?.async || !node?.generator) { + return; + } -const visitors: Visitors = { - FunctionDeclaration: transformFunction, - FunctionExpression: transformFunction, + // 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) {