Skip to content

Commit

Permalink
Simplify CTF traverse
Browse files Browse the repository at this point in the history
  • Loading branch information
lxsmnsyc committed Oct 25, 2024
1 parent b2e52b2 commit 9880ae2
Show file tree
Hide file tree
Showing 2 changed files with 277 additions and 269 deletions.
28 changes: 17 additions & 11 deletions packages/core/babel/transform-comment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,19 +155,19 @@ const COMMENT_TRAVERSE: babel.Visitor<State> = {
}
}
},
BlockStatement(p, state) {
if (!t.isBlockStatement(p.parent)) {
BlockStatement(path, state) {
if (!t.isBlockStatement(path.parent)) {
return;
}
const comments = p.node.leadingComments;
const comments = path.node.leadingComments;
if (comments) {
const [preference, nameOption] = getCallbackLabelPreference(
state,
comments,
);
if (preference) {
const [name, source, named] = CALLBACK_LABEL[preference];
const callback = t.arrowFunctionExpression([], p.node);
const callback = t.arrowFunctionExpression([], path.node);
const args: t.Expression[] = [callback];
if (named && nameOption) {
args.push(
Expand All @@ -180,22 +180,25 @@ const COMMENT_TRAVERSE: babel.Visitor<State> = {
]),
);
}
p.replaceWith(
t.callExpression(getImportIdentifier(state, p, name, source), args),
path.replaceWith(
t.callExpression(
getImportIdentifier(state, path, name, source),
args,
),
);
}
}
},
ExpressionStatement(p, state) {
const comments = p.node.leadingComments;
ExpressionStatement(path, state) {
const comments = path.node.leadingComments;
if (comments) {
const [preference, nameOption] = getCallbackLabelPreference(
state,
comments,
);
if (preference) {
const [name, source, named] = CALLBACK_LABEL[preference];
const callback = t.arrowFunctionExpression([], p.node.expression);
const callback = t.arrowFunctionExpression([], path.node.expression);
const args: t.Expression[] = [callback];
if (named && nameOption) {
args.push(
Expand All @@ -208,8 +211,11 @@ const COMMENT_TRAVERSE: babel.Visitor<State> = {
]),
);
}
p.replaceWith(
t.callExpression(getImportIdentifier(state, p, name, source), args),
path.replaceWith(
t.callExpression(
getImportIdentifier(state, path, name, source),
args,
),
);
}
}
Expand Down
Loading

0 comments on commit 9880ae2

Please sign in to comment.