Skip to content
This repository was archived by the owner on Apr 4, 2025. It is now read-only.

Commit 9452559

Browse files
filipesilvahansl
authored andcommitted
fix(@angular-devkit/build-optimizer): remove decorators in classes with static member access
Fix #228
1 parent e1ba2f5 commit 9452559

File tree

2 files changed

+69
-4
lines changed

2 files changed

+69
-4
lines changed

packages/angular_devkit/build_optimizer/src/transforms/scrub-file.ts

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -206,11 +206,23 @@ function isDecorateAssignmentExpression(
206206
if (expr.left.kind !== ts.SyntaxKind.Identifier) {
207207
return false;
208208
}
209-
if (expr.right.kind !== ts.SyntaxKind.CallExpression) {
209+
const classIdent = expr.left as ts.Identifier;
210+
let callExpr: ts.CallExpression;
211+
212+
if (expr.right.kind === ts.SyntaxKind.CallExpression) {
213+
callExpr = expr.right as ts.CallExpression;
214+
} else if (expr.right.kind === ts.SyntaxKind.BinaryExpression) {
215+
// `Clazz = Clazz_1 = __decorate([...], Clazz)` can be found when there are static property
216+
// accesses.
217+
const innerExpr = expr.right as ts.BinaryExpression;
218+
if (innerExpr.left.kind !== ts.SyntaxKind.Identifier
219+
|| innerExpr.right.kind !== ts.SyntaxKind.CallExpression) {
220+
return false;
221+
}
222+
callExpr = innerExpr.right as ts.CallExpression;
223+
} else {
210224
return false;
211225
}
212-
const classIdent = expr.left as ts.Identifier;
213-
const callExpr = expr.right as ts.CallExpression;
214226

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

373385
const expr = expect<ts.BinaryExpression>(exprStmt.expression, ts.SyntaxKind.BinaryExpression);
374386
const classId = expect<ts.Identifier>(expr.left, ts.SyntaxKind.Identifier);
375-
const callExpr = expect<ts.CallExpression>(expr.right, ts.SyntaxKind.CallExpression);
387+
let callExpr: ts.CallExpression;
388+
389+
if (expr.right.kind === ts.SyntaxKind.CallExpression) {
390+
callExpr = expect<ts.CallExpression>(expr.right, ts.SyntaxKind.CallExpression);
391+
} else if (expr.right.kind === ts.SyntaxKind.BinaryExpression) {
392+
const innerExpr = expr.right as ts.BinaryExpression;
393+
callExpr = expect<ts.CallExpression>(innerExpr.right, ts.SyntaxKind.CallExpression);
394+
} else {
395+
return [];
396+
}
397+
376398
const arrLiteral = expect<ts.ArrayLiteralExpression>(callExpr.arguments[0],
377399
ts.SyntaxKind.ArrayLiteralExpression);
400+
378401
if (!arrLiteral.elements.every((elem) => elem.kind === ts.SyntaxKind.CallExpression)) {
379402
return [];
380403
}

packages/angular_devkit/build_optimizer/src/transforms/scrub-file_spec.ts

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,48 @@ describe('scrub-file', () => {
138138
expect(tags.oneLine`${transform(input)}`).toEqual(tags.oneLine`${output}`);
139139
});
140140

141+
it('removes constructor parameter metadata when static properties are present', () => {
142+
const output = tags.stripIndent`
143+
import { Injectable } from '@angular/core';
144+
import { Logger } from 'another-lib';
145+
var GaService = (function () {
146+
function GaService(logger) {
147+
this.logger = logger;
148+
}
149+
GaService_1 = GaService;
150+
GaService.prototype.initializeGa = function () {
151+
console.log(GaService_1.initializeDelay);
152+
};
153+
GaService.initializeDelay = 1000;
154+
return GaService;
155+
var GaService_1;
156+
}());
157+
`;
158+
const input = tags.stripIndent`
159+
import { Injectable } from '@angular/core';
160+
import { Logger } from 'another-lib';
161+
var GaService = (function () {
162+
function GaService(logger) {
163+
this.logger = logger;
164+
}
165+
GaService_1 = GaService;
166+
GaService.prototype.initializeGa = function () {
167+
console.log(GaService_1.initializeDelay);
168+
};
169+
GaService.initializeDelay = 1000;
170+
GaService = GaService_1 = __decorate([
171+
Injectable(),
172+
__metadata("design:paramtypes", [Logger])
173+
], GaService);
174+
return GaService;
175+
var GaService_1;
176+
}());
177+
`;
178+
179+
expect(testScrubFile(input)).toBeTruthy();
180+
expect(tags.oneLine`${transform(input)}`).toEqual(tags.oneLine`${output}`);
181+
});
182+
141183
it('doesn\t remove constructor parameter metadata for whitelisted classes', () => {
142184
const input = tags.stripIndent`
143185
import { ElementRef } from '@angular/core';

0 commit comments

Comments
 (0)