Skip to content

Commit 08995e9

Browse files
committed
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 06affd3 commit 08995e9

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

+1-1
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

+24
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)