Skip to content

Commit c9533e7

Browse files
committed
Merge branch 'main' into stdin-filepath-test-fixes
2 parents 585f1be + e66a9b5 commit c9533e7

16 files changed

Lines changed: 389 additions & 14 deletions

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"repository": "github:prettier/prettier-cli",
44
"description": "A faster CLI for Prettier.",
55
"license": "MIT",
6-
"version": "0.7.0",
6+
"version": "0.7.1",
77
"type": "module",
88
"main": "dist/index.js",
99
"types": "./dist/index.d.ts",

src/bin.ts

Lines changed: 11 additions & 3 deletions
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 (
@@ -53,6 +53,10 @@ const makeBin = (): Bin => {
5353
section: "Format",
5454
enum: ["lf", "crlf", "cr", "auto"],
5555
})
56+
.option("--experimental-operator-position <start|end>", 'Where to print operators when binary expressions wrap lines\nDefaults to "end"', {
57+
section: "Format",
58+
enum: ["start", "end"],
59+
})
5660
.option("--experimental-ternaries", 'Use curious ternaries, with the question mark after the condition\nDefaults to "false"', {
5761
section: "Format",
5862
})
@@ -63,6 +67,10 @@ const makeBin = (): Bin => {
6367
.option("--jsx-single-quote", 'Use single quotes in JSX\nDefaults to "false"', {
6468
section: "Format",
6569
})
70+
.option("--object-wrap <preserve|collapse>", 'How to wrap object literals\nDefaults to "preserve"', {
71+
section: "Format",
72+
enum: ["preserve", "collapse"],
73+
})
6674
.option(`--parser <${DEFAULT_PARSERS.join('|')}>`, "Which parser to use", {
6775
section: "Format",
6876
enum: DEFAULT_PARSERS,
@@ -208,7 +216,7 @@ const makePluggableBin = async (): Promise<Bin> => {
208216

209217
for (let i = 0, l = pluginsNames.length; i < l; i++) {
210218
const pluginName = pluginsNames[i];
211-
const plugin = await getPlugin(pluginName);
219+
const plugin = await getPluginOrExit(pluginName);
212220

213221
for (const option in plugin.options) {
214222
optionsNames.push(option);

src/prettier_plugins_builtin.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,22 @@ const options = {
106106
description: "Print semicolons.",
107107
oppositeDescription: "Do not print semicolons, except at the beginning of lines which may need them.",
108108
},
109+
experimentalOperatorPosition: {
110+
category: "JavaScript",
111+
type: "choice",
112+
default: "end",
113+
description: "Where to print operators when binary expressions wrap lines.",
114+
choices: [
115+
{
116+
value: "start",
117+
description: "Print operators at the start of new lines.",
118+
},
119+
{
120+
value: "end",
121+
description: "Print operators at the end of previous lines.",
122+
},
123+
],
124+
},
109125
experimentalTernaries: {
110126
category: "JavaScript",
111127
type: "boolean",
@@ -170,6 +186,22 @@ const options = {
170186
{ value: "preserve", description: "Wrap prose as-is." },
171187
],
172188
},
189+
objectWrap: {
190+
category: "Common",
191+
type: "choice",
192+
default: "preserve",
193+
description: "How to wrap object literals.",
194+
choices: [
195+
{
196+
value: "preserve",
197+
description: "Keep as multi-line, if there is a newline between the opening brace and first property.",
198+
},
199+
{
200+
value: "collapse",
201+
description: "Fit to a single line when possible.",
202+
},
203+
],
204+
},
173205
};
174206

175207
const languages = [

src/prettier_serial.ts

Lines changed: 2 additions & 2 deletions
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/types.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ type ContextOptions = {
99

1010
type FormatOptions = {
1111
[pluginOption: string]: unknown;
12+
experimentalOperatorPosition?: "start" | "end";
1213
experimentalTernaries?: boolean;
1314
arrowParens?: "avoid" | "always";
1415
bracketSameLine?: boolean;
@@ -18,6 +19,7 @@ type FormatOptions = {
1819
htmlWhitespaceSensitivity?: "css" | "strict" | "ignore";
1920
insertPragma?: boolean;
2021
jsxSingleQuote?: boolean;
22+
objectWrap?: "preserve" | "collapse";
2123
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"; // prettier-ignore
2224
plugins?: string[];
2325
printWidth?: number;

src/utils.ts

Lines changed: 34 additions & 5 deletions
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,14 @@ 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 await Promise.all(names.map((name) => getPlugin(name)));
160+
}
161+
162+
async function getPluginsOrExit(names: string[]): Promise<PrettierPlugin[]> {
150163
if (!names.length) return [];
151-
return Promise.all(names.map(getPlugin));
164+
return await Promise.all(names.map((name) => getPluginOrExit(name)));
152165
}
153166

154167
const getPluginsBuiltin = once(async (): Promise<PrettierPlugin[]> => {
@@ -431,6 +444,13 @@ function normalizeFormatOptions(options: unknown): FormatOptions {
431444

432445
const formatOptions: FormatOptions = {};
433446

447+
if ("experimentalOperatorPosition" in options) {
448+
const value = options.experimentalOperatorPosition;
449+
if (value === "start" || value === "end") {
450+
formatOptions.experimentalOperatorPosition = value;
451+
}
452+
}
453+
434454
if ("experimentalTernaries" in options) {
435455
const value = options.experimentalTernaries;
436456
if (isBoolean(value)) {
@@ -494,11 +514,18 @@ function normalizeFormatOptions(options: unknown): FormatOptions {
494514
}
495515
}
496516

517+
if ("objectWrap" in options) {
518+
const value = options.objectWrap;
519+
if (value === "preserve" || value === "collapse") {
520+
formatOptions.objectWrap = value;
521+
}
522+
}
523+
497524
if ("parser" in options) {
498525
const value = options.parser;
499-
// prettier-ignore
500-
if (value === "flow" || value === "babel" || value === "babel-flow" || value === "babel-ts" || value === "typescript" || value === "acorn" || value === "espree" || value === "meriyah" || value === "css" || value === "less" || value === "scss" || value === "json" || value === "json5" || value === "json-stringify" || value === "graphql" || value === "markdown" || value === "mdx" || value === "vue" || value === "yaml" || value === "glimmer" || value === "html" || value === "angular" || value === "lwc") {
501-
formatOptions.parser = value;
526+
if (isString(value)) {
527+
// New parsers that we don't about can be added by plugins
528+
formatOptions.parser = value as FormatOptions["parser"];
502529
}
503530
}
504531

@@ -722,10 +749,12 @@ export {
722749
getModule,
723750
getModulePath,
724751
getPlugin,
752+
getPluginOrExit,
725753
getPluginPath,
726754
getPluginVersion,
727755
getPlugins,
728756
getPluginsBuiltin,
757+
getPluginsOrExit,
729758
getPluginsPaths,
730759
getPluginsVersions,
731760
getProjectPath,
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"plugins": ["./plugin.cjs"],
3+
"fooString": "baz"
4+
}
Lines changed: 31 additions & 0 deletions
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+
};
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/* JavaScript */
2+
"use strict";
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/* Stylesheet */
2+
* {
3+
outline: none;
4+
}

0 commit comments

Comments
 (0)