Skip to content

Commit 821a1b5

Browse files
devversionclydin
authored andcommitted
fix(@angular-devkit/build-angular): babel adjust enum plugin incorrectly transforming loose enums
With Angular Package Format v13, we will be using a more recent version of rollup. Rollup always suffixed exports to avoid collisions, but with the most recent version, the order has changed slightly changed. e.g. previously for `@angular/core`, there were two instances of the `ViewEncapsulation` enum part of the `fesm` bundle. The second instance of the enum orginated from the compiler <--> core facade and it got renamed to avoid conflicts with the actual declaration of `ViewEncapsulation`. Now this has changed, and the first export is being renamed instead. This now breaks at runtime because the first export is being incorrectly transformed by the adjust enum plugin of `build-angular`. The plugin always had this problem of incorrectly transforming the enums, but it never surfaced because only the face enum has been transformed (which is not used at runtime). e.g. consider the following input in `core.mjs`: ``` var ViewEncapsulation$1; (function (ViewEncapsulation) { ViewEncapsulation[ViewEncapsulation["Emulated"] = 0] = "Emulated"; })(ViewEncapsulation$1 || (ViewEncapsulation$1 = {})); ``` this is transformed into: ``` var ViewEncapsulation$1 = /*#__PURE__*/(() => { ViewEncapsulation$1 = ViewEncapsulation$1 || {}; ViewEncapsulation[ViewEncapsulation["Emulated"] = 0] = "Emulated"; }()); ``` Note how the enum assignment for `Emulated` incorrectly still uses the non-suffixed identifier that previously was part of the callee wrapper function. (cherry picked from commit 0e7277c)
1 parent 258b76a commit 821a1b5

File tree

2 files changed

+36
-1
lines changed

2 files changed

+36
-1
lines changed

packages/angular_devkit/build_angular/src/babel/plugins/adjust-typescript-enums.ts

+9-1
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,17 @@ export default function (): PluginObj {
7676
return;
7777
}
7878

79+
const enumCalleeParam = enumCallee.node.params[0];
80+
const isEnumCalleeMatching =
81+
types.isIdentifier(enumCalleeParam) && enumCalleeParam.name === declarationId.name;
82+
7983
// Loose mode rewrites the enum to a shorter but less TypeScript-like form
84+
// Note: We only can apply the `loose` mode transformation if the callee parameter matches
85+
// with the declaration identifier name. This is necessary in case the the declaration id has
86+
// been renamed to avoid collisions, as the loose transform would then break the enum assignments
87+
// which rely on the differently-named callee identifier name.
8088
let enumAssignments: types.ExpressionStatement[] | undefined;
81-
if (loose) {
89+
if (loose && isEnumCalleeMatching) {
8290
enumAssignments = [];
8391
}
8492

packages/angular_devkit/build_angular/src/babel/plugins/adjust-typescript-enums_spec.ts

+27
Original file line numberDiff line numberDiff line change
@@ -240,4 +240,31 @@ describe('adjust-typescript-enums Babel plugin', () => {
240240
options: { loose: true },
241241
});
242242
});
243+
244+
it(
245+
'should not wrap TypeScript enums in loose mode if the declaration identifier has been ' +
246+
'renamed to avoid collisions',
247+
() => {
248+
testCase({
249+
input: `
250+
var ChangeDetectionStrategy$1;
251+
(function (ChangeDetectionStrategy) {
252+
ChangeDetectionStrategy[ChangeDetectionStrategy["OnPush"] = 0] = "OnPush";
253+
ChangeDetectionStrategy[ChangeDetectionStrategy["Default"] = 1] = "Default";
254+
})(ChangeDetectionStrategy$1 || (ChangeDetectionStrategy$1 = {}));
255+
`,
256+
expected: `
257+
var ChangeDetectionStrategy$1 = /*#__PURE__*/ (() => {
258+
(function (ChangeDetectionStrategy) {
259+
ChangeDetectionStrategy[(ChangeDetectionStrategy["OnPush"] = 0)] = "OnPush";
260+
ChangeDetectionStrategy[(ChangeDetectionStrategy["Default"] = 1)] = "Default";
261+
})(ChangeDetectionStrategy$1 || (ChangeDetectionStrategy$1 = {}));
262+
263+
return ChangeDetectionStrategy$1;
264+
})();
265+
`,
266+
options: { loose: true },
267+
});
268+
},
269+
);
243270
});

0 commit comments

Comments
 (0)