Skip to content

Commit

Permalink
Add docs for parser error codes (#2491)
Browse files Browse the repository at this point in the history
Co-authored-by: Brian Ng <bng412@gmail.com>
  • Loading branch information
sosukesuzuki and existentialism committed Apr 29, 2021
1 parent f4ed4b3 commit ffc40b0
Showing 1 changed file with 29 additions and 0 deletions.
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

0 comments on commit ffc40b0

Please sign in to comment.