Skip to content

Commit 78c62b7

Browse files
clydinhansl
authored andcommitted
fix(@angular-devkit/build-optimizer): ensure only one pure annotation per IIFE
1 parent 1a50c57 commit 78c62b7

File tree

1 file changed

+18
-23
lines changed

1 file changed

+18
-23
lines changed

packages/angular_devkit/build_optimizer/src/transforms/prefix-functions.ts

Lines changed: 18 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -52,40 +52,32 @@ export function getPrefixFunctionsTransformer(): ts.TransformerFactory<ts.Source
5252
export function findTopLevelFunctions(parentNode: ts.Node): ts.Node[] {
5353
const topLevelFunctions: ts.Node[] = [];
5454

55-
let previousNode: ts.Node;
5655
function cb(node: ts.Node) {
5756
// Stop recursing into this branch if it's a function expression or declaration
5857
if (node.kind === ts.SyntaxKind.FunctionDeclaration
5958
|| node.kind === ts.SyntaxKind.FunctionExpression) {
6059
return;
6160
}
6261

63-
// We need to check specially for IIFEs formatted as call expressions inside parenthesized
64-
// expressions: `(function() {}())` Their start pos doesn't include the opening paren
65-
// and must be adjusted.
66-
if (isIIFE(node)
67-
&& previousNode.kind === ts.SyntaxKind.ParenthesizedExpression
68-
&& node.parent
69-
&& !hasPureComment(node.parent)) {
70-
topLevelFunctions.push(node.parent);
71-
} else if ((node.kind === ts.SyntaxKind.CallExpression
72-
|| node.kind === ts.SyntaxKind.NewExpression)
73-
&& !hasPureComment(node)
74-
) {
75-
topLevelFunctions.push(node);
62+
let noPureComment = !hasPureComment(node);
63+
let innerNode = node;
64+
while (innerNode && ts.isParenthesizedExpression(innerNode)) {
65+
innerNode = innerNode.expression;
66+
noPureComment = noPureComment && !hasPureComment(innerNode);
7667
}
7768

78-
previousNode = node;
69+
if (!innerNode) {
70+
return;
71+
}
7972

80-
ts.forEachChild(node, cb);
81-
}
73+
if (noPureComment) {
74+
if (innerNode.kind === ts.SyntaxKind.CallExpression
75+
|| innerNode.kind === ts.SyntaxKind.NewExpression) {
76+
topLevelFunctions.push(node);
77+
}
78+
}
8279

83-
function isIIFE(node: ts.Node): boolean {
84-
return node.kind === ts.SyntaxKind.CallExpression
85-
// This check was in the old ngo but it doesn't seem to make sense with the typings.
86-
// TODO(filipesilva): ask Alex Rickabaugh about it.
87-
// && !(<ts.CallExpression>node).expression.text
88-
&& (node as ts.CallExpression).expression.kind !== ts.SyntaxKind.PropertyAccessExpression;
80+
ts.forEachChild(innerNode, cb);
8981
}
9082

9183
ts.forEachChild(parentNode, cb);
@@ -116,6 +108,9 @@ export function findPureImports(parentNode: ts.Node): string[] {
116108
}
117109

118110
function hasPureComment(node: ts.Node) {
111+
if (!node) {
112+
return false;
113+
}
119114
const leadingComment = ts.getSyntheticLeadingComments(node);
120115

121116
return leadingComment && leadingComment.some((comment) => comment.text === pureFunctionComment);

0 commit comments

Comments
 (0)