Remove support for custom parser api (#13250)

Co-authored-by: fisker Cheung <lionkay@gmail.com>
Co-authored-by: Georgii Dolzhykov <thorn.mailbox@gmail.com>
This commit is contained in:
fisker CheungandGeorgii Dolzhykov authored and GitHub committed 2022-08-08 01:09:10 +08:00
1 parent 3e9e9ddec7
commit 0b9cb96f58
11 files changed
+56 -171

No files matched your search

+3
View File
@@ -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
View File
@@ -156,6 +156,7 @@
"Joar", "Joar",
"josephfrazier", "josephfrazier",
"jscodefmt", "jscodefmt",
"jscodeshift",
"jsesc", "jsesc",
"jsfmt", "jsfmt",
"jsonata", "jsonata",
+39 -9
View File
@@ -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)
_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’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 ```js
(text: string, parsers: object, options: object) => AST; import { format } from "prettier";
```
Prettier’s built-in parsers are exposed as properties on the `parsers` argument. format("lodash ( )", {
```js
prettier.format("lodash ( )", {
parser(text, { babel }) { parser(text, { babel }) {
const ast = babel(text); const ast = babel(text);
ast.program.body[0].expression.callee.name = "_"; ast.program.body[0].expression.callee.name = "_";
@@ -147,4 +149,32 @@ prettier.format("lodash ( )", {
// -> "_();\n" // -> "_();\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).
+5 -5
View File
@@ -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_ - `"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_ - `"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 |
| ------- | ------------------- | -------------------- |
| Default | CLI Override | API Override | | None | `--parser <string>` | `parser: "<string>"` |
| ------- | ----------------------------------------------- | ---------------------------------------------------------- |
| None | `--parser <string>`<br />`--parser ./my-parser` | `parser: "<string>"`<br />`parser: require("./my-parser")` |
Note: the default value was `"babylon"` until v1.13.0. 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> <a name="filepath"></a>
## File Path ## File Path
-5
View File
@@ -1,7 +1,6 @@
import path from "node:path"; import path from "node:path";
import { createRequire } from "node:module"; import { createRequire } from "node:module";
import createEsmUtils from "esm-utils"; import createEsmUtils from "esm-utils";
import { PROJECT_ROOT } from "../utils/index.mjs";
const { require, dirname } = createEsmUtils(import.meta); const { require, dirname } = createEsmUtils(import.meta);
@@ -324,10 +323,6 @@ const coreBundles = [
path: path.join(dirname, "./shims/chalk.js"), path: path.join(dirname, "./shims/chalk.js"),
}, },
replaceDiffPackageEntry("lib/diff/array.js"), replaceDiffPackageEntry("lib/diff/array.js"),
{
module: path.join(PROJECT_ROOT, "src/main/load-parser.js"),
text: "export default () => {};",
},
], ],
}, },
{ {
-25
View File
@@ -1,25 +0,0 @@
// This module only works in Node.js
// Will be replaced when bundling `standalone.js`
import { createRequire } from "node:module";
import path from "node:path";
import { ConfigError } from "../common/errors.js";
import { locStart, locEnd } from "../language-js/loc.js";
const require = createRequire(import.meta.url);
function requireParser(parser) {
try {
return {
parse: require(path.resolve(process.cwd(), parser)),
astFormat: "estree",
locStart,
locEnd,
};
} catch {
/* istanbul ignore next */
throw new ConfigError(`Couldn't resolve parser "${parser}"`);
}
}
export default requireParser;
+8 -24
View File
@@ -1,7 +1,5 @@
import { createRequire } from "node:module"; import { createRequire } from "node:module";
import { ConfigError } from "../common/errors.js"; 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); const require = createRequire(import.meta.url);
@@ -26,30 +24,16 @@ function getParsers(options) {
return parsers; return parsers;
} }
function resolveParser(opts, parsers = getParsers(opts)) { function resolveParser(options, parsers = getParsers(options)) {
if (typeof opts.parser === "function") { if (Object.prototype.hasOwnProperty.call(parsers, options.parser)) {
// Custom parser API always works with JavaScript. return parsers[options.parser];
return {
parse: opts.parser,
astFormat: "estree",
locStart,
locEnd,
};
} }
if (typeof opts.parser === "string") { /* istanbul ignore next */
if (Object.prototype.hasOwnProperty.call(parsers, opts.parser)) { if (process.env.PRETTIER_TARGET === "universal") {
return parsers[opts.parser]; throw new ConfigError(
} `Couldn't resolve parser "${options.parser}". Parsers must be explicitly added to the standalone bundle.`
);
/* 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);
} }
} }
@@ -1,11 +0,0 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`allows passing a string to resolve a parser (stderr) 1`] = `""`;
exports[`allows passing a string to resolve a parser (stdout) 1`] = `
"/* eslint-disable */
bar();
"
`;
exports[`allows passing a string to resolve a parser (write) 1`] = `[]`;
-83
View File
@@ -1,83 +0,0 @@
import prettier from "../../config/prettier-entry.js";
test("allows custom parser provided as object", async () => {
const output = await prettier.format("1", {
parser(text) {
expect(text).toBe("1");
return {
type: "Literal",
value: 2,
raw: "2",
};
},
});
expect(output).toBe("2");
});
test("allows usage of prettier's supported parsers", async () => {
const output = await prettier.format("foo ( )", {
parser(text, parsers) {
expect(typeof parsers.babel).toBe("function");
const ast = parsers.babel(text);
ast.program.body[0].expression.callee.name = "bar";
return ast;
},
});
expect(output).toBe("bar();\n");
});
test("parsers should allow omit optional arguments", async () => {
let parsers;
try {
await prettier.format("{}", {
parser(text, builtinParsers) {
parsers = builtinParsers;
},
});
} catch {
// noop
}
expect(typeof parsers.babel).toBe("function");
const code = {
graphql: "type A {hero: Character}",
default: "{}",
};
for (const [name, parse] of Object.entries(parsers)) {
// Private parser should not be used by users
if (name.startsWith("__")) {
continue;
}
expect(() => parse(code[name] || code.default)).not.toThrow();
}
});
test("allows add empty `trailingComments` array", async () => {
const output = await prettier.format("(foo /* comment */)( )", {
parser(text, parsers) {
const ast = parsers.babel(text);
// This may happens in this use case https://github.com/prettier/prettier/pull/5497#issuecomment-439841965
// Simply simulate it
const callExpression = ast.program.body[0].expression;
callExpression.extra = { parenthesized: true };
callExpression.trailingComments = [];
return ast;
},
});
expect(output).toBe("foo(/* comment */);\n");
});
describe("allows passing a string to resolve a parser", () => {
runPrettier("./custom-parsers/", [
"--end-of-line",
"lf",
"./custom-rename-input.js",
"--parser",
"./custom-rename-parser.cjs",
]).test({
status: 0,
});
});
@@ -1,2 +0,0 @@
/* eslint-disable */
foo ( )
@@ -1,7 +0,0 @@
"use strict";
module.exports = function (text, parsers) {
const ast = parsers.babel(text);
ast.program.body[0].expression.callee.name = "bar";
return ast;
};