Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve formatting for do expression #10693

Merged
merged 3 commits into from Apr 12, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
24 changes: 24 additions & 0 deletions changelog_unreleased/javascript/10693.md
@@ -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
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"
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are there any other conditions that should be considered?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't think of any

);
}

Expand Down
108 changes: 108 additions & 0 deletions tests/js/do/__snapshots__/jsfmt.spec.js.snap
@@ -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
@@ -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);