GithubHelp home page GithubHelp logo

keccak384 / solidity-ast-3 Goto Github PK

View Code? Open in Web Editor NEW

This project forked from openzeppelin/solidity-ast

0.0 0.0 0.0 1.84 MB

TypeScript types and a JSON Schema for the Solidity AST

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

License: MIT License

Shell 0.23% JavaScript 72.59% TypeScript 7.94% Solidity 19.24%

solidity-ast-3's Introduction

Solidity AST Types

Docs NPM Package

TypeScript types and a JSON Schema for the Solidity AST.

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.6, 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.

An array of node types can be used as well to check if the node matches one of them.

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

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

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

findAll(nodeType, node[, prune])

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
}

If the optional prune: (node: Node) => boolean argument is specified, findAll will apply the function to each node, if the return value is truthy the node will be ignored, neither yielding the node nor recursing into it. Note that prune is not available when curried.

To enumerate multiple node types at the same time, nodeType can be an array of node types such as ['EnumDefinition', 'StructDefinition'].

for (const typeDef of findAll(['EnumDefinition', 'StructDefinition'], sourceUnit)) {
  // typeDef: EnumDefinition | StructDefinition
}

astDereferencer(solcOutput) => (nodeType, id) => Node

astDereferencer looks up AST nodes based on their id. Notably, it works across multiple source files, which is why it needs the entire solc JSON output with the ASTs for all source files in a compilation.

On Hardhat, the solc JSON output can be found in build info files.

const deref = astDereferencer(solcOutput);

deref('ContractDefinition', 4);

for (const contractDef of findAll('ContractDefinition', sourceUnit)) {
  const baseContracts = contractDef.linearizedBaseContracts.map(deref('ContractDefinition'));
  ...
}

srcDecoder(solcInput, solcOutput, basePath = '.') => (node: Node) => string

srcDecoder allows decoding of the src property of a node, which looks something like 123:4:0, into a human-readable description of the location of that node, such as file.sol:10.

On Hardhat, the solc JSON input and output can be found in build info files.

const decodeSrc = srcDecoder(solcInput, solcOutput);
...
const location = decodeSrc(contractDefinition);
console.log('found contract at ' + location);

solidity-ast-3's People

Contributors

frangio avatar dependabot[bot] avatar ernestognw avatar amxx avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.