Skip to content

Commit

Permalink
fix(@angular-devkit/build-optimizer): remove decorators in classes wi…
Browse files Browse the repository at this point in the history
…th static member access

Fix #228
  • Loading branch information
filipesilva authored and hansl committed Jun 6, 2018
1 parent 8e7cc11 commit e4d40ac
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -206,11 +206,23 @@ function isDecorateAssignmentExpression(
if (expr.left.kind !== ts.SyntaxKind.Identifier) {
return false;
}
if (expr.right.kind !== ts.SyntaxKind.CallExpression) {
const classIdent = expr.left as ts.Identifier;
let callExpr: ts.CallExpression;

if (expr.right.kind === ts.SyntaxKind.CallExpression) {
callExpr = expr.right as ts.CallExpression;
} else if (expr.right.kind === ts.SyntaxKind.BinaryExpression) {
// `Clazz = Clazz_1 = __decorate([...], Clazz)` can be found when there are static property
// accesses.
const innerExpr = expr.right as ts.BinaryExpression;
if (innerExpr.left.kind !== ts.SyntaxKind.Identifier
|| innerExpr.right.kind !== ts.SyntaxKind.CallExpression) {
return false;
}
callExpr = innerExpr.right as ts.CallExpression;
} else {
return false;
}
const classIdent = expr.left as ts.Identifier;
const callExpr = expr.right as ts.CallExpression;

if (!isTslibHelper(callExpr, '__decorate', tslibImports, checker)) {
return false;
Expand Down Expand Up @@ -372,9 +384,20 @@ function pickDecorateNodesToRemove(

const expr = expect<ts.BinaryExpression>(exprStmt.expression, ts.SyntaxKind.BinaryExpression);
const classId = expect<ts.Identifier>(expr.left, ts.SyntaxKind.Identifier);
const callExpr = expect<ts.CallExpression>(expr.right, ts.SyntaxKind.CallExpression);
let callExpr: ts.CallExpression;

if (expr.right.kind === ts.SyntaxKind.CallExpression) {
callExpr = expect<ts.CallExpression>(expr.right, ts.SyntaxKind.CallExpression);
} else if (expr.right.kind === ts.SyntaxKind.BinaryExpression) {
const innerExpr = expr.right as ts.BinaryExpression;
callExpr = expect<ts.CallExpression>(innerExpr.right, ts.SyntaxKind.CallExpression);
} else {
return [];
}

const arrLiteral = expect<ts.ArrayLiteralExpression>(callExpr.arguments[0],
ts.SyntaxKind.ArrayLiteralExpression);

if (!arrLiteral.elements.every((elem) => elem.kind === ts.SyntaxKind.CallExpression)) {
return [];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,48 @@ describe('scrub-file', () => {
expect(tags.oneLine`${transform(input)}`).toEqual(tags.oneLine`${output}`);
});

it('removes constructor parameter metadata when static properties are present', () => {
const output = tags.stripIndent`
import { Injectable } from '@angular/core';
import { Logger } from 'another-lib';
var GaService = (function () {
function GaService(logger) {
this.logger = logger;
}
GaService_1 = GaService;
GaService.prototype.initializeGa = function () {
console.log(GaService_1.initializeDelay);
};
GaService.initializeDelay = 1000;
return GaService;
var GaService_1;
}());
`;
const input = tags.stripIndent`
import { Injectable } from '@angular/core';
import { Logger } from 'another-lib';
var GaService = (function () {
function GaService(logger) {
this.logger = logger;
}
GaService_1 = GaService;
GaService.prototype.initializeGa = function () {
console.log(GaService_1.initializeDelay);
};
GaService.initializeDelay = 1000;
GaService = GaService_1 = __decorate([
Injectable(),
__metadata("design:paramtypes", [Logger])
], GaService);
return GaService;
var GaService_1;
}());
`;

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

it('doesn\t remove constructor parameter metadata for whitelisted classes', () => {
const input = tags.stripIndent`
import { ElementRef } from '@angular/core';
Expand Down

0 comments on commit e4d40ac

Please sign in to comment.