Skip to content

Commit af45049

Browse files
committed
Eliminate indirection to simplify code
1 parent 829e003 commit af45049

File tree

1 file changed

+19
-29
lines changed

1 file changed

+19
-29
lines changed

src/utils/builder.ts

Lines changed: 19 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -147,10 +147,10 @@ export default class Builder {
147147
prefixedIdentifiers.push(negatedPredicate);
148148
}
149149

150-
this.expandMacro(
151-
path,
152-
this._buildLogicalExpressions(prefixedIdentifiers, callExpression),
153-
);
150+
// Expand the macro!
151+
let replacementPath = this._buildLogicalExpressions(prefixedIdentifiers, callExpression, this._debugExpression(path));
152+
path.replaceWith(replacementPath);
153+
path.scope.crawl();
154154
}
155155

156156
/**
@@ -208,15 +208,6 @@ export default class Builder {
208208
});
209209
}
210210

211-
/**
212-
* Performs the actually expansion of macro
213-
*/
214-
expandMacro(exp: CallStatementPath, logicalExp: (debugIdentifier: t.Expression) => t.Expression) {
215-
const flag = this._debugExpression(exp);
216-
exp.replaceWith(this.t.parenthesizedExpression(logicalExp(flag)));
217-
exp.scope.crawl();
218-
}
219-
220211
_debugExpression(target: NodePath) {
221212
if (typeof this.isDebug === 'boolean') {
222213
return this.t.booleanLiteral(this.isDebug);
@@ -244,26 +235,25 @@ export default class Builder {
244235

245236
_buildLogicalExpressions(
246237
identifiers: t.Expression[],
247-
callExpression: t.Expression
248-
): (debugIdentifier: t.Expression) => t.Expression {
238+
callExpression: t.Expression,
239+
debugIdentifier: t.Expression
240+
): t.Expression {
249241
let t = this.t;
250242

251-
return (debugIdentifier: t.Expression) => {
252-
identifiers.unshift(debugIdentifier);
253-
identifiers.push(callExpression);
254-
let logicalExpressions;
243+
identifiers.unshift(debugIdentifier);
244+
identifiers.push(callExpression);
245+
let logicalExpressions;
255246

256-
for (let i = 0; i < identifiers.length; i++) {
257-
let left = identifiers[i];
258-
let right = identifiers[i + 1];
259-
if (!logicalExpressions) {
260-
logicalExpressions = t.logicalExpression('&&', left, right);
261-
} else if (right) {
262-
logicalExpressions = t.logicalExpression('&&', logicalExpressions, right);
263-
}
247+
for (let i = 0; i < identifiers.length; i++) {
248+
let left = identifiers[i];
249+
let right = identifiers[i + 1];
250+
if (!logicalExpressions) {
251+
logicalExpressions = t.logicalExpression('&&', left, right);
252+
} else if (right) {
253+
logicalExpressions = t.logicalExpression('&&', logicalExpressions, right);
264254
}
255+
}
265256

266-
return logicalExpressions!;
267-
};
257+
return t.parenthesizedExpression(logicalExpressions!);
268258
}
269259
}

0 commit comments

Comments
 (0)