Skip to content

Commit

Permalink
[RFC] Keep expanded objects expanded
Browse files Browse the repository at this point in the history
Another attempt at solving the issue where objects are not expanded the way people expect. If there's any new line in the original source, it's going to expand it. This gives more control to the user in how the objects should be formatted.

Fixes #74
  • Loading branch information
vjeux committed Jan 27, 2017
1 parent caf7f2a commit e2b5dc6
Show file tree
Hide file tree
Showing 90 changed files with 5,089 additions and 1,304 deletions.
10 changes: 9 additions & 1 deletion src/printer.js
Expand Up @@ -580,6 +580,14 @@ function genericPrintNoParens(path, options, print) {
);
});

const shouldBreak = util.hasNewlineInRange(
options.originalText,
util.locStart(n),
util.locEnd(n)
);

const lineType = shouldBreak ? hardline : options.bracketSpacing ? line : softline;

if (props.length === 0) {
return "{}";
} else {
Expand All @@ -589,7 +597,7 @@ function genericPrintNoParens(path, options, print) {
indent(
options.tabWidth,
concat([
options.bracketSpacing ? line : softline,
lineType,
join(concat([ separator, line ]), props)
])
),
Expand Down
10 changes: 10 additions & 0 deletions src/util.js
Expand Up @@ -160,6 +160,15 @@ function hasNewline(text, index, opts) {
return idx !== idx2;
}

function hasNewlineInRange(text, start, end) {
for (var i = start; i <= end; ++i) {
if (text.charAt(i) === "\n") {
return true;
}
}
return false;
}

function hasSpaces(text, index, opts) {
opts = opts || {};
const idx = skipSpaces(text, opts.backwards ? index - 1 : index, opts);
Expand Down Expand Up @@ -250,6 +259,7 @@ module.exports = {
skipToLineEnd,
skipNewline,
hasNewline,
hasNewlineInRange,
hasSpaces,
locStart,
locEnd,
Expand Down
12 changes: 10 additions & 2 deletions tests/bracketSpacing/__snapshots__/jsfmt.spec.js.snap
Expand Up @@ -20,7 +20,11 @@ exports[`test object.js 1`] = `
"const obj1 = {a:1, b:2, c:3}
const obj2 = { a:1, b:2, c:3 };
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
const obj1 = { a: 1, b: 2, c: 3 };
const obj1 = {
a: 1,
b: 2,
c: 3
};
const obj2 = { a: 1, b: 2, c: 3 };
"
`;
Expand All @@ -29,7 +33,11 @@ exports[`test object.js 2`] = `
"const obj1 = {a:1, b:2, c:3}
const obj2 = { a:1, b:2, c:3 };
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
const obj1 = {a: 1, b: 2, c: 3};
const obj1 = {
a: 1,
b: 2,
c: 3
};
const obj2 = {a: 1, b: 2, c: 3};
"
`;
60 changes: 46 additions & 14 deletions tests/comments/__snapshots__/jsfmt.spec.js.snap
Expand Up @@ -152,13 +152,29 @@ export type BuckWebSocketMessage =
// Not actually from Buck - this is to let the receiver know that the socket is connected.
type: \"SocketConnected\"
}
| { type: \"BuildProgressUpdated\", progressValue: number }
| { type: \"BuildFinished\", exitCode: number }
| { type: \"BuildStarted\" }
| { type: \"ParseStarted\" }
| { type: \"ParseFinished\" }
| { type: \"RunStarted\" }
| { type: \"RunComplete\" };
| {
type: \"BuildProgressUpdated\",
progressValue: number
}
| {
type: \"BuildFinished\",
exitCode: number
}
| {
type: \"BuildStarted\"
}
| {
type: \"ParseStarted\"
}
| {
type: \"ParseFinished\"
}
| {
type: \"RunStarted\"
}
| {
type: \"RunComplete\"
};
// Missing one level of indentation because of the comment
const rootEpic = (actions, store) =>
Expand Down Expand Up @@ -351,13 +367,29 @@ export type BuckWebSocketMessage =
// Not actually from Buck - this is to let the receiver know that the socket is connected.
type: \"SocketConnected\"
}
| { type: \"BuildProgressUpdated\", progressValue: number }
| { type: \"BuildFinished\", exitCode: number }
| { type: \"BuildStarted\" }
| { type: \"ParseStarted\" }
| { type: \"ParseFinished\" }
| { type: \"RunStarted\" }
| { type: \"RunComplete\" };
| {
type: \"BuildProgressUpdated\",
progressValue: number
}
| {
type: \"BuildFinished\",
exitCode: number
}
| {
type: \"BuildStarted\"
}
| {
type: \"ParseStarted\"
}
| {
type: \"ParseFinished\"
}
| {
type: \"RunStarted\"
}
| {
type: \"RunComplete\"
};
// Missing one level of indentation because of the comment
const rootEpic = (actions, store) =>
Expand Down
6 changes: 5 additions & 1 deletion tests/flow/arrays/__snapshots__/jsfmt.spec.js.snap
Expand Up @@ -73,7 +73,11 @@ var abig2: Array<{ x: number, y: number }> = [
{ 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,
c: \"hey\"
}
];
module.exports = \"arrays\";
Expand Down
6 changes: 5 additions & 1 deletion tests/flow/async_iteration/__snapshots__/jsfmt.spec.js.snap
Expand Up @@ -82,7 +82,11 @@ async function f() {
}
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
interface File { readLine(): Promise<string>, close(): void, EOF: boolean }
interface File {
readLine(): Promise<string>,
close(): void,
EOF: boolean
}
declare function fileOpen(path: string): Promise<File>;
Expand Down
4 changes: 3 additions & 1 deletion tests/flow/binding/__snapshots__/jsfmt.spec.js.snap
Expand Up @@ -44,7 +44,9 @@ x ^= 0;
x &= 0;
// regression tests -- OK to assign consts like this:
const { foo } = { foo: \"foo\" };
const { foo } = {
foo: \"foo\"
};
const [bar] = [\"bar\"];
(foo: number); // error: string ~> number
(bar: number); // error: string ~> number
Expand Down
13 changes: 10 additions & 3 deletions tests/flow/callable/__snapshots__/jsfmt.spec.js.snap
Expand Up @@ -10,7 +10,10 @@ function f(x) {
(f: F);
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
type F = { (x: string): number, p?: string };
type F = {
(x: string): number,
p?: string
};
function f(x) {
return x.length;
Expand Down Expand Up @@ -49,11 +52,15 @@ foo(Boolean);
var dict: { [k: string]: any } = {};
dict(); // error, callable signature not found
interface ICall { (x: string): void }
interface ICall {
(x: string): void
}
declare var icall: ICall;
icall(0); // error, number ~> string
icall.call(null, 0); // error, number ~> string
type Callable = { (x: string): void };
type Callable = {
(x: string): void
};
declare var callable: Callable;
callable(0); // error, number ~> string
Expand Down
8 changes: 7 additions & 1 deletion tests/flow/class_statics/__snapshots__/jsfmt.spec.js.snap
Expand Up @@ -117,6 +117,12 @@ class E {
}
// note: above classdefs are sufficiently annotated to export
module.exports = { A: A, B: B, C: C, D: D, E: E };
module.exports = {
A: A,
B: B,
C: C,
D: D,
E: E
};
"
`;
18 changes: 15 additions & 3 deletions tests/flow/computed_props/__snapshots__/jsfmt.spec.js.snap
Expand Up @@ -23,9 +23,17 @@ ColorIdToNumber.XXX; // oops
module.exports = { ColorId, ColorNumber };
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
var ColorId = { RED: \"R\", GREEN: \"G\", BLUE: \"B\" };
var ColorId = {
RED: \"R\",
GREEN: \"G\",
BLUE: \"B\"
};
var ColorNumber = { RED: \"ff0000\", GREEN: \"00ff00\", BLUE: \"0000ff\" };
var ColorNumber = {
RED: \"ff0000\",
GREEN: \"00ff00\",
BLUE: \"0000ff\"
};
var ColorIdToNumber = {
[ColorId.RED]: ColorNumber.RED,
Expand Down Expand Up @@ -94,7 +102,11 @@ module.exports = {
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
var hello = require(\"./test4\");
var dummy = require(\"./test\");
module.exports = { ...dummy, [hello]: \"world\", ...dummy };
module.exports = {
...dummy,
[hello]: \"world\",
...dummy
};
"
`;
Expand Down
Expand Up @@ -44,6 +44,8 @@ module.exports = {
* @flow
*/
module.exports = { numVal: 42 };
module.exports = {
numVal: 42
};
"
`;
Expand Up @@ -60,6 +60,8 @@ module.exports = {
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/* @flow */
module.exports = { numVal: 42 };
module.exports = {
numVal: 42
};
"
`;
Expand Up @@ -37,7 +37,9 @@ Foo.prototype = {
exports.Foo = Foo;
// so you want to type Foo, by declaring it as a class
interface IFooPrototype { m(): number }
interface IFooPrototype {
m(): number
}
interface IFoo extends IFooPrototype {
static (): void,
x: boolean /* error, should have declared x: number instead*/,
Expand Down
5 changes: 4 additions & 1 deletion tests/flow/declare_class/__snapshots__/jsfmt.spec.js.snap
Expand Up @@ -10,7 +10,10 @@ C.foo(\"\");
(C.name: string);
(C.name: number); // error, it\'s a string
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
declare class C { static x: number, static foo(x: number): void }
declare class C {
static x: number,
static foo(x: number): void
}
C.x = \"\";
C.foo(\"\");
Expand Down
12 changes: 9 additions & 3 deletions tests/flow/declare_class/lib/__snapshots__/jsfmt.spec.js.snap
Expand Up @@ -9,8 +9,14 @@ declare class D extends _module.C {
foo(): string;
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
declare class _C { foo(): number }
declare var _module: { C: Class<_C> };
declare class D extends _module.C { foo(): string }
declare class _C {
foo(): number
}
declare var _module: {
C: Class<_C>
};
declare class D extends _module.C {
foo(): string
}
"
`;
4 changes: 3 additions & 1 deletion tests/flow/declare_export/__snapshots__/jsfmt.spec.js.snap
Expand Up @@ -549,7 +549,9 @@ module.exports = {
* @flow
*/
module.exports = { numberValue: 42 };
module.exports = {
numberValue: 42
};
"
`;

Expand Down
8 changes: 6 additions & 2 deletions tests/flow/declare_type/lib/__snapshots__/jsfmt.spec.js.snap
Expand Up @@ -43,15 +43,19 @@ declare module \'foo\' {
/* @flow */
declare module \"foo\" {
declare class A { toz: number }
declare class A {
toz: number
}
declare var n: string;
declare type Foo = typeof n;
declare type Bar = A;
declare type Id<T> = T;
declare var exports: { (a: number, b: number): number };
declare var exports: {
(a: number, b: number): number
};
}
"
`;

0 comments on commit e2b5dc6

Please sign in to comment.