|
|
|
@ -1,12 +1,12 @@ |
|
|
|
|
import { EventEmitter } from "events"; |
|
|
|
|
import { Node, AstNode } from "./index"; |
|
|
|
|
import { EventEmitter } from 'events' |
|
|
|
|
import { Node, AstNode } from './index' |
|
|
|
|
|
|
|
|
|
export declare interface AstWalker { |
|
|
|
|
new(): EventEmitter; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
const isObject = function (obj: any): boolean { |
|
|
|
|
return obj != null && obj.constructor.name === "Object" |
|
|
|
|
return obj != null && obj.constructor.name === 'Object' |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
export function isAstNode (node: Record<string, unknown>): boolean { |
|
|
|
@ -26,7 +26,6 @@ export function isYulAstNode(node: Record<string, unknown>): boolean { |
|
|
|
|
) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Crawl the given AST through the function walk(ast, callback) |
|
|
|
|
*/ |
|
|
|
@ -51,18 +50,18 @@ export class AstWalker extends EventEmitter { |
|
|
|
|
// return that.
|
|
|
|
|
if (node) { |
|
|
|
|
if ((node).name in callback) { |
|
|
|
|
return callback[(node).name](node); |
|
|
|
|
return callback[(node).name](node) |
|
|
|
|
} else { |
|
|
|
|
return callback["*"](node); |
|
|
|
|
return callback['*'](node) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
if (<AstNode>node) { |
|
|
|
|
if ((<AstNode>node).nodeType in callback) { |
|
|
|
|
/* istanbul ignore next */ |
|
|
|
|
return callback[(<AstNode>node).nodeType](node); |
|
|
|
|
return callback[(<AstNode>node).nodeType](node) |
|
|
|
|
} else { |
|
|
|
|
/* istanbul ignore next */ |
|
|
|
|
return callback["*"](node); |
|
|
|
|
return callback['*'](node) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
@ -76,31 +75,30 @@ export class AstWalker extends EventEmitter { |
|
|
|
|
nodes.forEach(x => { |
|
|
|
|
if (Array.isArray(x)) objNodes.push(...x) |
|
|
|
|
else objNodes.push(x) |
|
|
|
|
}); |
|
|
|
|
}) |
|
|
|
|
|
|
|
|
|
// Filter duplicate nodes using id field
|
|
|
|
|
const normalizedNodes = [] |
|
|
|
|
objNodes.forEach((element) => { |
|
|
|
|
const firstIndex = normalizedNodes.findIndex(e => e.id === element.id) |
|
|
|
|
if(firstIndex == -1) normalizedNodes.push(element) |
|
|
|
|
if (firstIndex === -1) normalizedNodes.push(element) |
|
|
|
|
}) |
|
|
|
|
return normalizedNodes |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
getASTNodeChildren (ast: AstNode): AstNode[] { |
|
|
|
|
|
|
|
|
|
let nodes = ast.nodes // for ContractDefinition
|
|
|
|
|
|| ast.body // for FunctionDefinition, ModifierDefinition, WhileStatement, DoWhileStatement, ForStatement
|
|
|
|
|
|| ast.statements // for Block, YulBlock
|
|
|
|
|
|| ast.members // for StructDefinition, EnumDefinition
|
|
|
|
|
|| ast.overrides // for OverrideSpecifier
|
|
|
|
|
|| ast.parameters // for ParameterList, EventDefinition
|
|
|
|
|
|| ast.declarations // for VariableDeclarationStatement
|
|
|
|
|
|| ast.expression // for Return, ExpressionStatement, FunctionCall, FunctionCallOptions, MemberAccess
|
|
|
|
|
|| ast.components // for TupleExpression
|
|
|
|
|
|| ast.subExpression // for UnaryOperation
|
|
|
|
|
|| ast.eventCall // for EmitStatement
|
|
|
|
|
|| [] |
|
|
|
|
let nodes = ast.nodes || // for ContractDefinition
|
|
|
|
|
ast.body || // for FunctionDefinition, ModifierDefinition, WhileStatement, DoWhileStatement, ForStatement
|
|
|
|
|
ast.statements || // for Block, YulBlock
|
|
|
|
|
ast.members || // for StructDefinition, EnumDefinition
|
|
|
|
|
ast.overrides || // for OverrideSpecifier
|
|
|
|
|
ast.parameters || // for ParameterList, EventDefinition
|
|
|
|
|
ast.declarations || // for VariableDeclarationStatement
|
|
|
|
|
ast.expression || // for Return, ExpressionStatement, FunctionCall, FunctionCallOptions, MemberAccess
|
|
|
|
|
ast.components || // for TupleExpression
|
|
|
|
|
ast.subExpression || // for UnaryOperation
|
|
|
|
|
ast.eventCall || // for EmitStatement
|
|
|
|
|
[] |
|
|
|
|
|
|
|
|
|
// If 'nodes' is not an array, convert it into one, for example: ast.body
|
|
|
|
|
if (nodes && !Array.isArray(nodes)) { |
|
|
|
@ -182,47 +180,48 @@ export class AstWalker extends EventEmitter { |
|
|
|
|
const children: AstNode[] = this.getASTNodeChildren(ast) |
|
|
|
|
if (callback) { |
|
|
|
|
if (callback instanceof Function) { |
|
|
|
|
callback = Object({ "*": callback }); |
|
|
|
|
callback = Object({ '*': callback }) |
|
|
|
|
} |
|
|
|
|
if (!('*' in callback)) { |
|
|
|
|
callback['*'] = function () { |
|
|
|
|
return true |
|
|
|
|
} |
|
|
|
|
if (!("*" in callback)) { |
|
|
|
|
callback["*"] = function() { |
|
|
|
|
return true; |
|
|
|
|
}; |
|
|
|
|
} |
|
|
|
|
if (this.manageCallback(ast, callback) && children?.length) { |
|
|
|
|
for (const k in children) { |
|
|
|
|
const child = children[k]; |
|
|
|
|
this.walk(child, callback); |
|
|
|
|
const child = children[k] |
|
|
|
|
this.walk(child, callback) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} else { |
|
|
|
|
if (children?.length) { |
|
|
|
|
for (const k in children) { |
|
|
|
|
const child = children[k]; |
|
|
|
|
this.emit("node", child); |
|
|
|
|
this.walk(child); |
|
|
|
|
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
|
|
|
|
|
walkFullInternal (ast: AstNode, callback: Function) { |
|
|
|
|
if (isAstNode(ast) || isYulAstNode(ast)) { |
|
|
|
|
// console.log(`XXX id ${ast.id}, nodeType: ${ast.nodeType}, src: ${ast.src}`);
|
|
|
|
|
callback(ast); |
|
|
|
|
callback(ast) |
|
|
|
|
for (const k of Object.keys(ast)) { |
|
|
|
|
// Possible optimization:
|
|
|
|
|
// if (k in ['id', 'src', 'nodeType']) continue;
|
|
|
|
|
const astItem = ast[k]; |
|
|
|
|
const astItem = ast[k] |
|
|
|
|
if (Array.isArray(astItem)) { |
|
|
|
|
for (const child of astItem) { |
|
|
|
|
if (child) { |
|
|
|
|
this.walkFullInternal(child, callback); |
|
|
|
|
this.walkFullInternal(child, callback) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} else { |
|
|
|
|
this.walkFullInternal(astItem, callback); |
|
|
|
|
this.walkFullInternal(astItem, callback) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
@ -231,18 +230,18 @@ export class AstWalker extends EventEmitter { |
|
|
|
|
// Normalizes parameter callback and calls walkFullInternal
|
|
|
|
|
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
|
|
|
|
|
walkFull (ast: AstNode, callback: any) { |
|
|
|
|
if (isAstNode(ast) || isYulAstNode(ast)) return this.walkFullInternal(ast, callback); |
|
|
|
|
if (isAstNode(ast) || isYulAstNode(ast)) return this.walkFullInternal(ast, callback) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// eslint-disable-next-line @typescript-eslint/ban-types, @typescript-eslint/explicit-module-boundary-types
|
|
|
|
|
walkAstList (sourcesList: Node, cb?: Function) { |
|
|
|
|
if (cb) { |
|
|
|
|
if (sourcesList.ast) { |
|
|
|
|
this.walk(sourcesList.ast, cb); |
|
|
|
|
this.walk(sourcesList.ast, cb) |
|
|
|
|
} |
|
|
|
|
} else { |
|
|
|
|
if (sourcesList.ast) { |
|
|
|
|
this.walk(sourcesList.ast); |
|
|
|
|
this.walk(sourcesList.ast) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|