Skip to content

Commit

Permalink
fix(javascript): add missing parens for binary in optionalMember (#5543)
Browse files Browse the repository at this point in the history
  • Loading branch information
ikatyang committed Nov 25, 2018
1 parent ab7f46b commit c788842
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 0 deletions.
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)];

0 comments on commit c788842

Please sign in to comment.