Skip to content

Commit

Permalink
Remove support for custom parser api (#13250)
Browse files Browse the repository at this point in the history
Co-authored-by: fisker Cheung <lionkay@gmail.com>
Co-authored-by: Georgii Dolzhykov <thorn.mailbox@gmail.com>
  • Loading branch information
fisker and thorn0 committed Aug 7, 2022
1 parent 3e9e9dd commit 0b9cb96
Show file tree
Hide file tree
Showing 11 changed files with 55 additions and 170 deletions.
3 changes: 3 additions & 0 deletions changelog_unreleased/api/13250.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#### [BREAKING] Removed support for custom parser api (#13250 by @fisker and @thorn0)

Before [plugins](/docs/en/plugins.html) were a thing, Prettier had a similar but more limited feature called custom parsers. It’s been removed in v3.0.0 as its functionality was a subset of what the Plugin API did. If you used it, please check [how to migrate](/docs/en/api.html#custom-parser-api).
1 change: 1 addition & 0 deletions cspell.json
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@
"Joar",
"josephfrazier",
"jscodefmt",
"jscodeshift",
"jsesc",
"jsfmt",
"jsonata",
Expand Down
46 changes: 38 additions & 8 deletions docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -126,18 +126,20 @@ 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 (removed)

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

Prettier’s built-in parsers are exposed as properties on the `parsers` argument.
Before [plugins](plugins.md) were a thing, Prettier had a similar but more limited feature called custom parsers. It’s been removed in v3.0.0 as its functionality was a subset of what the Plugin API did. If you used it, please check the example below on how to migrate.

❌ Custom parser API (removed):

```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 @@ -147,4 +149,32 @@ 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": {
async parse(text) {
const ast = await parserBabel.parsers.babel.parse(text);
ast.program.body[0].expression.callee.name = "_";
return ast;
},
astFormat: "estree",
},
},
};

await 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 removed Custom parser API, it was previously 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).
10 changes: 5 additions & 5 deletions docs/options.md
Original file line number Diff line number Diff line change
Expand Up @@ -281,14 +281,14 @@ 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_

| Default | CLI Override | API Override |
| ------- | ----------------------------------------------- | ---------------------------------------------------------- |
| None | `--parser <string>`<br />`--parser ./my-parser` | `parser: "<string>"`<br />`parser: require("./my-parser")` |
| Default | CLI Override | API Override |
| ------- | ------------------- | -------------------- |
| None | `--parser <string>` | `parser: "<string>"` |

Note: the default value was `"babylon"` until v1.13.0.

Note: the Custom parser API has been removed in v3.0.0. Use [plugins](plugins.md) instead ([how to migrate](api.md#custom-parser-api)).

<a name="filepath"></a>

## File Path
Expand Down
5 changes: 0 additions & 5 deletions scripts/build/config.mjs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import path from "node:path";
import { createRequire } from "node:module";
import createEsmUtils from "esm-utils";
import { PROJECT_ROOT } from "../utils/index.mjs";

const { require, dirname } = createEsmUtils(import.meta);

Expand Down Expand Up @@ -324,10 +323,6 @@ const coreBundles = [
path: path.join(dirname, "./shims/chalk.js"),
},
replaceDiffPackageEntry("lib/diff/array.js"),
{
module: path.join(PROJECT_ROOT, "src/main/load-parser.js"),
text: "export default () => {};",
},
],
},
{
Expand Down
25 changes: 0 additions & 25 deletions src/main/load-parser.js

This file was deleted.

32 changes: 8 additions & 24 deletions src/main/parser.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import { createRequire } from "node:module";
import { ConfigError } from "../common/errors.js";
import { locStart, locEnd } from "../language-js/loc.js";
import loadParser from "./load-parser.js";

const require = createRequire(import.meta.url);

Expand All @@ -26,30 +24,16 @@ function getParsers(options) {
return parsers;
}

function resolveParser(opts, parsers = getParsers(opts)) {
if (typeof opts.parser === "function") {
// Custom parser API always works with JavaScript.
return {
parse: opts.parser,
astFormat: "estree",
locStart,
locEnd,
};
function resolveParser(options, parsers = getParsers(options)) {
if (Object.prototype.hasOwnProperty.call(parsers, options.parser)) {
return parsers[options.parser];
}

if (typeof opts.parser === "string") {
if (Object.prototype.hasOwnProperty.call(parsers, opts.parser)) {
return parsers[opts.parser];
}

/* istanbul ignore next */
if (process.env.PRETTIER_TARGET === "universal") {
throw new ConfigError(
`Couldn't resolve parser "${opts.parser}". Parsers must be explicitly added to the standalone bundle.`
);
}

return loadParser(opts.parser);
/* istanbul ignore next */
if (process.env.PRETTIER_TARGET === "universal") {
throw new ConfigError(
`Couldn't resolve parser "${options.parser}". Parsers must be explicitly added to the standalone bundle.`
);
}
}

Expand Down
11 changes: 0 additions & 11 deletions tests/integration/__tests__/__snapshots__/parser-api.js.snap

This file was deleted.

83 changes: 0 additions & 83 deletions tests/integration/__tests__/parser-api.js

This file was deleted.

2 changes: 0 additions & 2 deletions tests/integration/custom-parsers/custom-rename-input.js

This file was deleted.

7 changes: 0 additions & 7 deletions tests/integration/custom-parsers/custom-rename-parser.cjs

This file was deleted.

0 comments on commit 0b9cb96

Please sign in to comment.