Skip to content

Commit f8dc396

Browse files
43081jfabiospampinato
authored andcommitted
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 f8dc396

File tree

7 files changed

+135
-7
lines changed

7 files changed

+135
-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,7 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`show external options with \`--help\` 1`] = `
4+
" --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>
5+
--foo-string <value> foo description
6+
Defaults to "bar""
7+
`;
+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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+
describe("external options from CLI should work", () => {
18+
runCli("plugin-options-string", [
19+
"--plugin=./plugin.cjs",
20+
"--stdin-filepath",
21+
"example.foo",
22+
"--foo-string",
23+
"baz",
24+
], {
25+
input: "hello-world",
26+
}).test({
27+
stdout: "foo:baz",
28+
stderr: "",
29+
status: 0,
30+
write: [],
31+
});
32+
});
33+
34+
// TODO (43081j): this won't work until we fix #21
35+
describe.skip("external options from config file should work", () => {
36+
runCli("plugin-options-string", [
37+
"--config-path=./config.json",
38+
"--stdin-filepath",
39+
"example.foo",
40+
], {
41+
input: "hello-world",
42+
}).test({
43+
stdout: "foo:baz",
44+
stderr: "",
45+
status: 0,
46+
write: [],
47+
});
48+
});
49+
50+
describe("Non exists plugin", () => {
51+
runCli("plugin-options-string", [
52+
"--plugin=--foo--",
53+
"--stdin-filepath",
54+
"example.foo",
55+
], {
56+
input: "hello-world",
57+
}).test({
58+
stdout: "",
59+
stderr: expect.stringMatching(/The plugin "--foo--" could not be loaded/),
60+
status: 1,
61+
write: [],
62+
});
63+
});

0 commit comments

Comments
 (0)