`@nomicfoundation/solidity-analyzer` is an N-API library built in Rust, which exposes a single function, which takes the contents of a Solidity source file and returns its imports and version pragmas.
npm install @nomicfoundation/solidity-analyzer@nomicfoundation/solidity-analyzer@nomicfoundation/solidity-analyzer is an N-API library built in Rust, which exposes a single function, which takes the contents of a Solidity source file and returns its imports and version pragmas.
``bash`
npm install @nomicfoundation/solidity-analyzer
`ts
export interface AnalysisResult {
versionPragmas: Array
imports: Array
}
export function analyze(input: string): AnalysisResult;
`
`ts
analyze(
pragma solidity ^0.8.0;
import "./file.sol";);
// { versionPragmas: [ '^0.8.0' ], imports: [ './file.sol' ] }
`
This library has two different goals:
1. Being fast
2. Being error-tolerant
Both are achieved by not parsing the Solidity source but just tokenizing it instead. This allows us to create a simple state machine that only recognizes imports and pragmas, ignoring everything else, and recovering from malformed tokens or expressions.
This library doesn't work in a browser.
This project has some JavaScript unit tests in __test__, and it's also tested using the entire smart-contract-sanctuary.
You can find the scripts to test with the sanctuary in test-with-sanctuary/.
and index.d.tsThese files have to be committed because of our current Github Actions, yet, they are autogenerated.
You can run cargo clean, yarn clean, and yarn build` to recreate them.