Skip to content

Commit c75c201

Browse files
authored
Merge pull request #103 from robbytx/expand-macros-in-place
Expand debug statements in place to permit subsequent Babel plugin translation
2 parents dde2d5b + 6c54b34 commit c75c201

File tree

3 files changed

+25
-50
lines changed

3 files changed

+25
-50
lines changed

src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ export default function macros(babel: typeof Babel): Babel.PluginObj<State> {
100100
},
101101

102102
exit() {
103-
this.macroBuilder.expand();
103+
this.macroBuilder.cleanImports();
104104
},
105105
},
106106

src/utils/builder.ts

Lines changed: 19 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,6 @@ export default class Builder {
3030
private isDebug: boolean | '@embroider/macros';
3131
private util: ImportUtil;
3232

33-
private expressions: [CallStatementPath, (debugIdentifier: t.Expression) => t.Expression][] = [];
34-
3533
constructor(
3634
readonly t: typeof Babel.types,
3735
util: ImportUtil,
@@ -149,10 +147,10 @@ export default class Builder {
149147
prefixedIdentifiers.push(negatedPredicate);
150148
}
151149

152-
this.expressions.push([
153-
path,
154-
this._buildLogicalExpressions(prefixedIdentifiers, callExpression),
155-
]);
150+
// Expand the macro!
151+
let replacementPath = this._buildLogicalExpressions(prefixedIdentifiers, callExpression, this._debugExpression(path));
152+
path.replaceWith(replacementPath);
153+
path.scope.crawl();
156154
}
157155

158156
/**
@@ -210,21 +208,6 @@ export default class Builder {
210208
});
211209
}
212210

213-
/**
214-
* Performs the actually expansion of macros
215-
*/
216-
expandMacros() {
217-
let t = this.t;
218-
for (let i = 0; i < this.expressions.length; i++) {
219-
let expression = this.expressions[i];
220-
let exp = expression[0];
221-
let flag = this._debugExpression(exp);
222-
let logicalExp = expression[1];
223-
exp.replaceWith(t.parenthesizedExpression(logicalExp(flag)));
224-
exp.scope.crawl();
225-
}
226-
}
227-
228211
_debugExpression(target: NodePath) {
229212
if (typeof this.isDebug === 'boolean') {
230213
return this.t.booleanLiteral(this.isDebug);
@@ -252,26 +235,25 @@ export default class Builder {
252235

253236
_buildLogicalExpressions(
254237
identifiers: t.Expression[],
255-
callExpression: t.Expression
256-
): (debugIdentifier: t.Expression) => t.Expression {
238+
callExpression: t.Expression,
239+
debugIdentifier: t.Expression
240+
): t.Expression {
257241
let t = this.t;
258242

259-
return (debugIdentifier: t.Expression) => {
260-
identifiers.unshift(debugIdentifier);
261-
identifiers.push(callExpression);
262-
let logicalExpressions;
243+
identifiers.unshift(debugIdentifier);
244+
identifiers.push(callExpression);
245+
let logicalExpressions;
263246

264-
for (let i = 0; i < identifiers.length; i++) {
265-
let left = identifiers[i];
266-
let right = identifiers[i + 1];
267-
if (!logicalExpressions) {
268-
logicalExpressions = t.logicalExpression('&&', left, right);
269-
} else if (right) {
270-
logicalExpressions = t.logicalExpression('&&', logicalExpressions, right);
271-
}
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);
272254
}
255+
}
273256

274-
return logicalExpressions!;
275-
};
257+
return t.parenthesizedExpression(logicalExpressions!);
276258
}
277259
}

src/utils/macros.ts

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,6 @@ export default class Macros {
2626
});
2727
}
2828

29-
/**
30-
* Injects the either the env-flags module with the debug binding or
31-
* adds the debug binding if missing from the env-flags module.
32-
*/
33-
expand() {
34-
this.builder.expandMacros();
35-
36-
this._cleanImports();
37-
}
38-
3929
/**
4030
* Collects the import bindings for the debug tools.
4131
*/
@@ -55,7 +45,7 @@ export default class Macros {
5545
}
5646

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

73-
_cleanImports() {
63+
/**
64+
* Removes obsolete import bindings for the debug tools.
65+
*/
66+
cleanImports() {
7467
if (!this.debugHelpers?.module) {
7568
if (this.localDebugBindings.length > 0) {
7669
let importPath = this.localDebugBindings[0].findParent((p) =>

0 commit comments

Comments
 (0)