Home

Awesome

escomplex

Greenkeeper badge Build Status Known Vulnerabilities Dependencies Dev Dependencies

Software complexity analysis of JavaScript abstract syntax trees. The back-end for complexity-report.

Installation

The library is published on npm under the name escomplex. To install, you can add it to the dependencies in your package.json file or simply run:

npm i escomplex --save

Usage

You can use escomplex by including it as an Node.js module:

const escomplex = require('escomplex');

The module exports the analyse function.

analyse

The analyse function is used to convert the source code of one or more modules into one or more corresponding report objects containing metrics.

const result = escomplex.analyse(source, options);

Arguments

source

The first argument, source, must be either a string or an array of objects. If it is an array, each object should include a path property that is either a relative or full path to the equivalent module on disk and a code with the contents of the module. As well as identifying each of the result objects, the path property is also used during dependency analysis.

options

The third argument, options, is an optional object containing properties that modify some of the complexity calculations:

Result Format

The analyze function returns a report of the following format, with some variation depending on the given options.

For a single module

If a single source string is passed in the source argument, the result will be a report object that looks like the following:

{
    maintainability: 171,
    dependencies: [],
    aggregate: {
        sloc: {
            logical: 0,
            physical: 0
        },
        params: 0,
        cyclomatic: 1,
        cyclomaticDensity: 1,
        halstead: {
            vocabulary: 0,
            difficulty: 0,
            volume: 0,
            effort: 0,
            bugs: 0,
            time: 0
        }
    },
    functions: [
        {
            name: '',
            line: 0,
            sloc: {
                logical: 0,
                physical: 0
            },
            params: 0,
            cyclomatic: 1,
            cyclomaticDensity: 1,
            halstead: {
                vocabulary: 0,
                difficulty: 0,
                volume: 0,
                effort: 0,
                bugs: 0,
                time: 0
            }
        },
        ...
    ]
}

The meaning of those values, briefly, is as follows (see metrics for more information on each one):

For multiple modules

If an array of sources is passed in the source argument, the result will be an object that looks like the following:

{
    reports: [
        ...
    ],
    adjacencyMatrix: [
        [ 0 ]
    ],
    firstOrderDensity: 0,
    visibilityMatrix: [
        [ 0 ]
    ],
    changeCost: 100,
    coreSize: 100,
    loc: 0,
    cyclomatic: 1,
    effort: 0,
    params: 0,
    maintainability: 171
}

Those properties are defined as follows:

Refer to a more in-depth description of the metrics used for more details.

Related projects

Contributing

All changes should be submitted in the form of a pull request. Please refer to the contribution guidelines before submitting a pull request.

Source code is in /src. Unit tests are in /test. You can run the tests with npm test. You can run the linter with npm run lint. Make sure you've installed all the dependencies with npm install first.

License

MIT