@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-analyzerexport interface AnalysisResult {
versionPragmas: Array<string>;
imports: Array<string>;
}
export function analyze(input: string): AnalysisResult;analyze(`
pragma solidity ^0.8.0;
import "./file.sol";
`);
// { versionPragmas: [ '^0.8.0' ], imports: [ './file.sol' ] }This library has two different goals:
- Being fast
- 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/.
These 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.