-
Notifications
You must be signed in to change notification settings - Fork 85
Expand file tree
/
Copy pathcreate-binary-operation-printer.js
More file actions
31 lines (26 loc) · 1.07 KB
/
create-binary-operation-printer.js
File metadata and controls
31 lines (26 loc) · 1.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import { doc } from 'prettier';
import { assignment } from '../assignment.js';
const { group, line } = doc.builders;
const rightOperandPrinter = (node, path, print, options) => {
const right =
options.experimentalOperatorPosition === 'end'
? [' ', node.operator, line, path.call(print, 'right')]
: [line, node.operator, ' ', path.call(print, 'right')];
// If it's a single binary operation, avoid having a small right
// operand like - 1 on its own line
const parent = path.getParentNode();
return node.left.type !== 'BinaryOperation' &&
(parent.type !== 'BinaryOperation' || assignment.match(parent.operator))
? group(right)
: right;
};
export const createBinaryOperationPrinter =
(groupIfNecessaryBuilder, indentIfNecessaryBuilder) =>
(node, path, print, options) => {
const groupIfNecessary = groupIfNecessaryBuilder(path);
const indentIfNecessary = indentIfNecessaryBuilder(path, options);
return groupIfNecessary([
path.call(print, 'left'),
indentIfNecessary(rightOperandPrinter(node, path, print, options))
]);
};