julianmrodri / solidity-ast

TypeScript types and a JSON Schema for the Solidity AST

Home Page:https://solidity-ast.netlify.app

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Solidity AST Types

Docs NPM Package

TypeScript types and a JSON Schema for the Solidity AST.

The latest version successfully validates all of OpenZeppelin Contracts, but some types may still be inaccurate. Please report any issues you find.

npm install solidity-ast
import type { SourceUnit, ContractDefinition } from 'solidity-ast';

The types included in the NPM package are automatically generated from the JSON Schema, so you will not find them in the repository. You can see what they look like on unpkg or the documentation.

Solidity Versioning

The types are currently accurate and tested for Solidity >=0.6.8, but you can very likely use them safely for any version since 0.6.0. For simple traversals they will probably work well for 0.5.0 and up as well.

The versioning story will be gradually improved upon and the ultimate goal is to be able to manipulate and traverse the AST in a uniform way that is as agnostic to the Solidity version as possible.

Utilities

Included in the package is a set of utility function for type-safe interactions with nodes based on the node type.

isNodeType(nodeType, node)

A type predicate that can be used for narrowing the type of an unknown node, or combined with higher order functions like filter.

import { isNodeType } from 'solidity-ast/utils';

if (isNodeType('ContractDefinition', node)) {
  // node: ContractDefinition
}

const contractDefs = sourceUnit.nodes.filter(isNodeType('ContractDefinition'));
  // contractDefs: ContractDefinition[]

findAll(nodeType, node)

findAll is a generator function that will recursively enumerate all descendent nodes of a given node type. It does this in an efficient way by visiting only the nodes that are necessary for the searched node type.

import { findAll } from 'solidity-ast/utils';

for (const functionDef of findAll('FunctionDefinition', sourceUnit)) {
  // functionDef: FunctionDefinition
}

About

TypeScript types and a JSON Schema for the Solidity AST

https://solidity-ast.netlify.app

License:MIT License


Languages

Language:JavaScript 63.0%Language:Solidity 35.0%Language:Shell 2.0%