简体   繁体   中英

How to use jsHint to extract code metrics in the CLI report?

I am trying to get code metrics from jshint like on jshint.com :

Metrics

There is only one function in this file.

It takes no arguments.

This function contains 2 statements.

Cyclomatic complexity number for this function is 1.

The cli execution only shows linting outputs, like missing semicolon etc., But does not output any of these metrics. However, the above link seems to do that just fine for any pasted code.

How can I get such a code report across all JS files/modules? Technically this information should be available but I'm unable to have any output similar to the above via the CLI. Rummaging through the source code it seems entirely possible but I can't figure out if there's an easier way than hacking at the source code.

After rummaging through the source code and checking out the online docs, it seems you can write your own reporter as shown here . However, it fails to mention that you can also add additional arguments to get additional information in callback as seen in the code in its cli.js :

Combining the two here's how you can write your custom reporter function that prints the metrics to the console. It's a crude example modified from one in the repo in case someone stumbles on this:

module.exports = {
    reporter: function (res,data,options) {
        //console.dir(data,{depth:5}) //To see entire object
        data.forEach(d => {
            d.functions.forEach(f => {
                console.log(d.file, f.name, f.param, f.metrics);
            });
        });
    }
};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM