Skip to content

About

Solidity Code Metrics

Resources

Stars

293 stars

Watchers

10 watching

Forks

Latest commit

ย 

History

86 Commits

Folders and files

NameName
Last commit message
Last commit date
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

get in touch with Consensys Diligence
[ ๐ŸŒ ๐Ÿ“ฉ ๐Ÿ”ฅ ]

Solidity Code Metrics

๐ŸŒ npm install solidity-code-metrics

The number-crunching enginge behind ๐Ÿ“Štintinweb.solidity-metrics.

vscode-solidity-metrics3

Example

CLI

To use the CLI from the compiled sources you can do:

node ./src/cli.js <path to solidity file(s)>

It is however more useful to install solidity-metrics globally and call it from any directory:

npm install -g solidity-code-metrics
solidity-code-metrics myfile1.sol myfile2.sol

By default, the cli outputs to the console, you can however store the output in a file rather easily (both markdown and html are supported):

solidity-code-metrics myfile.sol > metrics.md
solidity-code-metrics myfile.sol --html > metrics.html

Library

Requires Node.js 20 or >=22.

const {SolidityMetricsContainer} = require('solidity-code-metrics');

let options = {
    basePath:"",
    inputFileGlobExclusions:undefined,
    inputFileGlob: undefined,
    inputFileGlobLimit: undefined,
    debug:false,
    disableDoppelGanger: false, // true skips matching contracts against known libraries (faster)
    repoInfo: {
        branch: undefined,
        commit: undefined,
        remote: undefined
    }    
}

let metrics = new SolidityMetricsContainer("metricsContainerName", options);


// analyze files
metrics.analyze(path_to_solidity_file);
// ...
metrics.analyze(path_to_solidity_file_N);
// analyze a source string instead of reading the file (e.g. an unsaved editor buffer).
// totals() and details() use the string; generateReportMarkdown() and getDotGraphs()
// still read the path from disk for their surya sections.
metrics.analyze("contracts/Token.sol", { content: solidity_source_string });

// output
console.log(metrics.totals());
// per-file metrics: [{ name, metrics: { sloc, nsloc, complexity, summary, num, capabilities, other } }]
// other.doppelganger holds pending Promises unless disableDoppelGanger is set
console.log(metrics.details());
metrics.generateReportMarkdown().then(text => console.log(text));
// or let text = await metrics.generateReportMarkdown();

// paths relative to basePath; both accept `\` and `/` separators
metrics.relativePath("/project/contracts/Token.sol");   // "contracts/Token.sol" (basePath "/project/")
metrics.toRelativeGlob("C:\\project\\contracts");      // "contracts" (basePath "C:\\project/"), always `/`-separated

Development

npm ci
npm test        # vitest: library tests and CLI end-to-end tests

About

Solidity Code Metrics

Resources

Stars

293 stars

Watchers

10 watching

Forks

Releases

Packages

Used by

Contributors

Languages