Skip to content

Commit 6048bfe

Browse files
authored
Docs: documenting public API (fixes #431) (#442)
* Docs: documenting public API * Chore: created script to sync README * Chore: changed subMaxLevel to 3
1 parent 9a4cff3 commit 6048bfe

File tree

5 files changed

+189
-12
lines changed

5 files changed

+189
-12
lines changed

Makefile.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ const path = require("path");
2323
const NODE_MODULES = "./node_modules/",
2424
TEMP_DIR = "./tmp/",
2525
BUILD_DIR = "./build/",
26+
DOCS_DIR = "./docs",
2627

2728
// Utilities - intentional extra space at the end of each string
2829
MOCHA = `${NODE_MODULES}mocha/bin/_mocha `,
@@ -92,9 +93,10 @@ target.test = function() {
9293
};
9394

9495
target.docs = function() {
95-
echo("Generating documentation");
96-
nodeCLI.exec("jsdoc", "-d jsdoc lib");
97-
echo("Documentation has been output to /jsdoc");
96+
echo("Syncing README.md from root with docs/README.md");
97+
rm("-r", `${DOCS_DIR}/README.md`);
98+
cp("README.md", DOCS_DIR);
99+
echo("Done. ");
98100
};
99101

100102
target.browserify = function() {

README.md

Lines changed: 91 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,101 @@ const espree = require("espree");
2323
const ast = espree.parse(code);
2424
```
2525

26-
There is a second argument to `parse()` that allows you to specify various options:
26+
## API
27+
28+
### `parse()`
29+
30+
`parse` parses the given code and returns a abstract syntax tree (AST). It takes two paramenter.
31+
32+
- `code` [string]() - the code which needs to be parsed.
33+
- `options (Optional)` [Object]() - read more about this [here](#options)
2734

2835
```javascript
2936
const espree = require("espree");
3037

31-
// Optional second options argument with the following default settings
32-
const ast = espree.parse(code, {
38+
const ast = espree.parse(code, options);
39+
```
40+
41+
**Example :**
42+
43+
```js
44+
const ast = espree.parse('let foo = "bar"', { ecmaVersion: 6 });
45+
console.log(ast);
46+
```
47+
48+
<details><summary>Output</summary>
49+
<p>
50+
51+
```
52+
Node {
53+
type: 'Program',
54+
start: 0,
55+
end: 15,
56+
body: [
57+
Node {
58+
type: 'VariableDeclaration',
59+
start: 0,
60+
end: 15,
61+
declarations: [Array],
62+
kind: 'let'
63+
}
64+
],
65+
sourceType: 'script'
66+
}
67+
```
68+
69+
</p>
70+
</details>
71+
72+
### `tokenize()`
73+
74+
`tokenize` returns the tokens of a give code. It takes two paramenter.
75+
76+
- `code` [string]() - the code which needs to be parsed.
77+
- `options (Optional)` [Object]() - read more about this [here](#options)
78+
79+
Even if `options` is empty or undefined or `options.tokens` is `false`, it assigns it to `true` in order to get the `tokens` array
80+
81+
**Example :**
82+
83+
```js
84+
const tokens = espree.tokenize('let foo = "bar"', { ecmaVersion: 6 });
85+
console.log(tokens);
86+
```
87+
88+
<details><summary>Output</summary>
89+
<p>
90+
91+
```
92+
Token { type: 'Keyword', value: 'let', start: 0, end: 3 },
93+
Token { type: 'Identifier', value: 'foo', start: 4, end: 7 },
94+
Token { type: 'Punctuator', value: '=', start: 8, end: 9 },
95+
Token { type: 'String', value: '"bar"', start: 10, end: 15 }
96+
```
97+
98+
</p>
99+
</details>
100+
101+
### `version`
102+
103+
Returns the current `espree` version
104+
105+
### `VisitorKeys`
106+
107+
Returns all visitor keys for traversing the AST from [eslint-visitor-keys](https://github.com/eslint/eslint-visitor-keys)
108+
109+
### `latestEcmaVersion`
110+
111+
Returns the latest ECMAScript supported by `espree`
112+
113+
### `supportedEcmaVersions`
114+
115+
Returns an array of all supported ECMAScript version
116+
117+
## Options
33118

119+
```js
120+
const options = {
34121
// attach range information to each node
35122
range: false,
36123

@@ -62,7 +149,7 @@ const ast = espree.parse(code, {
62149
// enable implied strict mode (if ecmaVersion >= 5)
63150
impliedStrict: false
64151
}
65-
});
152+
}
66153
```
67154

68155
## Esprima Compatibility Going Forward

docs/README.md

Lines changed: 91 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,101 @@ const espree = require("espree");
2323
const ast = espree.parse(code);
2424
```
2525

26-
There is a second argument to `parse()` that allows you to specify various options:
26+
## API
27+
28+
### `parse()`
29+
30+
`parse` parses the given code and returns a abstract syntax tree (AST). It takes two paramenter.
31+
32+
- `code` [string]() - the code which needs to be parsed.
33+
- `options (Optional)` [Object]() - read more about this [here](#options)
2734

2835
```javascript
2936
const espree = require("espree");
3037

31-
// Optional second options argument with the following default settings
32-
const ast = espree.parse(code, {
38+
const ast = espree.parse(code, options);
39+
```
40+
41+
**Example :**
42+
43+
```js
44+
const ast = espree.parse('let foo = "bar"', { ecmaVersion: 6 });
45+
console.log(ast);
46+
```
47+
48+
<details><summary>Output</summary>
49+
<p>
50+
51+
```
52+
Node {
53+
type: 'Program',
54+
start: 0,
55+
end: 15,
56+
body: [
57+
Node {
58+
type: 'VariableDeclaration',
59+
start: 0,
60+
end: 15,
61+
declarations: [Array],
62+
kind: 'let'
63+
}
64+
],
65+
sourceType: 'script'
66+
}
67+
```
68+
69+
</p>
70+
</details>
71+
72+
### `tokenize()`
73+
74+
`tokenize` returns the tokens of a give code. It takes two paramenter.
75+
76+
- `code` [string]() - the code which needs to be parsed.
77+
- `options (Optional)` [Object]() - read more about this [here](#options)
78+
79+
Even if `options` is empty or undefined or `options.tokens` is `false`, it assigns it to `true` in order to get the `tokens` array
80+
81+
**Example :**
82+
83+
```js
84+
const tokens = espree.tokenize('let foo = "bar"', { ecmaVersion: 6 });
85+
console.log(tokens);
86+
```
87+
88+
<details><summary>Output</summary>
89+
<p>
90+
91+
```
92+
Token { type: 'Keyword', value: 'let', start: 0, end: 3 },
93+
Token { type: 'Identifier', value: 'foo', start: 4, end: 7 },
94+
Token { type: 'Punctuator', value: '=', start: 8, end: 9 },
95+
Token { type: 'String', value: '"bar"', start: 10, end: 15 }
96+
```
97+
98+
</p>
99+
</details>
100+
101+
### `version`
102+
103+
Returns the current `espree` version
104+
105+
### `VisitorKeys`
106+
107+
Returns all visitor keys for traversing the AST from [eslint-visitor-keys](https://github.com/eslint/eslint-visitor-keys)
108+
109+
### `latestEcmaVersion`
110+
111+
Returns the latest ECMAScript supported by `espree`
112+
113+
### `supportedEcmaVersions`
114+
115+
Returns an array of all supported ECMAScript version
116+
117+
## Options
33118

119+
```js
120+
const options = {
34121
// attach range information to each node
35122
range: false,
36123

@@ -62,7 +149,7 @@ const ast = espree.parse(code, {
62149
// enable implied strict mode (if ecmaVersion >= 5)
63150
impliedStrict: false
64151
}
65-
});
152+
}
66153
```
67154

68155
## Esprima Compatibility Going Forward

docs/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@
135135
name: 'espree',
136136
repo: 'eslint/espree',
137137
loadSidebar: true,
138-
subMaxLevel: 2,
138+
subMaxLevel: 3,
139139
maxLevel: 3,
140140
auto2top: true,
141141
search: {

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
"generate-regex": "node tools/generate-identifier-regex.js",
5353
"test": "npm run-script lint && node Makefile.js test",
5454
"lint": "node Makefile.js lint",
55+
"sync-docs": "node Makefile.js docs",
5556
"browserify": "node Makefile.js browserify",
5657
"generate-release": "eslint-generate-release",
5758
"generate-alpharelease": "eslint-generate-prerelease alpha",

0 commit comments

Comments
 (0)