Registry / devops / solidity-ast

solidity-ast

JSON →
library0.4.62jsnpmunverified

Solidity AST schema, TypeScript type definitions, and utility functions for traversing and querying Solidity compiler ASTs. Current stable version is 0.4.62. Released as needed to support new Solidity versions. Key differentiators: auto-generated types from JSON Schema, type-safe helpers like isNodeType, findAll, astDereferencer, and srcDecoder. Works with Solidity >=0.6.6 (mostly compatible down to 0.5.0).

npm install solidity-ast
INSTALL
IMPORT
SIG · SOLIDITY-AST
S
solidity-ast
devopsjavascriptv0.4.62
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

SourceUnit
import type { SourceUnit } from 'solidity-ast'
const { SourceUnit } = require('solidity-ast')
TypeScript types are exported as types only; use `import type` for compile-time only, or `import { SourceUnit }` if you need it as a value (not recommended). Since it's a type-only import, it's erased at runtime.
isNodeType
import { isNodeType } from 'solidity-ast/utils'
import { isNodeType } from 'solidity-ast'
Utility functions are not in the main package; they must be imported from 'solidity-ast/utils'.
findAll
import { findAll } from 'solidity-ast/utils'
import { findAll } from 'solidity-ast'
findAll is a generator function; loop over it with `for...of` or spread into an array. Second argument is the root AST node; third optional argument is a prune function.
astDereferencer
import { astDereferencer } from 'solidity-ast/utils'
const { astDereferencer } = require('solidity-ast/utils');
astDereferencer returns a function that takes (nodeType, id) and returns the node. For CommonJS, use `const { astDereferencer } = require('solidity-ast/utils');`.

Shows importing types from solidity-ast and using isNodeType and findAll utilities to traverse a Solidity AST.

import type { SourceUnit, ContractDefinition } from 'solidity-ast'; import { isNodeType, findAll } from 'solidity-ast/utils'; // Example: parse a Solidity source and traverse AST // Assuming you have the AST from solc output const sourceUnit: SourceUnit = { nodeType: 'SourceUnit', nodes: [ { nodeType: 'ContractDefinition', name: 'MyContract', abstract: false, fullyImplemented: true, linearizedBaseContracts: [], baseContracts: [], nodes: [], }, ], }; // Check node type and narrow if (isNodeType('ContractDefinition', sourceUnit.nodes[0])) { console.log((sourceUnit.nodes[0] as ContractDefinition).name); } // Iterate all ContractDefinitions for (const contractDef of findAll('ContractDefinition', sourceUnit)) { console.log(contractDef.name); }
Debug
Known issues
gotchaUtility functions must be imported from 'solidity-ast/utils', not from 'solidity-ast'.
fix
Use `import { ... } from 'solidity-ast/utils'` for isNodeType, findAll, astDereferencer, srcDecoder.
affects: >=0.0.0
gotchaThe AST types are generated from the JSON Schema and may not cover all edge cases or newer Solidity features immediately.
fix
Pin to the latest version of solidity-ast and check compatibility with your Solidity version. The package states it's accurate for >=0.6.6.
affects: <0.5.0
breakingBreaking changes may occur between minor versions if the Solidity AST structure changes. No explicit semver guarantee for AST shape.
fix
Always test after upgrade and consult the changelog or diffs on the NPM package.
affects: >=0.0.0
deprecatedSome older Solidity versions (<0.6.0) may have incomplete type coverage.
fix
Use the types for traversal but be aware that some node types or fields may be missing or inaccurate.
affects: <0.5.0
gotchaastDereferencer requires the full solc output (with all source files' ASTs). Passing only a single source unit will not work.
fix
Always pass the complete solc output object (the entire JSON from solc standard JSON input/output).
affects: >=0.0.0
Errors
Common errors & fixes
TypeError: isNodeType is not a function
Importing from wrong path (solidity-ast instead of solidity-ast/utils).
fix
Change import to: `import { isNodeType } from 'solidity-ast/utils'`
Module '"solidity-ast"' has no exported member 'SourceUnit'.
Trying to import SourceUnit as a value in a JavaScript file or with `require` in TypeScript without `type` keyword.
fix
Use `import type { SourceUnit } from 'solidity-ast'` in TypeScript, or `const { SourceUnit } = require('solidity-ast')` is incorrect because it's a type-only export.
Cannot find module 'solidity-ast/utils' or its corresponding type declarations.
Missing type declaration for the utility subpath; older versions of TypeScript may not resolve it, or package.json exports missing.
fix
Update to latest version of solidity-ast and ensure your tsconfig includes `node` moduleResolution or `node16`.
Type '{}' is not assignable to type 'SourceUnit'.
Passing an empty object where a full SourceUnit object is expected.
fix
Ensure the object has all required fields like `nodeType: 'SourceUnit'`, `nodes`, etc. Use the defined types.
Upgrade
Version history
0.4.62latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
4 hits · last 30 days
node
4
Resources
solidity-ast — npm install solidity-ast · libregistry