Skip to content

Commit 1b8593d

Browse files
committed
fix(@angular-devkit/build-optimizer): fix tslib.__decorate calls
Some code already have been compiled to use tslib. The decorators should be stripped for those as well. Fixes #215
1 parent cd0d619 commit 1b8593d

File tree

2 files changed

+75
-4
lines changed

2 files changed

+75
-4
lines changed

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

+21-4
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
import * as ts from 'typescript';
99
import { collectDeepNodes } from '../helpers/ast-utils';
1010

11+
const tslibDecorateRe = /\btslib(?:_\d+)?\.__decorate\b/;
12+
const tslibRe = /\btslib(?:_\d+)?\b/;
13+
1114

1215
export function testScrubFile(content: string) {
1316
const markers = [
@@ -17,7 +20,8 @@ export function testScrubFile(content: string) {
1720
'ctorParameters',
1821
];
1922

20-
return markers.some((marker) => content.indexOf(marker) !== -1);
23+
return markers.some((marker) => content.indexOf(marker) !== -1)
24+
|| tslibDecorateRe.test(content);
2125
}
2226

2327
// Don't remove `ctorParameters` from these.
@@ -202,12 +206,25 @@ function isDecorateAssignmentExpression(exprStmt: ts.ExpressionStatement): boole
202206
}
203207
const classIdent = expr.left as ts.Identifier;
204208
const callExpr = expr.right as ts.CallExpression;
209+
let callExprIdent = callExpr.expression as ts.Identifier;
210+
205211
if (callExpr.expression.kind !== ts.SyntaxKind.Identifier) {
206-
return false;
212+
if (callExpr.expression.kind === ts.SyntaxKind.PropertyAccessExpression) {
213+
const propAccess = callExpr.expression as ts.PropertyAccessExpression;
214+
const left = propAccess.expression;
215+
callExprIdent = propAccess.name;
216+
217+
if (!(left.kind === ts.SyntaxKind.Identifier && tslibRe.test((left as ts.Identifier).text))) {
218+
return false;
219+
}
220+
} else {
221+
return false;
222+
}
207223
}
208-
const callExprIdent = callExpr.expression as ts.Identifier;
224+
209225
// node.text on a name that starts with two underscores will return three instead.
210-
if (callExprIdent.text !== '___decorate') {
226+
// Unless it's an expression like tslib.__decorate, in which case it's only 2.
227+
if (callExprIdent.text !== '___decorate' && callExprIdent.text !== '__decorate') {
211228
return false;
212229
}
213230
if (callExpr.arguments.length !== 2) {

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

+54
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,60 @@ describe('scrub-file', () => {
140140
expect(testScrubFile(input)).toBeTruthy();
141141
expect(tags.oneLine`${transform(input)}`).toEqual(tags.oneLine`${output}`);
142142
});
143+
144+
it('recognizes tslib as well', () => {
145+
const input = tags.stripIndent`
146+
import { Component } from '@angular/core';
147+
import { NotComponent } from 'another-lib';
148+
var Clazz = (function () {
149+
function Clazz() { }
150+
Clazz = tslib.__decorate([
151+
NotComponent(),
152+
Component({
153+
selector: 'app-root',
154+
templateUrl: './app.component.html',
155+
styleUrls: ['./app.component.css']
156+
})
157+
], Clazz);
158+
return Clazz;
159+
}());
160+
161+
var Clazz2 = (function () {
162+
function Clazz2() { }
163+
Clazz2 = tslib_2.__decorate([
164+
NotComponent(),
165+
Component({
166+
selector: 'app-root',
167+
templateUrl: './app.component.html',
168+
styleUrls: ['./app.component.css']
169+
})
170+
], Clazz2);
171+
return Clazz2;
172+
}());
173+
`;
174+
const output = tags.stripIndent`
175+
import { Component } from '@angular/core';
176+
import { NotComponent } from 'another-lib';
177+
var Clazz = (function () {
178+
function Clazz() { }
179+
Clazz = tslib.__decorate([
180+
NotComponent()
181+
], Clazz);
182+
return Clazz;
183+
}());
184+
185+
var Clazz2 = (function () {
186+
function Clazz2() { }
187+
Clazz2 = tslib_2.__decorate([
188+
NotComponent()
189+
], Clazz2);
190+
return Clazz2;
191+
}());
192+
`;
193+
194+
expect(testScrubFile(input)).toBeTruthy();
195+
expect(tags.oneLine`${transform(input)}`).toEqual(tags.oneLine`${output}`);
196+
});
143197
});
144198

145199
describe('propDecorators', () => {

0 commit comments

Comments
 (0)