Skip to content

Commit f665c46

Browse files
alan-agius4mgechev
authored andcommitted
refactor(@angular-devkit/build-optimizer): use type guards in scrub file
1 parent 50d559d commit f665c46

File tree

1 file changed

+7
-10
lines changed
  • packages/angular_devkit/build_optimizer/src/transforms

1 file changed

+7
-10
lines changed

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

+7-10
Original file line numberDiff line numberDiff line change
@@ -47,15 +47,13 @@ function scrubFileTransformer(program: ts.Program | undefined, isAngularCoreFile
4747
ts.forEachChild(sf, checkNodeForDecorators);
4848

4949
function checkNodeForDecorators(node: ts.Node): void {
50-
if (node.kind !== ts.SyntaxKind.ExpressionStatement) {
51-
// TS 2.4 nests decorators inside downleveled class IIFEs, so we
52-
// must recurse into them to find the relevant expression statements.
50+
if (!ts.isExpressionStatement(node)) {
5351
return ts.forEachChild(node, checkNodeForDecorators);
5452
}
53+
5554
const exprStmt = node as ts.ExpressionStatement;
5655
// Do checks that don't need the typechecker first and bail early.
57-
if (isIvyPrivateCallExpression(exprStmt)
58-
|| isCtorParamsAssignmentExpression(exprStmt)) {
56+
if (isIvyPrivateCallExpression(exprStmt) || isCtorParamsAssignmentExpression(exprStmt)) {
5957
nodes.push(node);
6058
} else if (isDecoratorAssignmentExpression(exprStmt)) {
6159
nodes.push(...pickDecorationNodesToRemove(exprStmt, ngMetadata, checker));
@@ -360,7 +358,7 @@ function pickDecorationNodesToRemove(
360358
const expr = expect<ts.BinaryExpression>(exprStmt.expression, ts.SyntaxKind.BinaryExpression);
361359
const literal = expect<ts.ArrayLiteralExpression>(expr.right,
362360
ts.SyntaxKind.ArrayLiteralExpression);
363-
if (!literal.elements.every((elem) => elem.kind === ts.SyntaxKind.ObjectLiteralExpression)) {
361+
if (!literal.elements.every(elem => ts.isObjectLiteralExpression(elem))) {
364362
return [];
365363
}
366364
const elements = literal.elements as ts.NodeArray<ts.ObjectLiteralExpression>;
@@ -451,8 +449,8 @@ function pickPropDecorationNodesToRemove(
451449
const expr = expect<ts.BinaryExpression>(exprStmt.expression, ts.SyntaxKind.BinaryExpression);
452450
const literal = expect<ts.ObjectLiteralExpression>(expr.right,
453451
ts.SyntaxKind.ObjectLiteralExpression);
454-
if (!literal.properties.every((elem) => elem.kind === ts.SyntaxKind.PropertyAssignment &&
455-
(elem as ts.PropertyAssignment).initializer.kind === ts.SyntaxKind.ArrayLiteralExpression)) {
452+
if (!literal.properties.every(elem => ts.isPropertyAssignment(elem)
453+
&& ts.isArrayLiteralExpression(elem.initializer))) {
456454
return [];
457455
}
458456
const assignments = literal.properties as ts.NodeArray<ts.PropertyAssignment>;
@@ -482,8 +480,7 @@ function pickPropDecorationNodesToRemove(
482480
// If every node to be removed is a property assignment (full property's decorators) and
483481
// all properties are accounted for, remove the whole assignment. Otherwise, remove the
484482
// nodes which were marked as safe.
485-
if (toRemove.length === assignments.length &&
486-
toRemove.every((node) => node.kind === ts.SyntaxKind.PropertyAssignment)) {
483+
if (toRemove.length === assignments.length && toRemove.every((node) => ts.isPropertyAssignment(node))) {
487484
return [exprStmt];
488485
}
489486

0 commit comments

Comments
 (0)