Skip to content

Commit

Permalink
[RFC] Always expand objects if contained in another object or array
Browse files Browse the repository at this point in the history
The flow tests look really bad with it but the more real world tests feel much better. I think we'll have to run that on a real codebase and see how it feels.

Fixes prettier#74
  • Loading branch information
vjeux committed Jan 26, 2017
1 parent 6e68f74 commit faf46e5
Show file tree
Hide file tree
Showing 11 changed files with 312 additions and 53 deletions.
11 changes: 9 additions & 2 deletions src/printer.js
Original file line number Diff line number Diff line change
Expand Up @@ -543,7 +543,7 @@ function genericPrintNoParens(path, options, print) {
case "ObjectExpression":
case "ObjectPattern":
case "ObjectTypeAnnotation":
var allowBreak = false;
var parent = path.getParentNode();
var isTypeAnnotation = n.type === "ObjectTypeAnnotation";
// Leave this here because we *might* want to make this
// configurable later -- flow accepts ";" for type separators
Expand All @@ -570,6 +570,13 @@ function genericPrintNoParens(path, options, print) {
);
});

// We want to always expand an object if it's contained inside of
// another object or array
const shouldForceBreak = parent.type === "ObjectProperty" ||
parent.type === "ArrayExpression";

const lineType = shouldForceBreak ? hardline : (options.bracketSpacing ? line : softline);

if (props.length === 0) {
return "{}";
} else {
Expand All @@ -579,7 +586,7 @@ function genericPrintNoParens(path, options, print) {
indent(
options.tabWidth,
concat([
options.bracketSpacing ? line : softline,
lineType,
join(concat([ separator, line ]), props)
])
),
Expand Down
89 changes: 72 additions & 17 deletions tests/flow/arrays/__snapshots__/jsfmt.spec.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -57,23 +57,78 @@ var alittle: Array<?number> = [0, 1, 2, 3, null];
var abig: Array<?number> = [0, 1, 2, 3, 4, 5, 6, 8, null];
var abig2: Array<{ x: number, y: number }> = [
{ x: 0, y: 0 },
{ x: 0, y: 0 },
{ x: 0, y: 0 },
{ x: 0, y: 0 },
{ x: 0, y: 0 },
{ x: 0, y: 0 },
{ x: 0, y: 0 },
{ x: 0, y: 0 },
{ x: 0, y: 0 },
{ x: 0, y: 0 },
{ x: 0, y: 0 },
{ x: 0, y: 0 },
{ x: 0, y: 0 },
{ x: 0, y: 0, a: true },
{ x: 0, y: 0, b: \"hey\" },
{ x: 0, y: 0, c: 1 },
{ x: 0, y: 0, c: \"hey\" }
{
x: 0,
y: 0
},
{
x: 0,
y: 0
},
{
x: 0,
y: 0
},
{
x: 0,
y: 0
},
{
x: 0,
y: 0
},
{
x: 0,
y: 0
},
{
x: 0,
y: 0
},
{
x: 0,
y: 0
},
{
x: 0,
y: 0
},
{
x: 0,
y: 0
},
{
x: 0,
y: 0
},
{
x: 0,
y: 0
},
{
x: 0,
y: 0
},
{
x: 0,
y: 0,
a: true
},
{
x: 0,
y: 0,
b: \"hey\"
},
{
x: 0,
y: 0,
c: 1
},
{
x: 0,
y: 0,
c: \"hey\"
}
];
module.exports = \"arrays\";
Expand Down
8 changes: 7 additions & 1 deletion tests/flow/destructuring/__snapshots__/jsfmt.spec.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,13 @@ var cp2_err: string = others.childprop2; // Error: number ~> string
declare var a: string;
declare var b: string;
declare var c: string;
[{ a1: a, b }, c] = [{ a1: 0, b: 1 }, 2];
[{ a1: a, b }, c] = [
{
a1: 0,
b: 1
},
2
];
var { m } = { m: 0 };
({ m } = { m: m });
Expand Down
64 changes: 48 additions & 16 deletions tests/flow/logical/__snapshots__/jsfmt.spec.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -901,14 +901,30 @@ 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\"
}
];
}
Expand All @@ -917,14 +933,30 @@ 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\"
}
];
}
Expand Down
15 changes: 13 additions & 2 deletions tests/flow/new_react/__snapshots__/jsfmt.spec.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -506,12 +506,23 @@ var C = React.createClass({
}
});
<C statistics={[{}, { label: \"\", value: undefined }]} />;
<C
statistics={[
{},
{
label: \"\",
value: undefined
}
]}
/>;
// error (label is required, value not required)
var props: Array<{ label: string, value?: number }> = [
{},
{ label: \"\", value: undefined }
{
label: \"\",
value: undefined
}
]; // error (same as ^)
"
`;
Expand Down
25 changes: 20 additions & 5 deletions tests/flow/object_assign/__snapshots__/jsfmt.spec.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -105,11 +105,26 @@ exports[`test apply.js 1`] = `
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// @flow
(Object.assign.apply(null, [{}, { a: 1 }, { b: \"foo\" }]): {
a: number,
b: string
});
(Object.assign({}, ...[{ a: 1 }, { b: \"foo\" }]): { a: number, b: string });
(Object.assign.apply(null, [
{},
{
a: 1
},
{
b: \"foo\"
}
]): { a: number, b: string });
(Object.assign(
{},
...[
{
a: 1
},
{
b: \"foo\"
}
]
): { a: number, b: string });
"
`;
Expand Down
40 changes: 32 additions & 8 deletions tests/flow/union/__snapshots__/jsfmt.spec.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,38 @@ var l: Array<T> = [
type T = { type: \"a\", a: number } | { type: \"b\", b: string };
var l: Array<T> = [
{ type: \"a\", a: 1 },
{ type: \"a\", a: 2 },
{ type: \"a\", a: 3 },
{ type: \"a\", a: 4 },
{ type: \"b\", b: \"monkey\" },
{ type: \"b\", b: \"gorilla\" },
{ type: \"b\", b: \"giraffe\" },
{ type: \"b\", b: \"penguin\" }
{
type: \"a\",
a: 1
},
{
type: \"a\",
a: 2
},
{
type: \"a\",
a: 3
},
{
type: \"a\",
a: 4
},
{
type: \"b\",
b: \"monkey\"
},
{
type: \"b\",
b: \"gorilla\"
},
{
type: \"b\",
b: \"giraffe\"
},
{
type: \"b\",
b: \"penguin\"
}
];
"
`;
Expand Down
9 changes: 7 additions & 2 deletions tests/flow/union_new/__snapshots__/jsfmt.spec.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,13 @@ var bar: Array<{b: ?boolean, c: number} | {b: boolean}> = [
/* @flow */

var bar: Array<{ b: ?boolean, c: number } | { b: boolean }> = [
{ b: true, c: 123 },
{ b: true }
{
b: true,
c: 123
},
{
b: true
}
];
"
`;
Expand Down

0 comments on commit faf46e5

Please sign in to comment.