{"id":29089643,"url":"https://github.com/openzeppelin/solidity-ast","last_synced_at":"2025-06-28T04:04:19.577Z","repository":{"id":38413537,"uuid":"264284930","full_name":"OpenZeppelin/solidity-ast","owner":"OpenZeppelin","description":"TypeScript types and a JSON Schema for the Solidity AST","archived":false,"fork":false,"pushed_at":"2025-05-08T00:15:55.000Z","size":2070,"stargazers_count":95,"open_issues_count":3,"forks_count":29,"subscribers_count":6,"default_branch":"master","last_synced_at":"2025-05-28T18:07:29.983Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"https://solidity-ast.info","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/OpenZeppelin.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-05-15T19:49:59.000Z","updated_at":"2025-05-16T08:54:40.000Z","dependencies_parsed_at":"2023-11-11T01:33:59.225Z","dependency_job_id":"ca75f434-c44e-4054-a93c-afcd9f1ff07c","html_url":"https://github.com/OpenZeppelin/solidity-ast","commit_stats":{"total_commits":266,"total_committers":5,"mean_commits":53.2,"dds":"0.12030075187969924","last_synced_commit":"facd1f96e22bb29a2841cacf60e6e1df8d8ee8ea"},"previous_names":[],"tags_count":78,"template":false,"template_full_name":null,"purl":"pkg:github/OpenZeppelin/solidity-ast","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/OpenZeppelin%2Fsolidity-ast","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/OpenZeppelin%2Fsolidity-ast/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/OpenZeppelin%2Fsolidity-ast/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/OpenZeppelin%2Fsolidity-ast/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/OpenZeppelin","download_url":"https://codeload.github.com/OpenZeppelin/solidity-ast/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/OpenZeppelin%2Fsolidity-ast/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":262371684,"owners_count":23300595,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2025-06-28T04:04:18.534Z","updated_at":"2025-06-28T04:04:19.554Z","avatar_url":"https://github.com/OpenZeppelin.png","language":"JavaScript","readme":"# Solidity AST Types\n\n[![Docs](https://img.shields.io/badge/docs-%F0%9F%93%84-blue)][docs]\n[![NPM Package](https://img.shields.io/npm/v/solidity-ast.svg)](https://www.npmjs.org/package/solidity-ast)\n\n**TypeScript types and a JSON Schema for the Solidity AST.**\n\n```\nnpm install solidity-ast\n```\n\n\n```typescript\nimport type { SourceUnit, ContractDefinition } from 'solidity-ast';\n```\n\nThe types included in the NPM package are automatically generated from the JSON\nSchema, so you will not find them in the repository. You can see what they look\nlike on [unpkg] or the [documentation][docs].\n\n[unpkg]: https://unpkg.com/solidity-ast@latest/types.d.ts\n[docs]: https://solidity-ast.info/\n\n## Solidity Versioning\n\nThe types are currently accurate and tested for Solidity \u003e=0.6.6, but you can\nvery likely use them safely for any version since 0.6.0. For simple traversals\nthey will probably work well for 0.5.0 and up as well.\n\nThe versioning story will be gradually improved upon and the ultimate goal is\nto be able to manipulate and traverse the AST in a uniform way that is as\nagnostic to the Solidity version as possible.\n\n## Utilities\n\nIncluded in the package is a set of utility functions for type-safe interactions\nwith nodes based on the node type.\n\n### `isNodeType(nodeType, node)`\n\nA type predicate that can be used for narrowing the type of an\nunknown node, or combined with higher order functions like `filter`.\n\nAn array of node types can be used as well to check if the node matches one of them.\n\n```typescript\nimport { isNodeType } from 'solidity-ast/utils';\n\nif (isNodeType('ContractDefinition', node)) {\n  // node: ContractDefinition\n}\n\nconst contractDefs = sourceUnit.nodes.filter(isNodeType('ContractDefinition'));\n  // contractDefs: ContractDefinition[]\n```\n\n### `findAll(nodeType, node[, prune])`\n\n`findAll` is a generator function that will recursively enumerate all\ndescendent nodes of a given node type. It does this in an efficient way by\nvisiting only the nodes that are necessary for the searched node type.\n\n```typescript\nimport { findAll } from 'solidity-ast/utils';\n\nfor (const functionDef of findAll('FunctionDefinition', sourceUnit)) {\n  // functionDef: FunctionDefinition\n}\n```\n\nIf the optional `prune: (node: Node) =\u003e boolean` argument is specified,\n`findAll` will apply the function to each node, if the return value is truthy\nthe node will be ignored, neither yielding the node nor recursing into it. Note\nthat `prune` is not available when curried.\n\nTo enumerate multiple node types at the same time, `nodeType` can be an array\nof node types such as `['EnumDefinition', 'StructDefinition']`.\n\n```typescript\nfor (const typeDef of findAll(['EnumDefinition', 'StructDefinition'], sourceUnit)) {\n  // typeDef: EnumDefinition | StructDefinition\n}\n```\n\nTo enumerate all subnodes regardless of node type, `nodeType` can be `'*'` (a\nstring with a single asterisk).\n\n### `astDereferencer(solcOutput) =\u003e (nodeType, id) =\u003e Node`\n\n`astDereferencer` looks up AST nodes based on their id. Notably, it works\nacross multiple source files, which is why it needs the entire solc JSON output\nwith the ASTs for all source files in a compilation.\n\n\u003e On Hardhat, the solc JSON output can be found in [build info files].\n\n[build info files]: https://hardhat.org/guides/compile-contracts.html#build-info-files\n\n```typescript\nconst deref = astDereferencer(solcOutput);\n\nderef('ContractDefinition', 4);\n\nfor (const contractDef of findAll('ContractDefinition', sourceUnit)) {\n  const baseContracts = contractDef.linearizedBaseContracts.map(deref('ContractDefinition'));\n  ...\n}\n```\n\nIt is also possible to obtain the source unit that contains the dereferenced node:\n\n```typescript\nconst deref = astDereferencer(solcOutput);\n\nconst { node, sourceUnit } = deref.withSourceUnit('ContractDefinition', 4);\n```\n\nIf the node type is unknown you can specify `'*'` for `nodeType`.\n\n### `srcDecoder(solcInput, solcOutput, basePath = '.') =\u003e (node: Node) =\u003e string`\n\n`srcDecoder` allows decoding of the `src` property of a node, which looks\nsomething like `123:4:0`, into a human-readable description of the location of\nthat node, such as `file.sol:10`.\n\n\u003e On Hardhat, the solc JSON input and output can be found in [build info files].\n\n[build info files]: https://hardhat.org/guides/compile-contracts.html#build-info-files\n\n```typescript\nconst decodeSrc = srcDecoder(solcInput, solcOutput);\n...\nconst location = decodeSrc(contractDefinition);\nconsole.log('found contract at ' + location);\n```\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fopenzeppelin%2Fsolidity-ast","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fopenzeppelin%2Fsolidity-ast","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fopenzeppelin%2Fsolidity-ast/lists"}