Skip to content

Commit 95236e2

Browse files
committed
using the same pattern we developed for the logical operators as well
1 parent 49eb32a commit 95236e2

File tree

5 files changed

+15
-20
lines changed

5 files changed

+15
-20
lines changed
Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
import { doc } from 'prettier';
2-
import { rightOperandPrinter } from './printers/right-operand-printer.js';
2+
import { createBinaryOperationPrinter } from './printers/create-binary-operation-printer.js';
3+
import { createGroupIfNecessaryBuilder } from './printers/create-group-if-necessary-builder.js';
34

4-
const { group, indent } = doc.builders;
5-
6-
const groupIfNecessaryBuilder = (path) => (document) =>
7-
path.getParentNode().type === 'BinaryOperation' ? document : group(document);
5+
const { indent } = doc.builders;
86

97
const indentIfNecessaryBuilder = (path, options) => (document) => {
108
let node = path.getNode();
@@ -25,15 +23,12 @@ const indentIfNecessaryBuilder = (path, options) => (document) => {
2523
}
2624
};
2725

26+
const logicalPrinter = createBinaryOperationPrinter(
27+
createGroupIfNecessaryBuilder([]),
28+
indentIfNecessaryBuilder
29+
);
30+
2831
export const logical = {
2932
match: (op) => ['&&', '||'].includes(op),
30-
print: (node, path, print, options) => {
31-
const groupIfNecessary = groupIfNecessaryBuilder(path);
32-
const indentIfNecessary = indentIfNecessaryBuilder(path, options);
33-
34-
return groupIfNecessary([
35-
path.call(print, 'left'),
36-
indentIfNecessary(rightOperandPrinter(node, path, print, options))
37-
]);
38-
}
33+
print: logicalPrinter
3934
};

src/binary-operator-printers/multiplication.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { createBinaryOperationPrinter } from './printers/create-binary-operation-printer.js';
2-
import { createArithmeticGroupIfNecessaryBuilder } from './printers/create-group-if-necessary-builder.js';
2+
import { createGroupIfNecessaryBuilder } from './printers/create-group-if-necessary-builder.js';
33
import { createArithmeticIndentIfNecessaryBuilder } from './printers/create-indent-if-necessary-builder.js';
44
import { addition } from './addition.js';
55
import { bit } from './bit.js';
@@ -10,7 +10,7 @@ import { shift } from './shift.js';
1010
const matchers = [addition, bit, equality, inequality, shift];
1111

1212
const multiplicationPrinter = createBinaryOperationPrinter(
13-
createArithmeticGroupIfNecessaryBuilder(matchers),
13+
createGroupIfNecessaryBuilder(matchers),
1414
createArithmeticIndentIfNecessaryBuilder(matchers)
1515
);
1616

src/binary-operator-printers/printers/create-binary-operation-printer.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ export const createBinaryOperationPrinter =
44
(groupIfNecessaryBuilder, indentIfNecessaryBuilder) =>
55
(node, path, print, options) => {
66
const groupIfNecessary = groupIfNecessaryBuilder(path);
7-
const indentIfNecessary = indentIfNecessaryBuilder(path);
7+
const indentIfNecessary = indentIfNecessaryBuilder(path, options);
88

99
return groupIfNecessary([
1010
path.call(print, 'left'),

src/binary-operator-printers/printers/create-group-if-necessary-builder.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { shouldGroupOrIndent } from '../utils/should-group-or-indent.js';
33

44
const { group } = doc.builders;
55

6-
export const createArithmeticGroupIfNecessaryBuilder =
6+
export const createGroupIfNecessaryBuilder =
77
(matchers) => (path) => (document) => {
88
const parentNode = path.getParentNode();
99
if (shouldGroupOrIndent(parentNode, matchers)) return group(document);
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import { createBinaryOperationPrinter } from './create-binary-operation-printer.js';
2-
import { createArithmeticGroupIfNecessaryBuilder } from './create-group-if-necessary-builder.js';
2+
import { createGroupIfNecessaryBuilder } from './create-group-if-necessary-builder.js';
33
import { createArithmeticIndentIfNecessaryBuilder } from './create-indent-if-necessary-builder.js';
44
import { equality } from '../equality.js';
55
import { inequality } from '../inequality.js';
66

77
const comparisonMatchers = [equality, inequality];
88

99
export const defaultBinaryOperationPrinter = createBinaryOperationPrinter(
10-
createArithmeticGroupIfNecessaryBuilder(comparisonMatchers),
10+
createGroupIfNecessaryBuilder(comparisonMatchers),
1111
createArithmeticIndentIfNecessaryBuilder(comparisonMatchers)
1212
);

0 commit comments

Comments
 (0)