Ability to break on `:` for objects (#314)

master
Davy Duperron 2017-01-24 20:20:24 +01:00 committed by James Long
parent 3aa267d5e8
commit 409d4b6eb5
4 changed files with 147 additions and 17 deletions

View File

@ -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(

View File

@ -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\" }
];
}

View File

@ -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

View File

@ -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
});