Skip to content

Commit f8e1ad8

Browse files
authored
Add parens to head of ExpressionStatement instead of whole statement (#14077)
1 parent 8034bad commit f8e1ad8

File tree

17 files changed

+89
-65
lines changed

17 files changed

+89
-65
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#### Add parentheses to head of `ExpressionStatement` instead of the whole statement (#14077 by @fisker)
2+
3+
<!-- prettier-ignore -->
4+
```jsx
5+
// Input
6+
({}).toString.call(foo) === "[object Array]"
7+
? foo.forEach(iterateArray)
8+
: iterateObject(foo);
9+
10+
// Prettier stable
11+
({}.toString.call(foo) === "[object Array]"
12+
? foo.forEach(iterateArray)
13+
: iterateObject(foo));
14+
15+
// Prettier main
16+
({}).toString.call(foo.forEach) === "[object Array]"
17+
? foo.forEach(iterateArray)
18+
: iterateObject(foo);
19+
```

src/language-js/needs-parens.js

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,26 @@ function needsParens(path, options) {
127127
return false;
128128
}
129129

130+
if (
131+
node.type === "ObjectExpression" ||
132+
node.type === "FunctionExpression" ||
133+
node.type === "ClassExpression" ||
134+
node.type === "DoExpression"
135+
) {
136+
const expression = path.findAncestor(
137+
(node) => node.type === "ExpressionStatement"
138+
)?.expression;
139+
if (
140+
expression &&
141+
startsWithNoLookaheadToken(
142+
expression,
143+
(leftmostNode) => leftmostNode === node
144+
)
145+
) {
146+
return true;
147+
}
148+
}
149+
130150
switch (parent.type) {
131151
case "ParenthesizedExpression":
132152
return false;
@@ -200,21 +220,6 @@ function needsParens(path, options) {
200220
}
201221
break;
202222
}
203-
case "ExpressionStatement": {
204-
if (
205-
startsWithNoLookaheadToken(
206-
node,
207-
(node) =>
208-
node.type === "ObjectExpression" ||
209-
node.type === "FunctionExpression" ||
210-
node.type === "ClassExpression" ||
211-
node.type === "DoExpression"
212-
)
213-
) {
214-
return true;
215-
}
216-
break;
217-
}
218223
case "ArrowFunctionExpression": {
219224
if (
220225
name === "body" &&

tests/format/flow-repo/arith/__snapshots__/jsfmt.spec.js.snap

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ let tests = [
168168
x + ""; // error
169169
"" + x; // error
170170
x + {}; // error
171-
({} + x); // error
171+
({}) + x; // error
172172
},
173173
174174
// when one side is a string or number and the other is invalid, we
@@ -344,10 +344,10 @@ let tests = [
344344
"foo" < 1; // error
345345
"foo" < "bar";
346346
1 < { foo: 1 }; // error
347-
({ foo: 1 } < 1); // error
348-
({ foo: 1 } < { foo: 1 }); // error
347+
({ foo: 1 }) < 1; // error
348+
({ foo: 1 }) < { foo: 1 }; // error
349349
"foo" < { foo: 1 }; // error
350-
({ foo: 1 } < "foo"); // error
350+
({ foo: 1 }) < "foo"; // error
351351
352352
var x = (null: ?number);
353353
1 < x; // 2 errors: null !~> number; undefined !~> number

tests/format/flow-repo/binary/__snapshots__/jsfmt.spec.js.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ let tests = [
9898
function () {
9999
null in {}; // error
100100
void 0 in {}; // error
101-
({} in {}); // error
101+
({}) in {}; // error
102102
[] in {}; // error
103103
false in []; // error
104104
},

tests/format/flow-repo/object-method/__snapshots__/jsfmt.spec.js.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ function foo() {
160160
161161
function bar(f: () => void) {
162162
f(); // passing global object as \`this\`
163-
({ f }.f()); // passing container object as \`this\`
163+
({ f }).f(); // passing container object as \`this\`
164164
}
165165
166166
bar(foo); // error, since \`this\` is used non-trivially in \`foo\`

tests/format/js/classes/__snapshots__/jsfmt.spec.js.snap

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -111,10 +111,10 @@ printWidth: 80
111111
(class a extends b {}) + 1;
112112
113113
=====================================output=====================================
114-
(class {} + 1);
115-
(class a {} + 1);
116-
(class extends b {} + 1);
117-
(class a extends b {} + 1);
114+
(class {}) + 1;
115+
(class a {}) + 1;
116+
(class extends b {}) + 1;
117+
(class a extends b {}) + 1;
118118
119119
================================================================================
120120
`;
@@ -128,7 +128,7 @@ printWidth: 80
128128
(class {})(class {});
129129
130130
=====================================output=====================================
131-
(class {}(class {}));
131+
(class {})(class {});
132132
133133
================================================================================
134134
`;
@@ -225,8 +225,8 @@ printWidth: 80
225225
(class {}).a;
226226
227227
=====================================output=====================================
228-
(class {}[1]);
229-
(class {}.a);
228+
(class {})[1];
229+
(class {}).a;
230230
231231
================================================================================
232232
`;
@@ -356,7 +356,7 @@ printWidth: 80
356356
if (1) (class {}) ? 1 : 2;
357357
358358
=====================================output=====================================
359-
if (1) (class {} ? 1 : 2);
359+
if (1) (class {}) ? 1 : 2;
360360
361361
================================================================================
362362
`;

tests/format/js/decorators/class-expression/__snapshots__/jsfmt.spec.js.snap

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -387,14 +387,14 @@ semi: false
387387
(@deco class {}).name;
388388
389389
=====================================output=====================================
390-
;((
390+
;(
391391
@deco
392392
class Foo {}
393-
).name)
394-
;((
393+
).name
394+
;(
395395
@deco
396396
class {}
397-
).name)
397+
).name
398398
399399
================================================================================
400400
`;
@@ -409,14 +409,14 @@ printWidth: 80
409409
(@deco class {}).name;
410410
411411
=====================================output=====================================
412-
((
412+
(
413413
@deco
414414
class Foo {}
415-
).name);
416-
((
415+
).name;
416+
(
417417
@deco
418418
class {}
419-
).name);
419+
).name;
420420
421421
================================================================================
422422
`;

tests/format/js/do/__snapshots__/jsfmt.spec.js.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ function foo() {
252252
}
253253
254254
(do {});
255-
(do {} + 1);
255+
(do {}) + 1;
256256
1 + do {};
257257
() => do {};
258258

tests/format/js/function/__snapshots__/jsfmt.spec.js.snap

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,15 @@ a + function() {};
1919
new function() {};
2020
2121
=====================================output=====================================
22-
(function () {}.length);
22+
(function () {}).length;
2323
typeof function () {};
2424
export default (function () {})();
2525
(function () {})()\`\`;
2626
(function () {})\`\`;
2727
new (function () {})();
2828
(function () {});
2929
a = function f() {} || b;
30-
(function () {} && a);
30+
(function () {}) && a;
3131
a + function () {};
3232
new (function () {})();
3333

tests/format/js/method-chain/__snapshots__/jsfmt.spec.js.snap

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1522,8 +1522,8 @@ method()
15221522
["abc"]((x) => x)
15231523
[abc]((x) => x);
15241524
1525-
({}.a().b());
1526-
({}.a().b());
1525+
({}).a().b();
1526+
({}).a().b();
15271527
15281528
================================================================================
15291529
`;

0 commit comments

Comments
 (0)