Skip to content

Commit 9de9b48

Browse files
committed
Standardising the printers so that we can review at every level what are the binary operations that that should force grouping and indentation
1 parent 0e96950 commit 9de9b48

14 files changed

+85
-92
lines changed
Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1-
import { defaultBinaryOperationPrinter } from './printers/default-binary-operation-printer.js';
1+
import { binaryOperationPrinter } from './printers/binary-operation-printer.js';
2+
import { bit } from './bit.js';
3+
import { shift } from './shift.js';
4+
import { inequality } from './inequality.js';
5+
import { equality } from './equality.js';
6+
import { logical } from './logical.js';
27

38
export const addition = {
49
match: (op) => ['+', '-'].includes(op),
5-
print: defaultBinaryOperationPrinter
6-
// grouping and indenting before `bit` and `shift` should technically be here
7-
// but they are properly parenthesised before reaching this point.
10+
print: binaryOperationPrinter([shift, bit, inequality, equality, logical])
811
};

src/binary-operator-printers/bit.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1-
import { defaultBinaryOperationPrinter } from './printers/default-binary-operation-printer.js';
1+
import { binaryOperationPrinter } from './printers/binary-operation-printer.js';
2+
import { inequality } from './inequality.js';
3+
import { equality } from './equality.js';
4+
import { logical } from './logical.js';
25

36
export const bit = {
47
match: (op) => ['&', '|', '^'].includes(op),
5-
print: defaultBinaryOperationPrinter
8+
print: binaryOperationPrinter([inequality, equality, logical])
69
};
Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,7 @@
1-
import { createGroupedBinaryOperationPrinter } from './printers/create-grouped-binary-operation-printer.js';
2-
import { createComparisonIndentIfNecessaryBuilder } from './printers/create-indent-if-necessary-builder.js';
1+
import { comparisonOperationPrinter } from './printers/comparison-operation-printer.js';
32
import { logical } from './logical.js';
43

5-
const equalityPrinter = createGroupedBinaryOperationPrinter(
6-
createComparisonIndentIfNecessaryBuilder([logical])
7-
);
8-
94
export const equality = {
105
match: (op) => ['==', '!='].includes(op),
11-
print: equalityPrinter
6+
print: comparisonOperationPrinter([logical])
127
};
Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,28 @@
1-
import { createGroupedBinaryOperationPrinter } from './printers/create-grouped-binary-operation-printer.js';
2-
import { createArithmeticIndentIfNecessaryBuilder } from './printers/create-indent-if-necessary-builder.js';
1+
import { doc } from 'prettier';
2+
import { createBinaryOperationPrinter } from './printers/create-binary-operation-printer.js';
3+
import { createBinaryIndentIfNecessaryBuilder } from './printers/binary-operation-printer.js';
4+
import { multiplication } from './multiplication.js';
35
import { addition } from './addition.js';
4-
import { equality } from './equality.js';
6+
import { shift } from './shift.js';
7+
import { bit } from './bit.js';
58
import { inequality } from './inequality.js';
6-
import { multiplication } from './multiplication.js';
9+
import { equality } from './equality.js';
10+
import { logical } from './logical.js';
711

8-
const exponentiationPrinter = createGroupedBinaryOperationPrinter(
9-
createArithmeticIndentIfNecessaryBuilder([
10-
addition,
11-
equality,
12-
inequality,
13-
multiplication
14-
// `bit` and `shift` should technically be here but they are properly
15-
// parenthesised before reaching this point.
16-
])
17-
);
12+
const { group } = doc.builders;
1813

1914
export const exponentiation = {
2015
match: (op) => op === '**',
21-
print: exponentiationPrinter
16+
print: createBinaryOperationPrinter(
17+
() => (document) => group(document), // always group
18+
createBinaryIndentIfNecessaryBuilder([
19+
multiplication,
20+
addition,
21+
shift,
22+
bit,
23+
inequality,
24+
equality,
25+
logical
26+
])
27+
)
2228
};
Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,8 @@
1-
import { createGroupedBinaryOperationPrinter } from './printers/create-grouped-binary-operation-printer.js';
2-
import { createComparisonIndentIfNecessaryBuilder } from './printers/create-indent-if-necessary-builder.js';
1+
import { comparisonOperationPrinter } from './printers/comparison-operation-printer.js';
32
import { logical } from './logical.js';
43
import { equality } from './equality.js';
54

6-
const inequalityPrinter = createGroupedBinaryOperationPrinter(
7-
createComparisonIndentIfNecessaryBuilder([logical, equality])
8-
);
9-
105
export const inequality = {
116
match: (op) => ['<', '>', '<=', '>='].includes(op),
12-
print: inequalityPrinter
7+
print: comparisonOperationPrinter([logical, equality])
138
};

src/binary-operator-printers/logical.js

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,10 @@ const indentIfNecessaryBuilder = (path, options) => (document) => {
2323
}
2424
};
2525

26-
const logicalPrinter = createBinaryOperationPrinter(
27-
createGroupIfNecessaryBuilder([]),
28-
indentIfNecessaryBuilder
29-
);
30-
3126
export const logical = {
3227
match: (op) => ['&&', '||'].includes(op),
33-
print: logicalPrinter
28+
print: createBinaryOperationPrinter(
29+
createGroupIfNecessaryBuilder([]),
30+
indentIfNecessaryBuilder
31+
)
3432
};
Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,19 @@
1-
import { createBinaryOperationPrinter } from './printers/create-binary-operation-printer.js';
2-
import { createGroupIfNecessaryBuilder } from './printers/create-group-if-necessary-builder.js';
3-
import { createArithmeticIndentIfNecessaryBuilder } from './printers/create-indent-if-necessary-builder.js';
1+
import { binaryOperationPrinter } from './printers/binary-operation-printer.js';
42
import { addition } from './addition.js';
53
import { bit } from './bit.js';
64
import { equality } from './equality.js';
75
import { inequality } from './inequality.js';
86
import { shift } from './shift.js';
9-
10-
const matchers = [addition, bit, equality, inequality, shift];
11-
12-
const multiplicationPrinter = createBinaryOperationPrinter(
13-
createGroupIfNecessaryBuilder(matchers),
14-
createArithmeticIndentIfNecessaryBuilder(matchers)
15-
);
7+
import { logical } from './logical.js';
168

179
export const multiplication = {
1810
match: (op) => ['*', '/', '%'].includes(op),
19-
print: multiplicationPrinter
11+
print: binaryOperationPrinter([
12+
addition,
13+
shift,
14+
bit,
15+
inequality,
16+
equality,
17+
logical
18+
])
2019
};
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { createBinaryOperationPrinter } from './create-binary-operation-printer.js';
2+
import { createGroupIfNecessaryBuilder } from './create-group-if-necessary-builder.js';
3+
import { createIndentIfNecessaryBuilder } from './create-indent-if-necessary-builder.js';
4+
5+
export const createBinaryIndentIfNecessaryBuilder =
6+
createIndentIfNecessaryBuilder(['ReturnStatement']);
7+
8+
export const binaryOperationPrinter = (shouldGroupAndIndentMatchers) =>
9+
createBinaryOperationPrinter(
10+
createGroupIfNecessaryBuilder(shouldGroupAndIndentMatchers),
11+
createBinaryIndentIfNecessaryBuilder(shouldGroupAndIndentMatchers)
12+
);
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { createBinaryOperationPrinter } from './create-binary-operation-printer.js';
2+
import { createGroupIfNecessaryBuilder } from './create-group-if-necessary-builder.js';
3+
import { createIndentIfNecessaryBuilder } from './create-indent-if-necessary-builder.js';
4+
5+
const createComparisonIndentIfNecessaryBuilder = createIndentIfNecessaryBuilder(
6+
['ReturnStatement', 'IfStatement', 'ForStatement', 'WhileStatement']
7+
);
8+
export const comparisonOperationPrinter = (shouldGroupAndIndentMatchers) =>
9+
createBinaryOperationPrinter(
10+
createGroupIfNecessaryBuilder(shouldGroupAndIndentMatchers),
11+
createComparisonIndentIfNecessaryBuilder(shouldGroupAndIndentMatchers)
12+
);

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@ import { shouldGroupOrIndent } from '../utils/should-group-or-indent.js';
44
const { group } = doc.builders;
55

66
export const createGroupIfNecessaryBuilder =
7-
(matchers) => (path) => (document) => {
7+
(shouldIndentMatchers) => (path) => (document) => {
88
const parentNode = path.getParentNode();
9-
if (shouldGroupOrIndent(parentNode, matchers)) return group(document);
9+
if (shouldGroupOrIndent(parentNode, shouldIndentMatchers))
10+
return group(document);
1011
return document;
1112
};

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

Lines changed: 0 additions & 10 deletions
This file was deleted.

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

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

44
const { indent } = doc.builders;
55

6-
const createIndentIfNecessaryBuilder =
6+
export const createIndentIfNecessaryBuilder =
77
(notIndentParentTypes) => (shouldIndentMatchers) => (path) => (document) => {
88
let node = path.getNode();
99
for (let i = 0; ; i += 1) {
@@ -15,14 +15,3 @@ const createIndentIfNecessaryBuilder =
1515
node = parentNode;
1616
}
1717
};
18-
19-
export const createArithmeticIndentIfNecessaryBuilder =
20-
createIndentIfNecessaryBuilder(['ReturnStatement']);
21-
22-
export const createComparisonIndentIfNecessaryBuilder =
23-
createIndentIfNecessaryBuilder([
24-
'ReturnStatement',
25-
'IfStatement',
26-
'ForStatement',
27-
'WhileStatement'
28-
]);

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

Lines changed: 0 additions & 12 deletions
This file was deleted.

src/binary-operator-printers/shift.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1-
import { defaultBinaryOperationPrinter } from './printers/default-binary-operation-printer.js';
1+
import { binaryOperationPrinter } from './printers/binary-operation-printer.js';
2+
import { bit } from './bit.js';
3+
import { inequality } from './inequality.js';
4+
import { equality } from './equality.js';
5+
import { logical } from './logical.js';
26

37
export const shift = {
48
match: (op) => ['<<', '>>'].includes(op),
5-
print: defaultBinaryOperationPrinter
6-
// grouping and indenting before `bit` should technically be here but they
7-
// are properly parenthesised before reaching this point.
9+
print: binaryOperationPrinter([bit, inequality, equality, logical])
810
};

0 commit comments

Comments
 (0)