refactor(@angular-devkit/build-optimizer): use type guards in scrub file

This commit is contained in:
Alan Agius 2020-09-02 15:37:41 +02:00 committed by Minko Gechev
parent 50d559d73d
commit f665c466c2

View File

@ -47,15 +47,13 @@ function scrubFileTransformer(program: ts.Program | undefined, isAngularCoreFile
ts.forEachChild(sf, checkNodeForDecorators);
function checkNodeForDecorators(node: ts.Node): void {
if (node.kind !== ts.SyntaxKind.ExpressionStatement) {
// TS 2.4 nests decorators inside downleveled class IIFEs, so we
// must recurse into them to find the relevant expression statements.
if (!ts.isExpressionStatement(node)) {
return ts.forEachChild(node, checkNodeForDecorators);
}
const exprStmt = node as ts.ExpressionStatement;
// Do checks that don't need the typechecker first and bail early.
if (isIvyPrivateCallExpression(exprStmt)
|| isCtorParamsAssignmentExpression(exprStmt)) {
if (isIvyPrivateCallExpression(exprStmt) || isCtorParamsAssignmentExpression(exprStmt)) {
nodes.push(node);
} else if (isDecoratorAssignmentExpression(exprStmt)) {
nodes.push(...pickDecorationNodesToRemove(exprStmt, ngMetadata, checker));
@ -360,7 +358,7 @@ function pickDecorationNodesToRemove(
const expr = expect<ts.BinaryExpression>(exprStmt.expression, ts.SyntaxKind.BinaryExpression);
const literal = expect<ts.ArrayLiteralExpression>(expr.right,
ts.SyntaxKind.ArrayLiteralExpression);
if (!literal.elements.every((elem) => elem.kind === ts.SyntaxKind.ObjectLiteralExpression)) {
if (!literal.elements.every(elem => ts.isObjectLiteralExpression(elem))) {
return [];
}
const elements = literal.elements as ts.NodeArray<ts.ObjectLiteralExpression>;
@ -451,8 +449,8 @@ function pickPropDecorationNodesToRemove(
const expr = expect<ts.BinaryExpression>(exprStmt.expression, ts.SyntaxKind.BinaryExpression);
const literal = expect<ts.ObjectLiteralExpression>(expr.right,
ts.SyntaxKind.ObjectLiteralExpression);
if (!literal.properties.every((elem) => elem.kind === ts.SyntaxKind.PropertyAssignment &&
(elem as ts.PropertyAssignment).initializer.kind === ts.SyntaxKind.ArrayLiteralExpression)) {
if (!literal.properties.every(elem => ts.isPropertyAssignment(elem)
&& ts.isArrayLiteralExpression(elem.initializer))) {
return [];
}
const assignments = literal.properties as ts.NodeArray<ts.PropertyAssignment>;
@ -482,8 +480,7 @@ function pickPropDecorationNodesToRemove(
// If every node to be removed is a property assignment (full property's decorators) and
// all properties are accounted for, remove the whole assignment. Otherwise, remove the
// nodes which were marked as safe.
if (toRemove.length === assignments.length &&
toRemove.every((node) => node.kind === ts.SyntaxKind.PropertyAssignment)) {
if (toRemove.length === assignments.length && toRemove.every((node) => ts.isPropertyAssignment(node))) {
return [exprStmt];
}