Application Programming Interface

JSHint exposes a JavaScript API for programmatic access in environments like web browsers and Node.js.

JSHINT( source, options, predef )

Analyze source code for errors and potential problems.

Parameters:

  • source
    • Description: The input JavaScript source code
    • Type: string or an array of strings (each element is interpreted as a newline)
    • Example: JSHINT(["'use strict';", "console.log('hello, world!');"]);
  • options
    • Description: The linting options to use when analyzing the source code
    • Type: an object whose property names are the desired options to use and whose property values are the configuration values for those properties.
    • Example: JSHINT(mySource, { undef: true });
  • predef
    • Description: variables defined outside of the current file; the behavior of this argument is identical to the globals linting option
    • Type: an object whose property names are the global variable identifiers and whose property values control whether each variable should be considered read-only
    • Example: JSHINT(mySource, myOptions, { jQuery: false });

JSHINT.errors

An array of warnings and errors generated by the most recent invocation of JSHINT.

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',
      param: undefined,
      line: 1,
      character: 14,
      last: 1,
      lastcharacter: 18,
      metrics: {
        complexity: 1,
        parameters: 0,
        statements: 0
      }
    }
  ],
  options: {
    undef: true,
    indent: 4,
    maxerr: 50
  },
  errors: [
    {
      id: '(error)',
      raw: 'Read only.',
      code: 'W020',
      evidence: 'foo = 3;',
      line: 2,
      character: 1,
      scope: '(main)',
      a: undefined,
      b: undefined,
      c: undefined,
      d: undefined,
      reason: 'Read only.'
    }
  ],
  globals: [
    'goo',
    'foo'
  ],
  unused: [
    {
      name: 'goo',
      line: 1, character: 10
    }
  ]
}