Skip to content

Commit 8f9e242

Browse files
filipesilvahansl
authored andcommitted
fix(@angular-devkit/build-optimizer): don't assume enum values
Enum values were assumed to start with 0 and be digits. But enum values can be anything really. When a enum declaration was not identified as an enum in a side effect free package, it would be identified as a top level IIFE (in another transform) and assumed pure. This drops the enum. This PR fixes the behaviour by allowing the enum to be initialized with anything that doesn't have whitespaces. This is still limited and might lead to other false negatives that break things. We should find a better way of identifying enums. /cc @clydin Fix angular/angular#23400
1 parent 6289c25 commit 8f9e242

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

packages/angular_devkit/build_optimizer/src/transforms/wrap-enums.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export function testWrapEnums(content: string) {
1313
const regexes = [
1414
// tslint:disable:max-line-length
1515
/var (\S+) = \{\};\r?\n(\1\.(\S+) = \d+;\r?\n)+\1\[\1\.(\S+)\] = "\4";\r?\n(\1\[\1\.(\S+)\] = "\S+";\r?\n*)+/,
16-
/var (\S+);(\/\*@__PURE__\*\/)*\r?\n\(function \(\1\) \{\s+(\1\[\1\["(\S+)"\] = 0\] = "\4";(\s+\1\[\1\["\S+"\] = \d\] = "\S+";)*\r?\n)\}\)\(\1 \|\| \(\1 = \{\}\)\);/,
16+
/var (\S+);(\/\*@__PURE__\*\/)*\r?\n\(function \(\1\) \{\s+(\1\[\1\["(\S+)"\] = (\S+)\] = "\4";(\s+\1\[\1\["\S+"\] = (\S+)\] = "\S+";)*\r?\n)\}\)\(\1 \|\| \(\1 = \{\}\)\);/,
1717
/\/\*\* @enum \{\w+\} \*\//,
1818
// tslint:enable:max-line-length
1919
];

packages/angular_devkit/build_optimizer/src/transforms/wrap-enums_spec.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,30 @@ describe('wrap-enums', () => {
5858
expect(tags.oneLine`${transform(input)}`).toEqual(tags.oneLine`${output}`);
5959
});
6060

61+
it('wraps ts 2.3 - 2.6 enums in IIFE, even if they have funny numbers', () => {
62+
const input = tags.stripIndent`
63+
export var AnimatorControlState;
64+
(function (AnimatorControlState) {
65+
AnimatorControlState[AnimatorControlState["INITIALIZED"] = 1] = "INITIALIZED";
66+
AnimatorControlState[AnimatorControlState["STARTED"] = 2] = "STARTED";
67+
AnimatorControlState[AnimatorControlState["FINISHED"] = 3] = "FINISHED";
68+
AnimatorControlState[AnimatorControlState["DESTROYED"] = 4] = "DESTROYED";
69+
})(AnimatorControlState || (AnimatorControlState = {}));
70+
`;
71+
const output = tags.stripIndent`
72+
export var AnimatorControlState = /*@__PURE__*/ (function (AnimatorControlState) {
73+
AnimatorControlState[AnimatorControlState["INITIALIZED"] = 1] = "INITIALIZED";
74+
AnimatorControlState[AnimatorControlState["STARTED"] = 2] = "STARTED";
75+
AnimatorControlState[AnimatorControlState["FINISHED"] = 3] = "FINISHED";
76+
AnimatorControlState[AnimatorControlState["DESTROYED"] = 4] = "DESTROYED";
77+
return AnimatorControlState;
78+
})({});
79+
`;
80+
81+
expect(testWrapEnums(input)).toBeTruthy();
82+
expect(tags.oneLine`${transform(input)}`).toEqual(tags.oneLine`${output}`);
83+
});
84+
6185
it('wraps tsickle enums in IIFE', () => {
6286
const input = tags.stripIndent`
6387
/** @enum {number} */

0 commit comments

Comments
 (0)