1

I am using JSHint and I want to turn on cyclomatic complexity.
From JsHint homepage and JsHint Docs , JsHint seem to be able to measure it.
But after I ran this command jshint test.js to test the below file

//test.js
$(document).ready(function() {
    $("p").click(function() {
        $(this).hide();
        var x = 5
    });

    function main() {
        return 'Hello, World!';
    }
});

I only get the linting report (test.js: line 4, col 14, Missing semicolon., ...)

How can I do this?

BombShelley
  • 107
  • 9

1 Answers1

2

From the API docs, it looks like you can call JSHINT.data() after linting to get the metrics, which includes measuring complexity:

JSHINT.data()

Generate a report containing details about the most recent invocation of JSHINT.

For example, the following code:

var source = [
  'function goo() {}',
  'foo = 3;'
];
var options = {
  undef: true
};
var predef = {
  foo: false
};

JSHINT(source, options, predef);

console.log(JSHINT.data());

...will produce the following output:

{
  functions: [
    {
      name: 'goo',
      // ...
      metrics: {
        complexity: 1,
        parameters: 0,
        statements: 0
      }
      // ...

This will display the measured complexity.

Note that this is completely separate from the maxcomplexity rule - if enabled, and you set it low enough, and you have a function complex enough, your linting output will include an error.

Community
  • 1
  • 1
CertainPerformance
  • 356,069
  • 52
  • 309
  • 320