From ac818121ad391ef89355a4876d10a641034cd029 Mon Sep 17 00:00:00 2001 From: aniket-engg Date: Thu, 1 Oct 2020 14:28:34 +0530 Subject: [PATCH] AST walk method updated --- libs/remix-astwalker/src/astWalker.ts | 83 ++++++++++----------------- 1 file changed, 30 insertions(+), 53 deletions(-) diff --git a/libs/remix-astwalker/src/astWalker.ts b/libs/remix-astwalker/src/astWalker.ts index 8fb6414b67..763f76b6b2 100644 --- a/libs/remix-astwalker/src/astWalker.ts +++ b/libs/remix-astwalker/src/astWalker.ts @@ -58,62 +58,43 @@ export class AstWalker extends EventEmitter { } } } + + getASTNodeChildren(ast: AstNode): AstNode[] { + const nodes = ast.nodes || (ast.body && ast.body.statements) || ast.declarations || [] + if (ast.body && ast.initializationExpression) { // 'for' loop handling + nodes.push(ast.initializationExpression) + } + return nodes + } + // eslint-disable-next-line @typescript-eslint/ban-types, @typescript-eslint/explicit-module-boundary-types - walk(ast: AstNodeLegacy | AstNode, callback?: Function | Record) { - if (callback) { - if (callback instanceof Function) { - callback = Object({ "*": callback }); - } - if (!("*" in callback)) { - callback["*"] = function() { - return true; - }; - } - if (ast) { - if ( - this.manageCallback(ast, callback) && - (ast).children && - (ast).children.length > 0 - ) { - for (const k in (ast).children) { - const child = (ast).children[k]; - this.walk(child, callback); - } + walk(ast: AstNode, callback?: Function | Record) { + if (ast) { + const children: AstNode[] = this.getASTNodeChildren(ast) + if (callback) { + if (callback instanceof Function) { + callback = Object({ "*": callback }); } - } else if (ast) { - if ( - this.manageCallback(ast, callback) && - (ast).nodes && - (ast).nodes.length > 0 - ) { - for (const k in (ast).nodes) { - const child = (ast).nodes[k]; - this.walk(child, callback); - } + if (!("*" in callback)) { + callback["*"] = function() { + return true; + }; } - } - } else { - if (ast) { - if ( - (ast).children && - (ast).children.length > 0 - ) { - for (const k in (ast).children) { - const child = (ast).children[k]; - this.emit("node", child); - this.walk(child); + if (this.manageCallback(ast, callback) && children?.length) { + for (const k in children) { + const child = children[k]; + this.walk(child, callback); } } - } - if (ast) { - if ((ast).nodes && (ast).nodes.length > 0) { - for (const k in (ast).nodes) { - const child = (ast).nodes[k]; - this.emit("node", child); - this.walk(child); + } else { + if (children?.length) { + for (const k in children) { + const child = children[k]; + this.emit("node", child); + this.walk(child); + } } } - } } } // eslint-disable-next-line @typescript-eslint/ban-types, @typescript-eslint/explicit-module-boundary-types @@ -151,14 +132,10 @@ export class AstWalker extends EventEmitter { if (cb) { if (sourcesList.ast) { this.walk(sourcesList.ast, cb); - } else { - this.walk(sourcesList.legacyAST, cb); } } else { if (sourcesList.ast) { this.walk(sourcesList.ast); - } else { - this.walk(sourcesList.legacyAST); } } }