Skip to content
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

refactor(ssr): share node types in traversal #4987

Merged
merged 4 commits into from
Dec 3, 2024
Merged
Changes from 2 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
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://github.com/sarsamurmu/estree-toolkit/issues/20
'FunctionDeclaration|FunctionExpression': (
path: NodePath<FunctionDeclaration | FunctionExpression, EstreeToolkitNode>,
state: TransmogrificationState
) => {
wjhsf marked this conversation as resolved.
Show resolved Hide resolved
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