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

fix(javascript): correctly handle parentheses between await and optional chaining #6087

Merged
merged 1 commit into from Apr 30, 2019
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
20 changes: 20 additions & 0 deletions CHANGELOG.unreleased.md
Expand Up @@ -60,3 +60,23 @@ Examples:
test(/** @type {!Array} */ (arrOrString).length);
test(/** @type {!Array} */ (arrOrString).length + 1);
```

- JavaScript: respect parenthesis around optional chaining before await ([#6087] by [@evilebottnawi])

<!-- prettier-ignore -->
```js
// Input
async function myFunction() {
var x = (await foo.bar.blah)?.hi;
}

// Output (Prettier stable)
async function myFunction() {
var x = await foo.bar.blah?.hi;
}

// Output (Prettier master)
async function myFunction() {
var x = (await foo.bar.blah)?.hi;
}
```
1 change: 1 addition & 0 deletions src/language-js/needs-parens.js
Expand Up @@ -455,6 +455,7 @@ function needsParens(path, options) {
case "TSAsExpression":
case "TSNonNullExpression":
case "BindExpression":
case "OptionalMemberExpression":
return true;

case "MemberExpression":
Expand Down
8 changes: 8 additions & 0 deletions tests/optional_chaining/__snapshots__/jsfmt.spec.js.snap
Expand Up @@ -33,6 +33,10 @@ a?.b[3].c?.(x).d.e?.f[3].g?.(y).h;
(list || list2)?.length;
(list || list2)?.[(list || list2)];

async function HelloWorld() {
var x = (await foo.bar.blah)?.hi;
}

=====================================output=====================================
var street = user.address?.street;
var fooValue = myForm.querySelector("input[name=foo]")?.value;
Expand Down Expand Up @@ -61,5 +65,9 @@ a?.b?.c.d?.e;
(list || list2)?.length;
(list || list2)?.[list || list2];

async function HelloWorld() {
var x = (await foo.bar.blah)?.hi;
}

================================================================================
`;
4 changes: 4 additions & 0 deletions tests/optional_chaining/chaining.js
Expand Up @@ -24,3 +24,7 @@ a?.b[3].c?.(x).d.e?.f[3].g?.(y).h;

(list || list2)?.length;
(list || list2)?.[(list || list2)];

async function HelloWorld() {
var x = (await foo.bar.blah)?.hi;
}