Skip to content

Commit b1f7537

Browse files
filipesilvavikerman
authored andcommitted
refactor(@angular-devkit/build-optimizer): refactor assignment expression identification
1 parent 016977c commit b1f7537

File tree

1 file changed

+16
-33
lines changed
  • packages/angular_devkit/build_optimizer/src/transforms

1 file changed

+16
-33
lines changed

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

Lines changed: 16 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -150,23 +150,10 @@ function isAngularCoreImport(node: ts.ImportDeclaration, isAngularCoreFile: bool
150150

151151
// Check if assignment is `Clazz.decorators = [...];`.
152152
function isDecoratorAssignmentExpression(exprStmt: ts.ExpressionStatement): boolean {
153-
if (exprStmt.expression.kind !== ts.SyntaxKind.BinaryExpression) {
153+
if (!isAssignmentExpressionTo(exprStmt, 'decorators')) {
154154
return false;
155155
}
156156
const expr = exprStmt.expression as ts.BinaryExpression;
157-
if (expr.left.kind !== ts.SyntaxKind.PropertyAccessExpression) {
158-
return false;
159-
}
160-
const propAccess = expr.left as ts.PropertyAccessExpression;
161-
if (propAccess.expression.kind !== ts.SyntaxKind.Identifier) {
162-
return false;
163-
}
164-
if (propAccess.name.text !== 'decorators') {
165-
return false;
166-
}
167-
if (expr.operatorToken.kind !== ts.SyntaxKind.FirstAssignment) {
168-
return false;
169-
}
170157
if (expr.right.kind !== ts.SyntaxKind.ArrayLiteralExpression) {
171158
return false;
172159
}
@@ -275,32 +262,33 @@ function isAngularDecoratorMetadataExpression(
275262

276263
// Check if assignment is `Clazz.propDecorators = [...];`.
277264
function isPropDecoratorAssignmentExpression(exprStmt: ts.ExpressionStatement): boolean {
278-
if (exprStmt.expression.kind !== ts.SyntaxKind.BinaryExpression) {
265+
if (!isAssignmentExpressionTo(exprStmt, 'propDecorators')) {
279266
return false;
280267
}
281268
const expr = exprStmt.expression as ts.BinaryExpression;
282-
if (expr.left.kind !== ts.SyntaxKind.PropertyAccessExpression) {
283-
return false;
284-
}
285-
const propAccess = expr.left as ts.PropertyAccessExpression;
286-
if (propAccess.expression.kind !== ts.SyntaxKind.Identifier) {
287-
return false;
288-
}
289-
if (propAccess.name.text !== 'propDecorators') {
269+
if (expr.right.kind !== ts.SyntaxKind.ObjectLiteralExpression) {
290270
return false;
291271
}
292-
if (expr.operatorToken.kind !== ts.SyntaxKind.FirstAssignment) {
272+
273+
return true;
274+
}
275+
276+
// Check if assignment is `Clazz.ctorParameters = [...];`.
277+
function isCtorParamsAssignmentExpression(exprStmt: ts.ExpressionStatement): boolean {
278+
if (!isAssignmentExpressionTo(exprStmt, 'ctorParameters')) {
293279
return false;
294280
}
295-
if (expr.right.kind !== ts.SyntaxKind.ObjectLiteralExpression) {
281+
const expr = exprStmt.expression as ts.BinaryExpression;
282+
if (expr.right.kind !== ts.SyntaxKind.FunctionExpression
283+
&& expr.right.kind !== ts.SyntaxKind.ArrowFunction
284+
) {
296285
return false;
297286
}
298287

299288
return true;
300289
}
301290

302-
// Check if assignment is `Clazz.ctorParameters = [...];`.
303-
function isCtorParamsAssignmentExpression(exprStmt: ts.ExpressionStatement): boolean {
291+
function isAssignmentExpressionTo(exprStmt: ts.ExpressionStatement, name: string) {
304292
if (exprStmt.expression.kind !== ts.SyntaxKind.BinaryExpression) {
305293
return false;
306294
}
@@ -309,7 +297,7 @@ function isCtorParamsAssignmentExpression(exprStmt: ts.ExpressionStatement): boo
309297
return false;
310298
}
311299
const propAccess = expr.left as ts.PropertyAccessExpression;
312-
if (propAccess.name.text !== 'ctorParameters') {
300+
if (propAccess.name.text !== name) {
313301
return false;
314302
}
315303
if (propAccess.expression.kind !== ts.SyntaxKind.Identifier) {
@@ -318,11 +306,6 @@ function isCtorParamsAssignmentExpression(exprStmt: ts.ExpressionStatement): boo
318306
if (expr.operatorToken.kind !== ts.SyntaxKind.FirstAssignment) {
319307
return false;
320308
}
321-
if (expr.right.kind !== ts.SyntaxKind.FunctionExpression
322-
&& expr.right.kind !== ts.SyntaxKind.ArrowFunction
323-
) {
324-
return false;
325-
}
326309

327310
return true;
328311
}

0 commit comments

Comments
 (0)