Skip to content

Commit

Permalink
Share implementation of function-like visitors
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewiggins committed Jun 22, 2023
1 parent be03ccf commit ff17284
Showing 1 changed file with 30 additions and 54 deletions.
84 changes: 30 additions & 54 deletions packages/react-transform/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,8 @@ function shouldTransform(
path: NodePath<FunctionLike>,
options: PluginOptions
): boolean {
if (getData(path, alreadyTransformed) === true) return false;

// Opt-out takes first precedence
if (isOptedOutOfSignalTracking(path)) return false;
// Opt-in opts in to transformation regardless of mode
Expand Down Expand Up @@ -211,72 +213,24 @@ export default function signalsTransform(
// seeing a function would probably be faster than running an entire
// babel pass with plugins on components twice.
exit(path, state) {
if (
getData(path, alreadyTransformed) !== true &&
shouldTransform(path, options)
) {
let newFunction: BabelTypes.ArrowFunctionExpression;
if (options.experimental?.noTryFinally) {
newFunction = prependUseSignals(t, path, state);
} else {
newFunction = wrapInTryFinally(t, path, state);
}

// Using replaceWith keeps the existing leading comments already so
// we'll clear our cloned node's leading comments to ensure they
// aren't duplicated in the output.
newFunction.leadingComments = [];

setData(path, alreadyTransformed, true);
path.replaceWith(newFunction);
if (shouldTransform(path, options)) {
transformFunction(t, options, path, state);
}
},
},

FunctionExpression: {
exit(path, state) {
if (
getData(path, alreadyTransformed) !== true &&
shouldTransform(path, options)
) {
let newFunction: BabelTypes.FunctionExpression;
if (options.experimental?.noTryFinally) {
newFunction = prependUseSignals(t, path, state);
} else {
newFunction = wrapInTryFinally(t, path, state);
}

// Using replaceWith keeps the existing leading comments already so
// we'll clear our cloned node's leading comments to ensure they
// aren't duplicated in the output.
newFunction.leadingComments = [];

setData(path, alreadyTransformed, true);
path.replaceWith(newFunction);
if (shouldTransform(path, options)) {
transformFunction(t, options, path, state);
}
},
},

FunctionDeclaration: {
exit(path, state) {
if (
getData(path, alreadyTransformed) !== true &&
shouldTransform(path, options)
) {
let newFunction: BabelTypes.FunctionDeclaration;
if (options.experimental?.noTryFinally) {
newFunction = prependUseSignals(t, path, state);
} else {
newFunction = wrapInTryFinally(t, path, state);
}

// Using replaceWith keeps the existing leading comments already so
// we'll clear our cloned node's leading comments to ensure they
// aren't duplicated in the output.
newFunction.leadingComments = [];

setData(path, alreadyTransformed, true);
path.replaceWith(newFunction);
if (shouldTransform(path, options)) {
transformFunction(t, options, path, state);
}
},
},
Expand All @@ -298,6 +252,28 @@ export default function signalsTransform(
};
}

function transformFunction<T extends FunctionLike>(
t: typeof BabelTypes,
options: PluginOptions,
path: NodePath<T>,
state: PluginPass
) {
let newFunction: T;
if (options.experimental?.noTryFinally) {
newFunction = prependUseSignals(t, path, state);
} else {
newFunction = wrapInTryFinally(t, path, state);
}

// Using replaceWith keeps the existing leading comments already so
// we'll clear our cloned node's leading comments to ensure they
// aren't duplicated in the output.
newFunction.leadingComments = [];

setData(path, alreadyTransformed, true);
path.replaceWith(newFunction);
}

function wrapInTryFinally<T extends FunctionLike>(
t: typeof BabelTypes,
path: NodePath<T>,
Expand Down

0 comments on commit ff17284

Please sign in to comment.