Skip to content

Commit 6223c0d

Browse files
fiskerfabiospampinato
authored andcommitted
Added support for the "objectWrap" and "experimentalOperatorPosition" options
1 parent ad666d6 commit 6223c0d

File tree

5 files changed

+62
-0
lines changed

5 files changed

+62
-0
lines changed

src/bin.ts

+8
Original file line numberDiff line numberDiff line change
@@ -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,

src/prettier_plugins_builtin.ts

+32
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/types.ts

+2
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

+14
Original file line numberDiff line numberDiff line change
@@ -429,6 +429,13 @@ function normalizeFormatOptions(options: unknown): FormatOptions {
429429

430430
const formatOptions: FormatOptions = {};
431431

432+
if ("experimentalOperatorPosition" in options) {
433+
const value = options.experimentalOperatorPosition;
434+
if (value === "start" || value === "end") {
435+
formatOptions.experimentalOperatorPosition = value;
436+
}
437+
}
438+
432439
if ("experimentalTernaries" in options) {
433440
const value = options.experimentalTernaries;
434441
if (isBoolean(value)) {
@@ -492,6 +499,13 @@ function normalizeFormatOptions(options: unknown): FormatOptions {
492499
}
493500
}
494501

502+
if ("objectWrap" in options) {
503+
const value = options.objectWrap;
504+
if (value === "preserve" || value === "collapse") {
505+
formatOptions.objectWrap = value;
506+
}
507+
}
508+
495509
if ("parser" in options) {
496510
const value = options.parser;
497511
// prettier-ignore

test/__tests__/__snapshots__/early-exit.js.snap

+6
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ exports[`show usage with --help (stdout) 1`] = `
3939
--end-of-line <lf|crlf|cr|auto>
4040
Which end of line characters to apply
4141
Defaults to "lf"
42+
--experimental-operator-position <start|end>
43+
Where to print operators when binary expressions wrap lines
44+
Defaults to "end"
4245
--experimental-ternaries
4346
Use curious ternaries, with the question mark after the condition
4447
Defaults to "false"
@@ -47,6 +50,9 @@ exports[`show usage with --help (stdout) 1`] = `
4750
Defaults to "css"
4851
--jsx-single-quote Use single quotes in JSX
4952
Defaults to "false"
53+
--object-wrap <preserve|collapse>
54+
How to wrap object literals
55+
Defaults to "preserve"
5056
--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>
5157
Which parser to use
5258
--print-width <int> The line length where Prettier will try wrap

0 commit comments

Comments
 (0)