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): add missing parens for binary in optionalMember #5543

Merged
Show file tree
Hide file tree
Changes from 2 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
1 change: 1 addition & 0 deletions src/language-js/needs-parens.js
Expand Up @@ -258,6 +258,7 @@ function needsParens(path, options) {
return true;

case "MemberExpression":
case "OptionalMemberExpression":
return name === "object" && parent.object === node;

case "AssignmentExpression":
Expand Down
6 changes: 6 additions & 0 deletions tests/optional_chaining/__snapshots__/jsfmt.spec.js.snap
Expand Up @@ -23,6 +23,9 @@ a?.b[3].c?.(x).d.e?.f[3].g?.(y).h;

(a?.b)?.c.d?.e;
(a ? b : c)?.d;

(list || list2)?.length;
(list || list2)?.[(list || list2)];
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
var street = user.address?.street;
var fooValue = myForm.querySelector("input[name=foo]")?.value;
Expand All @@ -47,4 +50,7 @@ a?.b[3].c?.(x).d.e?.f[3].g?.(y).h;
a?.b?.c.d?.e;
(a ? b : c)?.d;

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

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

(a?.b)?.c.d?.e;
(a ? b : c)?.d;

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