solidity-ast-traverse

Traversal function for Solidity AST trees.

Usage no npm install needed!

<script type="module">
  import solidityAstTraverse from 'https://cdn.skypack.dev/solidity-ast-traverse';
</script>

README

Solidity AST Traverse

Traversal function for Solidity AST trees.

Description

solidity-ast-traverse provides depth-first traversal of a Solidity abstract syntax tree (AST).

This package is a fork of sol-explore updated for modern AST code.

Install

yarn add solidity-ast-traverse

or

npm install solidity-ast-traverse --save 

Usage

Basic example

const solidityAstTraverse = require('solidity-ast-traverse')

solidityAstTraverse(ast, (node, parent) => {
    console.log('Node type: ' + node.nodeType)

    if (node.type === 'StructDeclaration') {	
        console.log('Stopping traversal')	
        return solidityAstTraverse.STOP_TRAVERSAL
    }
})

Advanced example

const solidityAstTraverse = require('solidity-ast-traverse')

solidityAstTraverse(ast, {
    enter: (node, parent) => {
        console.log ('Entering', node.nodeType)

        if (node.type === 'StructDeclaration') {
            console.log('Skipping child nodes')
            return solidityAstTraverse.SKIP_NODES_BELOW
        }
    },
    leave: (node) => {
        console.log ('Leaving', node.type)
    }
})

License

MIT