Skip to content

Commit 4203efe

Browse files
authored
Avoid having small right operand on its own line (#180) (#180)
1 parent 5da8fa1 commit 4203efe

5 files changed

Lines changed: 51 additions & 47 deletions

File tree

src/binary-operator-printers/arithmetic.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,17 @@ module.exports = {
3838
const groupIfNecessary = groupIfNecessaryBuilder(path);
3939
const indentIfNecessary = indentIfNecessaryBuilder(path);
4040

41+
const right = concat([node.operator, line, path.call(print, 'right')]);
42+
// If it's a single binary operation, avoid having a small right
43+
// operand like - 1 on its own line
44+
const shouldGroup =
45+
node.left.type !== 'BinaryOperation' &&
46+
path.getParentNode().type !== 'BinaryOperation';
4147
return groupIfNecessary(
4248
concat([
4349
path.call(print, 'left'),
4450
' ',
45-
indentIfNecessary(
46-
concat([node.operator, line, path.call(print, 'right')])
47-
)
51+
indentIfNecessary(shouldGroup ? group(right) : right)
4852
])
4953
);
5054
}

src/binary-operator-printers/comparison.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,17 @@ module.exports = {
2323
print: (node, path, print) => {
2424
const indentIfNecessary = indentIfNecessaryBuilder(path);
2525

26+
const right = concat([node.operator, line, path.call(print, 'right')]);
27+
// If it's a single binary operation, avoid having a small right
28+
// operand like - 1 on its own line
29+
const shouldGroup =
30+
node.left.type !== 'BinaryOperation' &&
31+
path.getParentNode().type !== 'BinaryOperation';
2632
return group(
2733
concat([
2834
path.call(print, 'left'),
2935
' ',
30-
indentIfNecessary(
31-
concat([node.operator, line, path.call(print, 'right')])
32-
)
36+
indentIfNecessary(shouldGroup ? group(right) : right)
3337
])
3438
);
3539
}

src/binary-operator-printers/exponentiation.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,21 @@ const {
77
module.exports = {
88
match: op => op === '**',
99
print: (node, path, print) => {
10+
const right = concat([
11+
ifBreak(' ', ''),
12+
node.operator,
13+
softline,
14+
path.call(print, 'right')
15+
]);
16+
// If it's a single binary operation, avoid having a small right
17+
// operand like - 1 on its own line
18+
const shouldGroup =
19+
node.left.type !== 'BinaryOperation' &&
20+
path.getParentNode().type !== 'BinaryOperation';
1021
return group(
1122
concat([
1223
path.call(print, 'left'),
13-
ifBreak(' ', ''),
14-
indent(concat([node.operator, softline, path.call(print, 'right')]))
24+
indent(shouldGroup ? group(right) : right)
1525
])
1626
);
1727
}

src/binary-operator-printers/logical.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,17 @@ module.exports = {
2626
const groupIfNecessary = groupIfNecessaryBuilder(path);
2727
const indentIfNecessary = indentIfNecessaryBuilder(path);
2828

29+
const right = concat([node.operator, line, path.call(print, 'right')]);
30+
// If it's a single binary operation, avoid having a small right
31+
// operand like - 1 on its own line
32+
const shouldGroup =
33+
node.left.type !== 'BinaryOperation' &&
34+
path.getParentNode().type !== 'BinaryOperation';
2935
return groupIfNecessary(
3036
concat([
3137
path.call(print, 'left'),
3238
' ',
33-
indentIfNecessary(
34-
concat([node.operator, line, path.call(print, 'right')])
35-
)
39+
indentIfNecessary(shouldGroup ? group(right) : right)
3640
])
3741
);
3842
}

tests/BinaryOperators/__snapshots__/jsfmt.spec.js.snap

Lines changed: 18 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -246,8 +246,7 @@ contract ArithmeticOperators {
246246
veryVeryVeryVeryVeryLongVariableCalledB;
247247
veryVeryVeryVeryVeryLongFunctionCalledA(
248248
veryVeryVeryVeryVeryLongVariableCalledB
249-
) +
250-
c;
249+
) + c;
251250
veryVeryVeryVeryVeryLongFunctionCalledA(
252251
veryVeryVeryVeryVeryLongVariableCalledB
253252
) +
@@ -264,20 +263,17 @@ contract ArithmeticOperators {
264263
if (
265264
veryVeryVeryVeryVeryLongFunctionCalledA(
266265
veryVeryVeryVeryVeryLongVariableCalledB
267-
) +
268-
c
266+
) + c
269267
) {}
270268
a(
271269
veryVeryVeryVeryVeryLongFunctionCalledA(
272270
veryVeryVeryVeryVeryLongVariableCalledB
273-
) +
274-
c
271+
) + c
275272
);
276273
return
277274
veryVeryVeryVeryVeryLongFunctionCalledA(
278275
veryVeryVeryVeryVeryLongVariableCalledB
279-
) +
280-
c;
276+
) + c;
281277
}
282278
}
283279
@@ -331,8 +327,7 @@ contract ArithmeticOperators {
331327
veryVeryVeryVeryVeryLongVariableCalledB;
332328
veryVeryVeryVeryVeryLongFunctionCalledA(
333329
veryVeryVeryVeryVeryLongVariableCalledB
334-
) **
335-
c;
330+
)**c;
336331
veryVeryVeryVeryVeryLongFunctionCalledA(
337332
veryVeryVeryVeryVeryLongVariableCalledB
338333
) **
@@ -349,20 +344,17 @@ contract ArithmeticOperators {
349344
if (
350345
veryVeryVeryVeryVeryLongFunctionCalledA(
351346
veryVeryVeryVeryVeryLongVariableCalledB
352-
) **
353-
c
347+
)**c
354348
) {}
355349
a(
356350
veryVeryVeryVeryVeryLongFunctionCalledA(
357351
veryVeryVeryVeryVeryLongVariableCalledB
358-
) **
359-
c
352+
)**c
360353
);
361354
return
362355
veryVeryVeryVeryVeryLongFunctionCalledA(
363356
veryVeryVeryVeryVeryLongVariableCalledB
364-
) **
365-
c;
357+
)**c;
366358
}
367359
}
368360
@@ -494,8 +486,7 @@ contract ComparisonOperators {
494486
veryVeryVeryVeryVeryLongVariableCalledB;
495487
veryVeryVeryVeryVeryLongFunctionCalledA(
496488
veryVeryVeryVeryVeryLongVariableCalledB
497-
) ==
498-
c;
489+
) == c;
499490
veryVeryVeryVeryVeryLongFunctionCalledA(
500491
veryVeryVeryVeryVeryLongVariableCalledB
501492
) <
@@ -512,20 +503,17 @@ contract ComparisonOperators {
512503
if (
513504
veryVeryVeryVeryVeryLongFunctionCalledA(
514505
veryVeryVeryVeryVeryLongVariableCalledB
515-
) ==
516-
c
506+
) == c
517507
) {}
518508
a(
519509
veryVeryVeryVeryVeryLongFunctionCalledA(
520510
veryVeryVeryVeryVeryLongVariableCalledB
521-
) ==
522-
c
511+
) == c
523512
);
524513
return
525514
veryVeryVeryVeryVeryLongFunctionCalledA(
526515
veryVeryVeryVeryVeryLongVariableCalledB
527-
) ==
528-
c;
516+
) == c;
529517
}
530518
}
531519
@@ -611,8 +599,7 @@ contract LogicalOperators {
611599
) {}
612600
if (
613601
(veryVeryVeryVeryVeryLongVariableCalledA &&
614-
veryVeryVeryVeryVeryLongVariableCalledB) ||
615-
c
602+
veryVeryVeryVeryVeryLongVariableCalledB) || c
616603
) {}
617604
if (
618605
veryVeryVeryVeryVeryLongVariableCalledA ||
@@ -632,8 +619,7 @@ contract LogicalOperators {
632619
) {}
633620
while (
634621
(veryVeryVeryVeryVeryLongVariableCalledA &&
635-
veryVeryVeryVeryVeryLongVariableCalledB) ||
636-
c
622+
veryVeryVeryVeryVeryLongVariableCalledB) || c
637623
) {}
638624
while (
639625
veryVeryVeryVeryVeryLongVariableCalledA ||
@@ -656,8 +642,7 @@ contract LogicalOperators {
656642
veryVeryVeryVeryVeryLongVariableCalledB;
657643
veryVeryVeryVeryVeryLongFunctionCalledA(
658644
veryVeryVeryVeryVeryLongVariableCalledB
659-
) ||
660-
c;
645+
) || c;
661646
veryVeryVeryVeryVeryLongFunctionCalledA(
662647
veryVeryVeryVeryVeryLongVariableCalledB
663648
) ||
@@ -674,20 +659,17 @@ contract LogicalOperators {
674659
if (
675660
veryVeryVeryVeryVeryLongFunctionCalledA(
676661
veryVeryVeryVeryVeryLongVariableCalledB
677-
) ||
678-
c
662+
) || c
679663
) {}
680664
a(
681665
veryVeryVeryVeryVeryLongFunctionCalledA(
682666
veryVeryVeryVeryVeryLongVariableCalledB
683-
) ||
684-
c
667+
) || c
685668
);
686669
return
687670
veryVeryVeryVeryVeryLongFunctionCalledA(
688671
veryVeryVeryVeryVeryLongVariableCalledB
689-
) ||
690-
c;
672+
) || c;
691673
}
692674
}
693675

0 commit comments

Comments
 (0)