Skip to content

Commit

Permalink
Improve formatting for do expression (#10693)
Browse files Browse the repository at this point in the history
  • Loading branch information
sosukesuzuki committed Apr 12, 2021
1 parent 07ae013 commit 0cdc62e
Show file tree
Hide file tree
Showing 4 changed files with 175 additions and 1 deletion.
24 changes: 24 additions & 0 deletions changelog_unreleased/javascript/10693.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#### Improve formatting for do expression (#10693 by @sosukesuzuki)

<!-- prettier-ignore -->
```js
// Input
expect(do {
var bar = "foo";
bar;
}).toBe("foo");

// Prettier stable
expect(
do {
var bar = "foo";
bar;
}
).toBe("foo");

// Prettier main
expect(do {
var bar = "foo";
bar;
}).toBe("foo");
```
3 changes: 2 additions & 1 deletion src/language-js/print/call-arguments.js
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,8 @@ function couldGroupArg(arg, arrowChainRecursion = false) {
(!arrowChainRecursion &&
(isCallExpression(arg.body) ||
arg.body.type === "ConditionalExpression")) ||
isJsxNode(arg.body)))
isJsxNode(arg.body))) ||
arg.type === "DoExpression"
);
}

Expand Down
108 changes: 108 additions & 0 deletions tests/js/do/__snapshots__/jsfmt.spec.js.snap
Original file line number Diff line number Diff line change
@@ -1,5 +1,113 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`call-arguments.js [espree] format 1`] = `
"Unexpected token do (3:3)
1 | // from https://github.com/babel/babel/pull/13122/
2 | expect(
> 3 | do {
| ^
4 | var bar = \\"foo\\";
5 | if (!bar) throw new Error(
6 | \\"unreachable\\""
`;
exports[`call-arguments.js [meriyah] format 1`] = `
"[3:4]: Unexpected token: 'do' (3:4)
1 | // from https://github.com/babel/babel/pull/13122/
2 | expect(
> 3 | do {
| ^
4 | var bar = \\"foo\\";
5 | if (!bar) throw new Error(
6 | \\"unreachable\\""
`;
exports[`call-arguments.js format 1`] = `
====================================options=====================================
parsers: ["babel"]
printWidth: 80
| printWidth
=====================================input======================================
// from https://github.com/babel/babel/pull/13122/
expect(
do {
var bar = "foo";
if (!bar) throw new Error(
"unreachable"
)
bar;
}
).toBe("foo");
expect(bar).toBe("foo");
var x = do {
var bar = "foo";
if (!bar) throw new Error(
"unreachable"
)
bar;
};
expect(
do {
var bar = "foo";
bar;
}
).toBe("foo");
expect(bar).toBe("foo");
var x = do {
var bar = "foo";
bar;
};
expect(
() => do {
() => {
var bar = "foo";
};
bar;
}
).toThrow(ReferenceError);
=====================================output=====================================
// from https://github.com/babel/babel/pull/13122/
expect(do {
var bar = "foo";
if (!bar) throw new Error("unreachable");
bar;
}).toBe("foo");
expect(bar).toBe("foo");
var x = do {
var bar = "foo";
if (!bar) throw new Error("unreachable");
bar;
};
expect(do {
var bar = "foo";
bar;
}).toBe("foo");
expect(bar).toBe("foo");
var x = do {
var bar = "foo";
bar;
};
expect(
() => do {
() => {
var bar = "foo";
};
bar;
}
).toThrow(ReferenceError);
================================================================================
`;
exports[`do.js [espree] format 1`] = `
"Unexpected token do (3:5)
1 | const envSpecific = {
Expand Down
41 changes: 41 additions & 0 deletions tests/js/do/call-arguments.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// from https://github.com/babel/babel/pull/13122/
expect(
do {
var bar = "foo";
if (!bar) throw new Error(
"unreachable"
)
bar;
}
).toBe("foo");
expect(bar).toBe("foo");

var x = do {
var bar = "foo";
if (!bar) throw new Error(
"unreachable"
)
bar;
};

expect(
do {
var bar = "foo";
bar;
}
).toBe("foo");
expect(bar).toBe("foo");

var x = do {
var bar = "foo";
bar;
};

expect(
() => do {
() => {
var bar = "foo";
};
bar;
}
).toThrow(ReferenceError);

0 comments on commit 0cdc62e

Please sign in to comment.