Skip to content

Commit

Permalink
Docs: documenting public API (fixes #431) (#442)
Browse files Browse the repository at this point in the history
* Docs: documenting public API

* Chore: created script to sync README

* Chore: changed subMaxLevel to 3
  • Loading branch information
anikethsaha committed May 5, 2020
1 parent 9a4cff3 commit 6048bfe
Show file tree
Hide file tree
Showing 5 changed files with 189 additions and 12 deletions.
8 changes: 5 additions & 3 deletions Makefile.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ const path = require("path");
const NODE_MODULES = "./node_modules/",
TEMP_DIR = "./tmp/",
BUILD_DIR = "./build/",
DOCS_DIR = "./docs",

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

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

target.browserify = function() {
Expand Down
95 changes: 91 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,101 @@ const espree = require("espree");
const ast = espree.parse(code);
```

There is a second argument to `parse()` that allows you to specify various options:
## API

### `parse()`

`parse` parses the given code and returns a abstract syntax tree (AST). It takes two paramenter.

- `code` [string]() - the code which needs to be parsed.
- `options (Optional)` [Object]() - read more about this [here](#options)

```javascript
const espree = require("espree");

// Optional second options argument with the following default settings
const ast = espree.parse(code, {
const ast = espree.parse(code, options);
```

**Example :**

```js
const ast = espree.parse('let foo = "bar"', { ecmaVersion: 6 });
console.log(ast);
```

<details><summary>Output</summary>
<p>

```
Node {
type: 'Program',
start: 0,
end: 15,
body: [
Node {
type: 'VariableDeclaration',
start: 0,
end: 15,
declarations: [Array],
kind: 'let'
}
],
sourceType: 'script'
}
```

</p>
</details>

### `tokenize()`

`tokenize` returns the tokens of a give code. It takes two paramenter.

- `code` [string]() - the code which needs to be parsed.
- `options (Optional)` [Object]() - read more about this [here](#options)

Even if `options` is empty or undefined or `options.tokens` is `false`, it assigns it to `true` in order to get the `tokens` array

**Example :**

```js
const tokens = espree.tokenize('let foo = "bar"', { ecmaVersion: 6 });
console.log(tokens);
```

<details><summary>Output</summary>
<p>

```
Token { type: 'Keyword', value: 'let', start: 0, end: 3 },
Token { type: 'Identifier', value: 'foo', start: 4, end: 7 },
Token { type: 'Punctuator', value: '=', start: 8, end: 9 },
Token { type: 'String', value: '"bar"', start: 10, end: 15 }
```

</p>
</details>

### `version`

Returns the current `espree` version

### `VisitorKeys`

Returns all visitor keys for traversing the AST from [eslint-visitor-keys](https://github.com/eslint/eslint-visitor-keys)

### `latestEcmaVersion`

Returns the latest ECMAScript supported by `espree`

### `supportedEcmaVersions`

Returns an array of all supported ECMAScript version

## Options

```js
const options = {
// attach range information to each node
range: false,

Expand Down Expand Up @@ -62,7 +149,7 @@ const ast = espree.parse(code, {
// enable implied strict mode (if ecmaVersion >= 5)
impliedStrict: false
}
});
}
```

## Esprima Compatibility Going Forward
Expand Down
95 changes: 91 additions & 4 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,101 @@ const espree = require("espree");
const ast = espree.parse(code);
```

There is a second argument to `parse()` that allows you to specify various options:
## API

### `parse()`

`parse` parses the given code and returns a abstract syntax tree (AST). It takes two paramenter.

- `code` [string]() - the code which needs to be parsed.
- `options (Optional)` [Object]() - read more about this [here](#options)

```javascript
const espree = require("espree");

// Optional second options argument with the following default settings
const ast = espree.parse(code, {
const ast = espree.parse(code, options);
```

**Example :**

```js
const ast = espree.parse('let foo = "bar"', { ecmaVersion: 6 });
console.log(ast);
```

<details><summary>Output</summary>
<p>

```
Node {
type: 'Program',
start: 0,
end: 15,
body: [
Node {
type: 'VariableDeclaration',
start: 0,
end: 15,
declarations: [Array],
kind: 'let'
}
],
sourceType: 'script'
}
```

</p>
</details>

### `tokenize()`

`tokenize` returns the tokens of a give code. It takes two paramenter.

- `code` [string]() - the code which needs to be parsed.
- `options (Optional)` [Object]() - read more about this [here](#options)

Even if `options` is empty or undefined or `options.tokens` is `false`, it assigns it to `true` in order to get the `tokens` array

**Example :**

```js
const tokens = espree.tokenize('let foo = "bar"', { ecmaVersion: 6 });
console.log(tokens);
```

<details><summary>Output</summary>
<p>

```
Token { type: 'Keyword', value: 'let', start: 0, end: 3 },
Token { type: 'Identifier', value: 'foo', start: 4, end: 7 },
Token { type: 'Punctuator', value: '=', start: 8, end: 9 },
Token { type: 'String', value: '"bar"', start: 10, end: 15 }
```

</p>
</details>

### `version`

Returns the current `espree` version

### `VisitorKeys`

Returns all visitor keys for traversing the AST from [eslint-visitor-keys](https://github.com/eslint/eslint-visitor-keys)

### `latestEcmaVersion`

Returns the latest ECMAScript supported by `espree`

### `supportedEcmaVersions`

Returns an array of all supported ECMAScript version

## Options

```js
const options = {
// attach range information to each node
range: false,

Expand Down Expand Up @@ -62,7 +149,7 @@ const ast = espree.parse(code, {
// enable implied strict mode (if ecmaVersion >= 5)
impliedStrict: false
}
});
}
```

## Esprima Compatibility Going Forward
Expand Down
2 changes: 1 addition & 1 deletion docs/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@
name: 'espree',
repo: 'eslint/espree',
loadSidebar: true,
subMaxLevel: 2,
subMaxLevel: 3,
maxLevel: 3,
auto2top: true,
search: {
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
"generate-regex": "node tools/generate-identifier-regex.js",
"test": "npm run-script lint && node Makefile.js test",
"lint": "node Makefile.js lint",
"sync-docs": "node Makefile.js docs",
"browserify": "node Makefile.js browserify",
"generate-release": "eslint-generate-release",
"generate-alpharelease": "eslint-generate-prerelease alpha",
Expand Down

0 comments on commit 6048bfe

Please sign in to comment.