|
| 1 | +{ "title": "JSHint API", "url": "/docs/api/", "template": "docs" } |
| 2 | + |
| 3 | +# Application Programming Interface |
| 4 | + |
| 5 | +JSHint exposes a JavaScript API for programmatic access in environments like |
| 6 | +web browsers and [Node.js](https://nodejs.org). |
| 7 | + |
| 8 | +## `JSHINT( source, options, predef )` |
| 9 | + |
| 10 | +Analyze source code for errors and potential problems. |
| 11 | + |
| 12 | +Parameters: |
| 13 | + |
| 14 | +- `source` |
| 15 | + - Description: The input JavaScript source code |
| 16 | + - Type: string or an array of strings (each element is interpreted as a |
| 17 | + newline) |
| 18 | + - Example: `JSHINT(["'use strict';", "console.log('hello, world!');"]);` |
| 19 | +- `options` |
| 20 | + - Description: The [linting options](/docs/options/) to use when analyzing |
| 21 | + the source code |
| 22 | + - Type: an object whose property names are the desired options to use and |
| 23 | + whose property values are the configuration values for those properties. |
| 24 | + - Example: `JSHINT(mySource, { undef: true });` |
| 25 | +- `predef` |
| 26 | + - Description: variables defined outside of the current file; the behavior of |
| 27 | + this argument is identical to [the `globals` linting |
| 28 | + option](/docs/options#globals) |
| 29 | + - Type: an object whose property names are the global variable identifiers |
| 30 | + and whose property values control whether each variable should be |
| 31 | + considered read-only |
| 32 | + - Example: `JSHINT(mySource, myOptions, { jQuery: false });` |
| 33 | + |
| 34 | +## `JSHINT.errors` |
| 35 | + |
| 36 | +An array of warnings and errors generated by the most recent invocation of |
| 37 | +`JSHINT`. |
| 38 | + |
| 39 | +## `JSHINT.data()` |
| 40 | + |
| 41 | +Generate a report containing details about the most recent invocation of |
| 42 | +`JSHINT`. |
| 43 | + |
| 44 | +For example, the following code: |
| 45 | + |
| 46 | + var source = [ |
| 47 | + 'function goo() {}', |
| 48 | + 'foo = 3;' |
| 49 | + ]; |
| 50 | + var options = { |
| 51 | + undef: true |
| 52 | + }; |
| 53 | + var predef = { |
| 54 | + foo: false |
| 55 | + }; |
| 56 | + |
| 57 | + JSHINT(source, options, predef); |
| 58 | + |
| 59 | + console.log(JSHINT.data()); |
| 60 | + |
| 61 | +...will produce the following output: |
| 62 | + |
| 63 | + { |
| 64 | + functions: [ |
| 65 | + { |
| 66 | + name: 'goo', |
| 67 | + param: undefined, |
| 68 | + line: 1, |
| 69 | + character: 14, |
| 70 | + last: 1, |
| 71 | + lastcharacter: 18, |
| 72 | + metrics: { |
| 73 | + complexity: 1, |
| 74 | + parameters: 0, |
| 75 | + statements: 0 |
| 76 | + } |
| 77 | + } |
| 78 | + ], |
| 79 | + options: { |
| 80 | + undef: true, |
| 81 | + indent: 4, |
| 82 | + maxerr: 50 |
| 83 | + }, |
| 84 | + errors: [ |
| 85 | + { |
| 86 | + id: '(error)', |
| 87 | + raw: 'Read only.', |
| 88 | + code: 'W020', |
| 89 | + evidence: 'foo = 3;', |
| 90 | + line: 2, |
| 91 | + character: 1, |
| 92 | + scope: '(main)', |
| 93 | + a: undefined, |
| 94 | + b: undefined, |
| 95 | + c: undefined, |
| 96 | + d: undefined, |
| 97 | + reason: 'Read only.' |
| 98 | + } |
| 99 | + ], |
| 100 | + globals: [ |
| 101 | + 'goo', |
| 102 | + 'foo' |
| 103 | + ], |
| 104 | + unused: [ |
| 105 | + { |
| 106 | + name: 'goo', |
| 107 | + line: 1, character: 10 |
| 108 | + } |
| 109 | + ] |
| 110 | + } |
0 commit comments