Skip to content

Commit 4b0f870

Browse files
43081jfabiospampinato
authored andcommitted
Ensuring that plugin-provided parsers are listed in the help page
1 parent 59e8aea commit 4b0f870

2 files changed

Lines changed: 45 additions & 4 deletions

File tree

src/bin.ts

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import { toKebabCase } from "kasi";
44
import { bin, color, exit, parseArgv } from "specialist";
5-
import { PRETTIER_VERSION } from "./constants.js";
5+
import { PRETTIER_VERSION, DEFAULT_PARSERS } from "./constants.js";
66
import { getPlugin, isBoolean, isNumber, isIntegerInRange, isString } from "./utils.js";
77
import { normalizeOptions, normalizeFormatOptions, normalizePluginOptions } from "./utils.js";
88
import type { Bin, PluginsOptions } from "./types.js";
@@ -63,9 +63,9 @@ const makeBin = (): Bin => {
6363
.option("--jsx-single-quote", 'Use single quotes in JSX\nDefaults to "false"', {
6464
section: "Format",
6565
})
66-
.option("--parser <flow|babel|babel-flow|babel-ts|typescript|acorn|espree|meriyah|css|less|scss|json|json5|json-stringify|graphql|markdown|mdx|vue|yaml|glimmer|html|angular|lwc>", "Which parser to use", {
66+
.option(`--parser <${DEFAULT_PARSERS.join('|')}>`, "Which parser to use", {
6767
section: "Format",
68-
enum: ["flow", "babel", "babel-flow", "babel-ts", "typescript", "acorn", "espree", "meriyah", "css", "less", "scss", "json", "json5", "json-stringify", "graphql", "markdown", "mdx", "vue", "yaml", "glimmer", "html", "angular", "lwc"],
68+
enum: DEFAULT_PARSERS,
6969
})
7070
.option("--print-width <int>", 'The line length where Prettier will try wrap\nDefaults to "80"', {
7171
section: "Format",
@@ -203,6 +203,7 @@ const makePluggableBin = async (): Promise<Bin> => {
203203
const formatOptions = normalizeFormatOptions(args);
204204
const pluginsDefaultOptions: PluginsOptions = {};
205205
const pluginsNames = formatOptions.plugins || [];
206+
const pluginsParsers = new Set<string>();
206207
const optionsNames: string[] = [];
207208

208209
for (let i = 0, l = pluginsNames.length; i < l; i++) {
@@ -252,6 +253,20 @@ const makePluggableBin = async (): Promise<Bin> => {
252253
bin = bin.option(`--${toKebabCase(option)} ${args}`, description, { deprecated, section, enum: values });
253254
}
254255
}
256+
257+
if (plugin.parsers) {
258+
for (const parserName of Object.keys(plugin.parsers)) {
259+
pluginsParsers.add(parserName);
260+
}
261+
}
262+
}
263+
264+
if (pluginsParsers.size) {
265+
const parsers = [...DEFAULT_PARSERS, ...pluginsParsers];
266+
bin.option(`--parser <${parsers}>`, "Which parser to use", {
267+
section: "Format",
268+
enum: parsers,
269+
});
255270
}
256271

257272
bin = bin.action(async (options, files) => {

src/constants.ts

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,30 @@ const cliPackage = require("@prettier/cli/package.json");
77
const PRETTIER_VERSION = prettierPackage.version;
88
const CLI_VERSION = cliPackage.version;
99

10-
export { PRETTIER_VERSION, CLI_VERSION };
10+
const DEFAULT_PARSERS = [
11+
"flow",
12+
"babel",
13+
"babel-flow",
14+
"babel-ts",
15+
"typescript",
16+
"acorn",
17+
"espree",
18+
"meriyah",
19+
"css",
20+
"less",
21+
"scss",
22+
"json",
23+
"json5",
24+
"json-stringify",
25+
"graphql",
26+
"markdown",
27+
"mdx",
28+
"vue",
29+
"yaml",
30+
"glimmer",
31+
"html",
32+
"angular",
33+
"lwc",
34+
];
35+
36+
export { PRETTIER_VERSION, CLI_VERSION, DEFAULT_PARSERS };

0 commit comments

Comments
 (0)