Skip to content
This repository has been archived by the owner on Apr 9, 2022. It is now read-only.

Commit

Permalink
fix(@angular-devkit/build-optimizer): support 2.7+ exported enums
Browse files Browse the repository at this point in the history
  • Loading branch information
clydin authored and filipesilva committed May 10, 2018
1 parent 6f41723 commit 8b5b9a1
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ describe('build-optimizer', () => {
ChangeDetectionStrategy[ChangeDetectionStrategy["OnPush"] = 0] = "OnPush";
ChangeDetectionStrategy[ChangeDetectionStrategy["Default"] = 1] = "Default";
return ChangeDetectionStrategy;
})({});
})(ChangeDetectionStrategy || (ChangeDetectionStrategy = {}));
var Clazz = /*@__PURE__*/ (function () { function Clazz() { } ${staticProperty} return Clazz; }());
var ComponentClazz = /*@__PURE__*/ (function () {
function ComponentClazz() { }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,16 +174,32 @@ function findTs2_3EnumIife(name: string, statement: ts.Statement): ts.CallExpres

const callExpression = expression;

const argument = expression.arguments[0];
if (!ts.isBinaryExpression(argument)
|| argument.operatorToken.kind !== ts.SyntaxKind.BarBarToken) {
let argument = expression.arguments[0];
if (!ts.isBinaryExpression(argument)) {
return null;
}

if (!ts.isIdentifier(argument.left) || argument.left.text !== name) {
return null;
}

if (argument.operatorToken.kind === ts.SyntaxKind.FirstAssignment) {
if (!ts.isBinaryExpression(argument.right)
|| argument.right.operatorToken.kind !== ts.SyntaxKind.BarBarToken) {
return null;
}

argument = argument.right;
}

if (!ts.isBinaryExpression(argument)) {
return null;
}

if (argument.operatorToken.kind !== ts.SyntaxKind.BarBarToken) {
return null;
}

expression = expression.expression;
while (ts.isParenthesizedExpression(expression)) {
expression = expression.expression;
Expand Down Expand Up @@ -406,7 +422,7 @@ function updateEnumIife(hostNode: ts.VariableStatement, iife: ts.CallExpression)
updatedFunction,
),
iife.typeArguments,
[ts.createObjectLiteral()],
iife.arguments,
);

return updateHostNode(hostNode, updatedIife);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ describe('wrap-enums', () => {
ChangeDetectionStrategy[ChangeDetectionStrategy["OnPush"] = 0] = "OnPush";
ChangeDetectionStrategy[ChangeDetectionStrategy["Default"] = 1] = "Default";
return ChangeDetectionStrategy;
})({});
})(ChangeDetectionStrategy || (ChangeDetectionStrategy = {}));
`;

expect(tags.oneLine`${transform(input)}`).toEqual(tags.oneLine`${output}`);
Expand All @@ -73,7 +73,7 @@ describe('wrap-enums', () => {
AnimatorControlState[AnimatorControlState["FINISHED"] = 3] = "FINISHED";
AnimatorControlState[AnimatorControlState["DESTROYED"] = 4] = "DESTROYED";
return AnimatorControlState;
})({});
})(AnimatorControlState || (AnimatorControlState = {}));
`;

expect(tags.oneLine`${transform(input)}`).toEqual(tags.oneLine`${output}`);
Expand Down Expand Up @@ -155,9 +155,31 @@ describe('wrap-enums', () => {
RequestMethod[RequestMethod["Head"] = 5] = "Head";
RequestMethod[RequestMethod["Patch"] = 6] = "Patch";
return RequestMethod;
})({});
})(RequestMethod || (RequestMethod = {}));
`;

expect(tags.oneLine`${transform(input)}`).toEqual(tags.oneLine`${output}`);
});

it('wraps exported enums in IIFE', () => {
const input = tags.stripIndent`
var ExportEnum;
(function (ExportEnum) {
ExportEnum[ExportEnum["A"] = 0] = "A";
ExportEnum[ExportEnum["B"] = 1] = "B";
ExportEnum[ExportEnum["C"] = 2] = "C";
})(ExportEnum = exports.ExportEnum || (exports.ExportEnum = {}));
`;
const output = tags.stripIndent`
var ExportEnum = /*@__PURE__*/ (function (ExportEnum) {
ExportEnum[ExportEnum["A"] = 0] = "A";
ExportEnum[ExportEnum["B"] = 1] = "B";
ExportEnum[ExportEnum["C"] = 2] = "C";
return ExportEnum;
})(ExportEnum = exports.ExportEnum || (exports.ExportEnum = {}));
`;

expect(tags.oneLine`${transform(input)}`).toEqual(tags.oneLine`${output}`);
});

});

0 comments on commit 8b5b9a1

Please sign in to comment.