Skip to content

Commit e4370c2

Browse files
authored
Ported test suite: plugin-options-string
- `--help option` is no longer supported at the moment. - Config resolution when using `--stdin-filepath` is not implemented at the moment.
1 parent 6223c0d commit e4370c2

File tree

7 files changed

+156
-7
lines changed

7 files changed

+156
-7
lines changed

src/bin.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
import { toKebabCase } from "kasi";
44
import { bin, color, exit, parseArgv } from "specialist";
55
import { PRETTIER_VERSION, DEFAULT_PARSERS } from "./constants.js";
6-
import { getPlugin, isBoolean, isNumber, isIntegerInRange, isString } from "./utils.js";
6+
import { getPluginOrExit, isBoolean, isNumber, isIntegerInRange, isString } from "./utils.js";
77
import { normalizeOptions, normalizeFormatOptions, normalizePluginOptions } from "./utils.js";
8-
import type { Bin, PluginsOptions } from "./types.js";
8+
import type { Bin, PluginsOptions, PrettierPlugin } from "./types.js";
99

1010
const makeBin = (): Bin => {
1111
return (
@@ -216,7 +216,7 @@ const makePluggableBin = async (): Promise<Bin> => {
216216

217217
for (let i = 0, l = pluginsNames.length; i < l; i++) {
218218
const pluginName = pluginsNames[i];
219-
const plugin = await getPlugin(pluginName);
219+
const plugin = await getPluginOrExit(pluginName);
220220

221221
for (const option in plugin.options) {
222222
optionsNames.push(option);

src/prettier_serial.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { readFile, writeFile } from "atomically";
22
import process from "node:process";
33
import prettier from "prettier/standalone";
4-
import { getPlugins, getPluginsBuiltin, resolve } from "./utils.js";
4+
import { getPluginsOrExit, getPluginsBuiltin, resolve } from "./utils.js";
55
import type { ContextOptions, LazyFormatOptions, PluginsOptions } from "./types.js";
66

77
async function check(
@@ -37,7 +37,7 @@ async function format(
3737
): Promise<string> {
3838
formatOptions = await resolve(formatOptions);
3939
const pluginsBuiltin = await getPluginsBuiltin();
40-
const plugins = await getPlugins(formatOptions.plugins || []);
40+
const plugins = await getPluginsOrExit(formatOptions.plugins || []);
4141
const pluginsOverride = contextOptions.configPrecedence !== "file-override";
4242

4343
const options = {

src/utils.ts

+25-2
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,14 @@ const getPlugin = memoize((name: string): Promise<PrettierPlugin> => {
129129
return plugin;
130130
});
131131

132+
async function getPluginOrExit(name: string): Promise<PrettierPlugin> {
133+
try {
134+
return await getPlugin(name);
135+
} catch {
136+
exit(`The plugin "${name}" could not be loaded`);
137+
}
138+
}
139+
132140
function getPluginPath(name: string): string {
133141
const rootPath = path.join(process.cwd(), "index.js");
134142
const pluginPath = getModulePath(name, rootPath);
@@ -146,9 +154,22 @@ function getPluginVersion(name: string): string | null {
146154
}
147155
}
148156

149-
function getPlugins(names: string[]): PromiseMaybe<PrettierPlugin[]> {
157+
async function getPlugins(names: string[]): Promise<PrettierPlugin[]> {
158+
if (!names.length) return [];
159+
return (
160+
await Promise.all(
161+
names.map((name) => getPlugin(name))
162+
)
163+
);
164+
}
165+
166+
async function getPluginsOrExit(names: string[]): Promise<PrettierPlugin[]> {
150167
if (!names.length) return [];
151-
return Promise.all(names.map(getPlugin));
168+
return (
169+
await Promise.all(
170+
names.map((name) => getPluginOrExit(name))
171+
)
172+
);
152173
}
153174

154175
const getPluginsBuiltin = once(async (): Promise<PrettierPlugin[]> => {
@@ -734,10 +755,12 @@ export {
734755
getModule,
735756
getModulePath,
736757
getPlugin,
758+
getPluginOrExit,
737759
getPluginPath,
738760
getPluginVersion,
739761
getPlugins,
740762
getPluginsBuiltin,
763+
getPluginsOrExit,
741764
getPluginsPaths,
742765
getPluginsVersions,
743766
getProjectPath,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"plugins": ["./plugin.cjs"],
3+
"fooString": "baz"
4+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
"use strict";
2+
3+
module.exports = {
4+
languages: [
5+
{
6+
name: "foo",
7+
parsers: ["foo-parser"],
8+
extensions: [".foo"],
9+
since: "1.0.0",
10+
},
11+
],
12+
options: {
13+
fooString: {
14+
type: "string",
15+
default: "bar",
16+
description: "foo description",
17+
},
18+
},
19+
parsers: {
20+
"foo-parser": {
21+
parse: (text) => ({ text }),
22+
astFormat: "foo-ast",
23+
},
24+
},
25+
printers: {
26+
"foo-ast": {
27+
print: (path, options) =>
28+
options.fooString ? `foo:${options.fooString}` : path.getValue().text,
29+
},
30+
},
31+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`show detailed external option with \`--help foo-string\` (stderr) 1`] = `""`;
4+
5+
exports[`show detailed external option with \`--help foo-string\` (stdout) 1`] = `
6+
"--foo-string <string>
7+
8+
foo description
9+
10+
Default: bar"
11+
`;
12+
13+
exports[`show detailed external option with \`--help foo-string\` (write) 1`] = `[]`;
14+
15+
exports[`show external options with \`--help\` 1`] = `
16+
" --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,foo-parser>
17+
--foo-string <value> foo description
18+
Defaults to "bar""
19+
`;
+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
import { runCli } from "../utils";
2+
3+
test("show external options with `--help`", async () => {
4+
const originalStdout = await runCli("plugin-options-string", ["--help"])
5+
.stdout;
6+
const pluggedStdout = await runCli("plugin-options-string", [
7+
"--help",
8+
"--plugin=./plugin.cjs",
9+
]).stdout;
10+
const originalLines = originalStdout.split("\n");
11+
const pluggedLines = pluggedStdout.split("\n");
12+
const differentLines = pluggedLines.filter((line) =>
13+
!originalLines.includes(line));
14+
expect(differentLines.join("\n")).toMatchSnapshot();
15+
});
16+
17+
// Note (43081j); we don't currently support `--help {optionName}`
18+
describe.skip("show detailed external option with `--help foo-string`", () => {
19+
runCli("plugin-options-string", [
20+
"--plugin=./plugin.cjs",
21+
"--help",
22+
"foo-string",
23+
]).test({
24+
status: 0,
25+
});
26+
});
27+
28+
describe("external options from CLI should work", () => {
29+
runCli(
30+
"plugin-options-string",
31+
[
32+
"--plugin=./plugin.cjs",
33+
"--stdin-filepath",
34+
"example.foo",
35+
"--foo-string",
36+
"baz",
37+
],
38+
{ input: "hello-world" },
39+
).test({
40+
stdout: "foo:baz",
41+
stderr: "",
42+
status: 0,
43+
write: [],
44+
});
45+
});
46+
47+
// TODO (43081j): this won't work until we fix #21
48+
describe.skip("external options from config file should work", () => {
49+
runCli(
50+
"plugin-options-string",
51+
["--config-path=./config.json", "--stdin-filepath", "example.foo"],
52+
{ input: "hello-world" },
53+
).test({
54+
stdout: "foo:baz",
55+
stderr: "",
56+
status: 0,
57+
write: [],
58+
});
59+
});
60+
61+
describe("Non exists plugin", () => {
62+
runCli(
63+
"plugin-options-string",
64+
["--plugin=--foo--", "--stdin-filepath", "example.foo"],
65+
{ input: "hello-world" },
66+
).test({
67+
stdout: "",
68+
stderr: expect.stringMatching(/The plugin "--foo--" could not be loaded/u),
69+
status: 1,
70+
write: [],
71+
});
72+
});

0 commit comments

Comments
 (0)