Skip to content

Commit d2b3cab

Browse files
committed
Document API
1 parent e166467 commit d2b3cab

File tree

2 files changed

+125
-8
lines changed

2 files changed

+125
-8
lines changed

pages/api.md

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
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+
}

pages/docs.md

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,24 @@ The core project consists of a library itself as well as a CLI program distribut
77
as a Node module.
88

99
More docs: [List of all JSHint options](/docs/options/) · [Command-line
10-
Interface](/docs/cli/) · [Writing your own reporter](/docs/reporters/) ·
11-
[FAQ](/docs/faq/)
10+
Interface](/docs/cli/) · [API](/docs/api/) · [Writing your own
11+
reporter](/docs/reporters/) · [FAQ](/docs/faq/)
1212

1313
### Basic usage
1414

15-
The easiest way to use JSHint is to install it as a Node program. To do so,
16-
simply run the following command in your terminal (flag -g installs JSHint
17-
globally on your system, omit it if you want to install JSHint in the current
18-
working directory):
15+
First, check out [the installation instructions](/install/) for details on
16+
how to install JSHint in your perferred environment. Both the command line
17+
executable and the JavaScript API offer unique ways to configure JSHint's
18+
behaviour. The most common usages are:
1919

20-
$ npm install jshint -g
20+
- [As a command-line tool](/docs/cli/) (via [Node.js](https://nodejs.org))
21+
- [As a JavaScript module](/docs/api/)
2122

22-
After you've done that you should be able to use the `jshint` program.
23+
Regardless of your preferred environment, you can control JSHint's behavior
24+
through specifying any number of [linting options](/docs/options/). In
25+
addition, JSHint will honor any directives declared within the input source
26+
code--see [the section on in-line directives](#inline-configuration) for more
27+
information.
2328

2429
### Configuration
2530

@@ -49,6 +54,8 @@ undefined and unused variables and tell JSHint about a global variable named
4954
"predef": [ "MY_GLOBAL" ]
5055
}
5156

57+
<a name="inline-configuration"></a>
58+
5259
### Inline configuration
5360

5461
In addition to using configuration files you can configure JSHint from within your

0 commit comments

Comments
 (0)