From 829e0030b03972c1bab6fa6167d89c9ed5fcf7e7 Mon Sep 17 00:00:00 2001 From: Robby Morgan Date: Thu, 12 Jun 2025 21:00:40 -0500 Subject: [PATCH 1/3] Expand debug statements in place to permit subsequent Babel plugins (e.g. from @embroider/macros) to perform further translation --- src/utils/builder.ts | 22 +++++++--------------- src/utils/macros.ts | 2 -- 2 files changed, 7 insertions(+), 17 deletions(-) diff --git a/src/utils/builder.ts b/src/utils/builder.ts index 45050d3..b83e56f 100644 --- a/src/utils/builder.ts +++ b/src/utils/builder.ts @@ -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, @@ -149,10 +147,10 @@ export default class Builder { prefixedIdentifiers.push(negatedPredicate); } - this.expressions.push([ + this.expandMacro( path, this._buildLogicalExpressions(prefixedIdentifiers, callExpression), - ]); + ); } /** @@ -211,18 +209,12 @@ export default class Builder { } /** - * Performs the actually expansion of macros + * Performs the actually expansion of macro */ - 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(); - } + expandMacro(exp: CallStatementPath, logicalExp: (debugIdentifier: t.Expression) => t.Expression) { + const flag = this._debugExpression(exp); + exp.replaceWith(this.t.parenthesizedExpression(logicalExp(flag))); + exp.scope.crawl(); } _debugExpression(target: NodePath) { diff --git a/src/utils/macros.ts b/src/utils/macros.ts index 986952b..9311fd7 100644 --- a/src/utils/macros.ts +++ b/src/utils/macros.ts @@ -31,8 +31,6 @@ export default class Macros { * adds the debug binding if missing from the env-flags module. */ expand() { - this.builder.expandMacros(); - this._cleanImports(); } From af4504956bda94818bf66231d325277bf3aa6240 Mon Sep 17 00:00:00 2001 From: Robby Morgan Date: Thu, 12 Jun 2025 21:17:21 -0500 Subject: [PATCH 2/3] Eliminate indirection to simplify code --- src/utils/builder.ts | 48 ++++++++++++++++++-------------------------- 1 file changed, 19 insertions(+), 29 deletions(-) diff --git a/src/utils/builder.ts b/src/utils/builder.ts index b83e56f..ffc85cf 100644 --- a/src/utils/builder.ts +++ b/src/utils/builder.ts @@ -147,10 +147,10 @@ export default class Builder { prefixedIdentifiers.push(negatedPredicate); } - this.expandMacro( - path, - this._buildLogicalExpressions(prefixedIdentifiers, callExpression), - ); + // Expand the macro! + let replacementPath = this._buildLogicalExpressions(prefixedIdentifiers, callExpression, this._debugExpression(path)); + path.replaceWith(replacementPath); + path.scope.crawl(); } /** @@ -208,15 +208,6 @@ export default class Builder { }); } - /** - * Performs the actually expansion of macro - */ - expandMacro(exp: CallStatementPath, logicalExp: (debugIdentifier: t.Expression) => t.Expression) { - const flag = this._debugExpression(exp); - exp.replaceWith(this.t.parenthesizedExpression(logicalExp(flag))); - exp.scope.crawl(); - } - _debugExpression(target: NodePath) { if (typeof this.isDebug === 'boolean') { return this.t.booleanLiteral(this.isDebug); @@ -244,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!); } } From 6c54b34503932c86b52e1435c04b8c721a1bb279 Mon Sep 17 00:00:00 2001 From: Robby Morgan Date: Thu, 12 Jun 2025 21:54:35 -0500 Subject: [PATCH 3/3] Rename and document current purpose of macro functions --- src/index.ts | 2 +- src/utils/macros.ts | 15 +++++---------- 2 files changed, 6 insertions(+), 11 deletions(-) diff --git a/src/index.ts b/src/index.ts index efcc30e..642693a 100644 --- a/src/index.ts +++ b/src/index.ts @@ -100,7 +100,7 @@ export default function macros(babel: typeof Babel): Babel.PluginObj { }, exit() { - this.macroBuilder.expand(); + this.macroBuilder.cleanImports(); }, }, diff --git a/src/utils/macros.ts b/src/utils/macros.ts index 9311fd7..44c7922 100644 --- a/src/utils/macros.ts +++ b/src/utils/macros.ts @@ -26,14 +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._cleanImports(); - } - /** * Collects the import bindings for the debug tools. */ @@ -53,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) { if (!isCallStatementPath(path)) { @@ -68,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) =>