Skip to content

Commit c402903

Browse files
hawkgsmgechev
authored andcommitted
refactor(@schematics/angular): optimize insertAfterLastOccurrence AST util
1 parent 3abd87a commit c402903

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

packages/schematics/angular/utility/ast-utils.ts

+6-2
Original file line numberDiff line numberDiff line change
@@ -189,8 +189,12 @@ export function insertAfterLastOccurrence(nodes: ts.Node[],
189189
file: string,
190190
fallbackPos: number,
191191
syntaxKind?: ts.SyntaxKind): Change {
192-
// sort() has a side effect, so make a copy so that we won't overwrite the parent's object.
193-
let lastItem = [...nodes].sort(nodesByPosition).pop();
192+
let lastItem: ts.Node | undefined;
193+
for (const node of nodes) {
194+
if (!lastItem || lastItem.getStart() < node.getStart()) {
195+
lastItem = node;
196+
}
197+
}
194198
if (syntaxKind && lastItem) {
195199
lastItem = findNodes(lastItem, syntaxKind).sort(nodesByPosition).pop();
196200
}

0 commit comments

Comments
 (0)