Skip to content

Commit fefa2ef

Browse files
filipesilvaKeen Yee Liau
authored andcommitted
fix(@angular-devkit/build-optimizer): scrub previously whitelisted angular classes
This whitelist a leftover from older Angular versions and isn't necessary anymore. Fix #15194
1 parent 0fd7abb commit fefa2ef

File tree

2 files changed

+33
-78
lines changed

2 files changed

+33
-78
lines changed

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

Lines changed: 33 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,6 @@ export function testScrubFile(content: string) {
2222
return markers.some((marker) => content.indexOf(marker) !== -1);
2323
}
2424

25-
// Don't remove `ctorParameters` from these.
26-
const platformWhitelist = [
27-
'PlatformRef_',
28-
'TestabilityRegistry',
29-
'Console',
30-
'BrowserPlatformLocation',
31-
];
32-
3325
const angularSpecifiers = [
3426
// Class level decorators.
3527
'Component',
@@ -89,8 +81,7 @@ function scrubFileTransformer(checker: ts.TypeChecker, isAngularCoreFile: boolea
8981
if (isPropDecoratorAssignmentExpression(exprStmt)) {
9082
nodes.push(...pickPropDecorationNodesToRemove(exprStmt, ngMetadata, checker));
9183
}
92-
if (isCtorParamsAssignmentExpression(exprStmt)
93-
&& !isCtorParamsWhitelistedService(exprStmt)) {
84+
if (isCtorParamsAssignmentExpression(exprStmt)) {
9485
nodes.push(node);
9586
}
9687
}
@@ -369,14 +360,6 @@ function isCtorParamsAssignmentExpression(exprStmt: ts.ExpressionStatement): boo
369360
return true;
370361
}
371362

372-
function isCtorParamsWhitelistedService(exprStmt: ts.ExpressionStatement): boolean {
373-
const expr = exprStmt.expression as ts.BinaryExpression;
374-
const propAccess = expr.left as ts.PropertyAccessExpression;
375-
const serviceId = propAccess.expression as ts.Identifier;
376-
377-
return platformWhitelist.indexOf(serviceId.text) !== -1;
378-
}
379-
380363
// Remove Angular decorators from`Clazz.decorators = [...];`, or expression itself if all are
381364
// removed.
382365
function pickDecorationNodesToRemove(
@@ -435,42 +418,40 @@ function pickDecorateNodesToRemove(
435418
return identifierIsMetadata(id, ngMetadata, checker);
436419
});
437420

438-
// Only remove constructor parameter metadata on non-whitelisted classes.
439-
if (platformWhitelist.indexOf(classId.text) === -1) {
440-
// Remove __metadata calls of type 'design:paramtypes'.
441-
const metadataCalls = elements.filter((el) => {
442-
if (!isTslibHelper(el, '__metadata', tslibImports, checker)) {
443-
return false;
444-
}
445-
if (el.arguments.length < 2) {
446-
return false;
447-
}
448-
if (el.arguments[0].kind !== ts.SyntaxKind.StringLiteral) {
449-
return false;
450-
}
451-
const metadataTypeId = el.arguments[0] as ts.StringLiteral;
452-
if (metadataTypeId.text !== 'design:paramtypes') {
453-
return false;
454-
}
455421

456-
return true;
457-
});
458-
// Remove all __param calls.
459-
const paramCalls = elements.filter((el) => {
460-
if (!isTslibHelper(el, '__param', tslibImports, checker)) {
461-
return false;
462-
}
463-
if (el.arguments.length != 2) {
464-
return false;
465-
}
466-
if (el.arguments[0].kind !== ts.SyntaxKind.NumericLiteral) {
467-
return false;
468-
}
422+
// Remove __metadata calls of type 'design:paramtypes'.
423+
const metadataCalls = elements.filter((el) => {
424+
if (!isTslibHelper(el, '__metadata', tslibImports, checker)) {
425+
return false;
426+
}
427+
if (el.arguments.length < 2) {
428+
return false;
429+
}
430+
if (el.arguments[0].kind !== ts.SyntaxKind.StringLiteral) {
431+
return false;
432+
}
433+
const metadataTypeId = el.arguments[0] as ts.StringLiteral;
434+
if (metadataTypeId.text !== 'design:paramtypes') {
435+
return false;
436+
}
469437

470-
return true;
471-
});
472-
ngDecoratorCalls.push(...metadataCalls, ...paramCalls);
473-
}
438+
return true;
439+
});
440+
// Remove all __param calls.
441+
const paramCalls = elements.filter((el) => {
442+
if (!isTslibHelper(el, '__param', tslibImports, checker)) {
443+
return false;
444+
}
445+
if (el.arguments.length != 2) {
446+
return false;
447+
}
448+
if (el.arguments[0].kind !== ts.SyntaxKind.NumericLiteral) {
449+
return false;
450+
}
451+
452+
return true;
453+
});
454+
ngDecoratorCalls.push(...metadataCalls, ...paramCalls);
474455

475456
// If all decorators are metadata decorators then return the whole `Class = __decorate([...])'`
476457
// statement so that it is removed in entirety

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

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -188,23 +188,6 @@ describe('scrub-file', () => {
188188
expect(tags.oneLine`${transform(input)}`).toEqual(tags.oneLine`${output}`);
189189
});
190190

191-
it('doesn\t remove constructor parameter metadata for whitelisted classes', () => {
192-
const input = tags.stripIndent`
193-
import { ElementRef } from '@angular/core';
194-
import { LibService } from 'another-lib';
195-
var BrowserPlatformLocation = (function () {
196-
function BrowserPlatformLocation() { }
197-
BrowserPlatformLocation = __decorate([
198-
__metadata("design:paramtypes", [ElementRef, LibService])
199-
], BrowserPlatformLocation);
200-
return BrowserPlatformLocation;
201-
}());
202-
`;
203-
204-
expect(testScrubFile(input)).toBeTruthy();
205-
expect(tags.oneLine`${transform(input)}`).toEqual(tags.oneLine`${input}`);
206-
});
207-
208191
it('removes only Angular decorators calls in __decorate', () => {
209192
const output = tags.stripIndent`
210193
import { Component } from '@angular/core';
@@ -628,14 +611,5 @@ describe('scrub-file', () => {
628611

629612
expect(tags.oneLine`${transform(input)}`).toEqual(tags.oneLine`${output}`);
630613
});
631-
632-
it('doesn\'t remove constructor parameters from whitelisted classes', () => {
633-
const input = tags.stripIndent`
634-
${clazz.replace('Clazz', 'PlatformRef_')}
635-
PlatformRef_.ctorParameters = function () { return []; };
636-
`;
637-
638-
expect(tags.oneLine`${transform(input)}`).toEqual(tags.oneLine`${input}`);
639-
});
640614
});
641615
});

0 commit comments

Comments
 (0)