Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add docs for parser error codes #2491

Merged
merged 6 commits into from
Apr 29, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
29 changes: 29 additions & 0 deletions docs/parser.md
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,35 @@ You should enable these features only if you are using an older version.
- `dts` (`boolean`, default `false`)
This option will enable parsing within a TypeScript ambient context, where certain syntax have different rules (like `.d.ts` files and inside `declare module` blocks). Please see https://www.typescriptlang.org/docs/handbook/declaration-files/introduction.html and https://basarat.gitbook.io/typescript/type-system/intro for more information about ambient contexts.

### Error codes

<details>
<summary>History</summary>
| Version | Changes |
| --- | --- |
| `v7.14.0` | Added error codes |
</details>

Error codes are useful for handling the errors thrown by `@babel/parser`.

There are two error codes, `code` and `reasonCode`.

- `code`
- Rough classification of errors (e.g. `BABEL_PARSER_SYNTAX_ERROR`, `BABEL_PARSER_SOURCETYPE_MODULE_REQUIRED`).
- `reasonCode`
- Detailed classification of errors (e.g. `MissingSemicolon`, `VarRedeclaration`).

Example of using error codes with `errorRecovery`:

```js
const { parse } = require("@babel/parser");

const ast = parse(`a b`, { errorRecovery: true });

console.log(ast.errors[0].code); // BABEL_PARSER_SYNTAX_ERROR
console.log(ast.errors[0].reasonCode); // MissingSemicolon
```

### FAQ

#### Will the Babel parser support a plugin system?
Expand Down