From ed27ed4cefdc87d82f04e0c2fff5063c17239ab5 Mon Sep 17 00:00:00 2001 From: Kristiyan Kostadinov Date: Fri, 11 Nov 2022 07:06:39 +0100 Subject: [PATCH] fix(material/schematics): avoid mutating the AST when traversing The `visitElements` function was calling `reverse` on the node's `children` which reverses it in place. We shouldn't be mutating the AST just to traverse it in reverse order. These changes switch to using a reverse loop instead. --- .../ng-generate/mdc-migration/rules/tree-traversal.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/material/schematics/ng-generate/mdc-migration/rules/tree-traversal.ts b/src/material/schematics/ng-generate/mdc-migration/rules/tree-traversal.ts index 6ac6dc2e88e6..0cbb73f4f661 100644 --- a/src/material/schematics/ng-generate/mdc-migration/rules/tree-traversal.ts +++ b/src/material/schematics/ng-generate/mdc-migration/rules/tree-traversal.ts @@ -30,8 +30,7 @@ export function visitElements( preorderCallback: (node: TmplAstElement) => void = () => {}, postorderCallback: (node: TmplAstElement) => void = () => {}, ): void { - nodes.reverse(); - for (let i = 0; i < nodes.length; i++) { + for (let i = nodes.length - 1; i > -1; i--) { const node = nodes[i]; const isElement = node instanceof TmplAstElement;