From 409d4b6eb541ab51f42817ca759e175c752a93cc Mon Sep 17 00:00:00 2001 From: Davy Duperron Date: Tue, 24 Jan 2017 20:20:24 +0100 Subject: [PATCH] Ability to break on `:` for objects (#314) --- src/printer.js | 40 +++++++++++- .../logical/__snapshots__/jsfmt.spec.js.snap | 32 ++++----- .../prettier/__snapshots__/jsfmt.spec.js.snap | 65 +++++++++++++++++++ tests/prettier/object-prop-break-in.js | 27 ++++++++ 4 files changed, 147 insertions(+), 17 deletions(-) create mode 100644 tests/prettier/object-prop-break-in.js diff --git a/src/printer.js b/src/printer.js index bb249fcc..48251f0b 100644 --- a/src/printer.js +++ b/src/printer.js @@ -609,7 +609,25 @@ function genericPrintNoParens(path, options, print) { } else { parts.push(printPropertyKey(path, options, print)); } - parts.push(": ", path.call(print, "value")); + + let printedValue = path.call(print, "value"); + if (shouldPrintSameLine(n.value)) { + parts.push(concat([": ", printedValue])); + } else { + parts.push( + concat([ + group( + concat([ + ":", + ifBreak(" (", " "), + indent(options.tabWidth, concat([softline, printedValue])) + ]) + ), + line, + ifBreak(")") + ]) + ); + } } return concat(parts); @@ -2458,6 +2476,26 @@ function isLastStatement(path) { return body && body[body.length - 1] === node; } +function shouldPrintSameLine(node) { + const type = node.type; + return namedTypes.Literal.check(node) || + type === "ArrayExpression" || + type === "ArrayPattern" || + type === "ArrowFunctionExpression" || + type === "AssignmentPattern" || + type === "CallExpression" || + type === "FunctionExpression" || + type === "Identifier" || + type === "Literal" || + type === "MemberExpression" || + type === "NewExpression" || + type === "ObjectExpression" || + type === "ObjectPattern" || + type === "StringLiteral" || + type === "ThisExpression" || + type === "TypeCastExpression"; +} + function printAstToDoc(ast, options) { function printGenerically(path) { return comments.printComments( diff --git a/tests/logical/__snapshots__/jsfmt.spec.js.snap b/tests/logical/__snapshots__/jsfmt.spec.js.snap index 2cc4a5ee..b19177b0 100644 --- a/tests/logical/__snapshots__/jsfmt.spec.js.snap +++ b/tests/logical/__snapshots__/jsfmt.spec.js.snap @@ -901,14 +901,14 @@ function logical12b(y: number): number { */ function logical13(x: number): Array<{ x: string }> { return [ - { x: x && \"bar\" }, - { x: true && \"bar\" }, - { x: true && false }, - { x: false && false }, - { x: 1 && \"bar\" }, - { x: \"foo\" && \"bar\" }, - { x: \"foo\" && \"bar\" }, - { x: \"foo\" && \"bar\" } + { x: x && \"bar\" }, + { x: true && \"bar\" }, + { x: true && false }, + { x: false && false }, + { x: 1 && \"bar\" }, + { x: \"foo\" && \"bar\" }, + { x: \"foo\" && \"bar\" }, + { x: \"foo\" && \"bar\" } ]; } @@ -917,14 +917,14 @@ function logical13(x: number): Array<{ x: string }> { */ function logical14(x: number): Array<{ x: string }> { return [ - { x: x || \"bar\" }, - { x: false || \"bar\" }, - { x: false || true }, - { x: true || false }, - { x: 0 || \"bar\" }, - { x: \"foo\" || \"bar\" }, - { x: \"foo\" || \"bar\" }, - { x: \"foo\" || \"bar\" } + { x: x || \"bar\" }, + { x: false || \"bar\" }, + { x: false || true }, + { x: true || false }, + { x: 0 || \"bar\" }, + { x: \"foo\" || \"bar\" }, + { x: \"foo\" || \"bar\" }, + { x: \"foo\" || \"bar\" } ]; } diff --git a/tests/prettier/__snapshots__/jsfmt.spec.js.snap b/tests/prettier/__snapshots__/jsfmt.spec.js.snap index f3a305f9..ea9ad6fe 100644 --- a/tests/prettier/__snapshots__/jsfmt.spec.js.snap +++ b/tests/prettier/__snapshots__/jsfmt.spec.js.snap @@ -690,6 +690,71 @@ method() " `; +exports[`test object-prop-break-in.js 1`] = ` +"const a = classnames({ + \"some-prop\": this.state.longLongLongLongLongLongLongLongLongTooLongProp +}); + +const b = classnames({ + \"some-prop\": this.state.longLongLongLongLongLongLongLongLongTooLongProp === true +}); + +const c = classnames({ + \"some-prop\": [ \"foo\", \"bar\", \"foo\", \"bar\", \"foo\", \"bar\", \"foo\", \"bar\", \"foo\" ] +}); + +const d = classnames({ + \"some-prop\": () => {} +}); + +const e = classnames({ + \"some-prop\": function bar() {} +}); + +const f = classnames({ + \"some-prop\": { foo: \"bar\", bar: \"foo\", foo: \"bar\", bar: \"foo\", foo: \"bar\" } +}); + +const g = classnames({ + \"some-prop\": longLongLongLongLongLongLongLongLongLongLongLongLongTooLongVar || 1337 +}); +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +const a = classnames({ + \"some-prop\": this.state.longLongLongLongLongLongLongLongLongTooLongProp +}); + +const b = classnames({ + \"some-prop\": ( + this.state.longLongLongLongLongLongLongLongLongTooLongProp === true + ) +}); + +const c = classnames({ + \"some-prop\": [ \"foo\", \"bar\", \"foo\", \"bar\", \"foo\", \"bar\", \"foo\", \"bar\", \"foo\" ] +}); + +const d = classnames({ + \"some-prop\": () => { + } +}); + +const e = classnames({ + \"some-prop\": function bar() { + } +}); + +const f = classnames({ + \"some-prop\": { foo: \"bar\", bar: \"foo\", foo: \"bar\", bar: \"foo\", foo: \"bar\" } +}); + +const g = classnames({ + \"some-prop\": ( + longLongLongLongLongLongLongLongLongLongLongLongLongTooLongVar || 1337 + ) +}); +" +`; + exports[`test optional-type-name.js 1`] = ` "type Foo = (any) => string diff --git a/tests/prettier/object-prop-break-in.js b/tests/prettier/object-prop-break-in.js new file mode 100644 index 00000000..7d4e42f0 --- /dev/null +++ b/tests/prettier/object-prop-break-in.js @@ -0,0 +1,27 @@ +const a = classnames({ + "some-prop": this.state.longLongLongLongLongLongLongLongLongTooLongProp +}); + +const b = classnames({ + "some-prop": this.state.longLongLongLongLongLongLongLongLongTooLongProp === true +}); + +const c = classnames({ + "some-prop": [ "foo", "bar", "foo", "bar", "foo", "bar", "foo", "bar", "foo" ] +}); + +const d = classnames({ + "some-prop": () => {} +}); + +const e = classnames({ + "some-prop": function bar() {} +}); + +const f = classnames({ + "some-prop": { foo: "bar", bar: "foo", foo: "bar", bar: "foo", foo: "bar" } +}); + +const g = classnames({ + "some-prop": longLongLongLongLongLongLongLongLongLongLongLongLongTooLongVar || 1337 +});