Skip to content

Commit

Permalink
Custom parser API: deprecation notice (prettier#13252)
Browse files Browse the repository at this point in the history
  • Loading branch information
thorn0 committed Aug 7, 2022
1 parent 2c77149 commit 67efa96
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 18 deletions.
1 change: 1 addition & 0 deletions cspell.json
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@
"Joar",
"josephfrazier",
"jscodefmt",
"jscodeshift",
"jsesc",
"jsfmt",
"jsonata",
Expand Down
43 changes: 35 additions & 8 deletions docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -132,18 +132,19 @@ The support information looks like this:
}
```

## Custom Parser API
<a name="custom-parser-api"></a>

If you need to make modifications to the AST (such as codemods), or you want to provide an alternate parser, you can do so by setting the `parser` option to a function. The function signature of the parser function is:
## Custom Parser API (deprecated)

```js
(text: string, parsers: object, options: object) => AST;
```
_Will be removed in v3.0.0 (superseded by the Plugin API)_

Before [plugins](plugins.md) were a thing, Prettier had a similar but more limited feature called custom parsers. It will be removed in v3.0.0 as its functionality is a subset of what the Plugin API does. If you used it, please check the example below on how to migrate.

Prettier’s built-in parsers are exposed as properties on the `parsers` argument.
❌ Custom parser API (deprecated):

```js
prettier.format("lodash ( )", {
import { format } from "prettier";
format("lodash ( )", {
parser(text, { babel }) {
const ast = babel(text);
ast.program.body[0].expression.callee.name = "_";
Expand All @@ -153,4 +154,30 @@ prettier.format("lodash ( )", {
// -> "_();\n"
```

The `--parser` CLI option may be a path to a node.js module exporting a parse function.
✔️ Plugin API:

```js
import { format } from "prettier";
import parserBabel from "prettier/parser-babel.js";
const myCustomPlugin = {
parsers: {
"my-custom-parser": {
parse(text) {
const ast = parserBabel.parsers.babel.parse(text);
ast.program.body[0].expression.callee.name = "_";
return ast;
},
astFormat: "estree",
},
},
};
format("lodash ( )", {
parser: "my-custom-parser",
plugins: [myCustomPlugin],
});
// -> "_();\n"
```

> Note: Overall, doing codemods this way isn’t recommended. Prettier uses the location data of AST nodes for many things like preserving blank lines and attaching comments. When the AST is modified after the parsing, the location data often gets out of sync, which may lead to unpredictable results. Consider using [jscodeshift](https://github.com/facebook/jscodeshift) if you need codemods.
As part of the deprecated Custom parser API, it is possible to pass a path to a module exporting a `parse` function via the `--parser` option. Use the `--plugin` CLI option or the `plugins` API option instead to [load plugins](plugins.md#using-plugins).
2 changes: 1 addition & 1 deletion docs/options.md
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ Valid options:
- `"lwc"` (same parser as `"html"`, but also formats LWC-specific syntax for unquoted template attributes) _First available in 1.17.0_
- `"yaml"` (via [yaml](https://github.com/eemeli/yaml) and [yaml-unist-parser](https://github.com/ikatyang/yaml-unist-parser)) _First available in 1.14.0_

[Custom parsers](api.md#custom-parser-api) are also supported. _First available in v1.5.0_
[Custom parsers](api.md#custom-parser-api) are also supported. _First available in v1.5.0. Deprecated. Will be removed in v3.0.0 (superseded by the Plugin API)_

| Default | CLI Override | API Override |
| ------- | ----------------------------------------------- | ---------------------------------------------------------- |
Expand Down
43 changes: 35 additions & 8 deletions website/versioned_docs/version-stable/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -133,18 +133,19 @@ The support information looks like this:
}
```

## Custom Parser API
<a name="custom-parser-api"></a>

If you need to make modifications to the AST (such as codemods), or you want to provide an alternate parser, you can do so by setting the `parser` option to a function. The function signature of the parser function is:
## Custom Parser API (deprecated)

```js
(text: string, parsers: object, options: object) => AST;
```
_Will be removed in v3.0.0 (superseded by the Plugin API)_

Before [plugins](plugins.md) were a thing, Prettier had a similar but more limited feature called custom parsers. It will be removed in v3.0.0 as its functionality is a subset of what the Plugin API does. If you used it, please check the example below on how to migrate.

Prettier’s built-in parsers are exposed as properties on the `parsers` argument.
❌ Custom parser API (deprecated):

```js
prettier.format("lodash ( )", {
import { format } from "prettier";
format("lodash ( )", {
parser(text, { babel }) {
const ast = babel(text);
ast.program.body[0].expression.callee.name = "_";
Expand All @@ -154,4 +155,30 @@ prettier.format("lodash ( )", {
// -> "_();\n"
```

The `--parser` CLI option may be a path to a node.js module exporting a parse function.
✔️ Plugin API:

```js
import { format } from "prettier";
import parserBabel from "prettier/parser-babel.js";
const myCustomPlugin = {
parsers: {
"my-custom-parser": {
parse(text) {
const ast = parserBabel.parsers.babel.parse(text);
ast.program.body[0].expression.callee.name = "_";
return ast;
},
astFormat: "estree",
},
},
};
format("lodash ( )", {
parser: "my-custom-parser",
plugins: [myCustomPlugin],
});
// -> "_();\n"
```

> Note: Overall, doing codemods this way isn’t recommended. Prettier uses the location data of AST nodes for many things like preserving blank lines and attaching comments. When the AST is modified after the parsing, the location data often gets out of sync, which may lead to unpredictable results. Consider using [jscodeshift](https://github.com/facebook/jscodeshift) if you need codemods.
As part of the deprecated Custom parser API, it is possible to pass a path to a module exporting a `parse` function via the `--parser` option. Use the `--plugin` CLI option or the `plugins` API option instead to [load plugins](plugins.md#using-plugins).
2 changes: 1 addition & 1 deletion website/versioned_docs/version-stable/options.md
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ Valid options:
- `"lwc"` (same parser as `"html"`, but also formats LWC-specific syntax for unquoted template attributes) _First available in 1.17.0_
- `"yaml"` (via [yaml](https://github.com/eemeli/yaml) and [yaml-unist-parser](https://github.com/ikatyang/yaml-unist-parser)) _First available in 1.14.0_

[Custom parsers](api.md#custom-parser-api) are also supported. _First available in v1.5.0_
[Custom parsers](api.md#custom-parser-api) are also supported. _First available in v1.5.0. Deprecated. Will be removed in v3.0.0 (superseded by the Plugin API)_

| Default | CLI Override | API Override |
| ------- | ----------------------------------------------- | ---------------------------------------------------------- |
Expand Down

0 comments on commit 67efa96

Please sign in to comment.