Skip to content

Commit

Permalink
refactor(ssr): share node types in traversal
Browse files Browse the repository at this point in the history
  • Loading branch information
nolanlawson committed Dec 2, 2024
1 parent 5bd5ae0 commit 4610748
Showing 1 changed file with 24 additions and 25 deletions.
49 changes: 24 additions & 25 deletions packages/@lwc/ssr-compiler/src/transmogrify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,32 +42,31 @@ const isWithinFn = (pattern: RegExp, nodePath: NodePath): boolean => {
return false;
};

function transformFunction(
path: NodePath<FunctionDeclaration | FunctionExpression, EstreeToolkitNode>,
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<FunctionDeclaration | FunctionExpression, EstreeToolkitNode>,
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) {
Expand Down

0 comments on commit 4610748

Please sign in to comment.