Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ export default function macros(babel: typeof Babel): Babel.PluginObj<State> {
},

exit() {
this.macroBuilder.expand();
this.macroBuilder.cleanImports();
},
},

Expand Down
56 changes: 19 additions & 37 deletions src/utils/builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@ export default class Builder {
private isDebug: boolean | '@embroider/macros';
private util: ImportUtil;

private expressions: [CallStatementPath, (debugIdentifier: t.Expression) => t.Expression][] = [];

constructor(
readonly t: typeof Babel.types,
util: ImportUtil,
Expand Down Expand Up @@ -149,10 +147,10 @@ export default class Builder {
prefixedIdentifiers.push(negatedPredicate);
}

this.expressions.push([
path,
this._buildLogicalExpressions(prefixedIdentifiers, callExpression),
]);
// Expand the macro!
let replacementPath = this._buildLogicalExpressions(prefixedIdentifiers, callExpression, this._debugExpression(path));
path.replaceWith(replacementPath);
path.scope.crawl();
}

/**
Expand Down Expand Up @@ -210,21 +208,6 @@ export default class Builder {
});
}

/**
* Performs the actually expansion of macros
*/
expandMacros() {
let t = this.t;
for (let i = 0; i < this.expressions.length; i++) {
let expression = this.expressions[i];
let exp = expression[0];
let flag = this._debugExpression(exp);
let logicalExp = expression[1];
exp.replaceWith(t.parenthesizedExpression(logicalExp(flag)));
exp.scope.crawl();
}
}

_debugExpression(target: NodePath) {
if (typeof this.isDebug === 'boolean') {
return this.t.booleanLiteral(this.isDebug);
Expand Down Expand Up @@ -252,26 +235,25 @@ export default class Builder {

_buildLogicalExpressions(
identifiers: t.Expression[],
callExpression: t.Expression
): (debugIdentifier: t.Expression) => t.Expression {
callExpression: t.Expression,
debugIdentifier: t.Expression
): t.Expression {
let t = this.t;

return (debugIdentifier: t.Expression) => {
identifiers.unshift(debugIdentifier);
identifiers.push(callExpression);
let logicalExpressions;
identifiers.unshift(debugIdentifier);
identifiers.push(callExpression);
let logicalExpressions;

for (let i = 0; i < identifiers.length; i++) {
let left = identifiers[i];
let right = identifiers[i + 1];
if (!logicalExpressions) {
logicalExpressions = t.logicalExpression('&&', left, right);
} else if (right) {
logicalExpressions = t.logicalExpression('&&', logicalExpressions, right);
}
for (let i = 0; i < identifiers.length; i++) {
let left = identifiers[i];
let right = identifiers[i + 1];
if (!logicalExpressions) {
logicalExpressions = t.logicalExpression('&&', left, right);
} else if (right) {
logicalExpressions = t.logicalExpression('&&', logicalExpressions, right);
}
}

return logicalExpressions!;
};
return t.parenthesizedExpression(logicalExpressions!);
}
}
17 changes: 5 additions & 12 deletions src/utils/macros.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,6 @@ export default class Macros {
});
}

/**
* Injects the either the env-flags module with the debug binding or
* adds the debug binding if missing from the env-flags module.
*/
expand() {
this.builder.expandMacros();

this._cleanImports();
}

/**
* Collects the import bindings for the debug tools.
*/
Expand All @@ -55,7 +45,7 @@ export default class Macros {
}

/**
* Builds the expressions that the CallExpression will expand into.
* Expands the given expression, if it is simple CallExpression statement for the debug tools.
*/
build(path: NodePath<t.ExpressionStatement>) {
if (!isCallStatementPath(path)) {
Expand All @@ -70,7 +60,10 @@ export default class Macros {
}
}

_cleanImports() {
/**
* Removes obsolete import bindings for the debug tools.
*/
cleanImports() {
if (!this.debugHelpers?.module) {
if (this.localDebugBindings.length > 0) {
let importPath = this.localDebugBindings[0].findParent((p) =>
Expand Down