Skip to content

Commit 8d645f7

Browse files
committed
support for experimentalOperatorPosition
1 parent afe81b0 commit 8d645f7

File tree

5 files changed

+1217
-232
lines changed

5 files changed

+1217
-232
lines changed

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,11 @@ import { assignment } from '../assignment.js';
33

44
const { group, line } = doc.builders;
55

6-
const rightOperandPrinter = (node, path, print) => {
7-
const right = [' ', node.operator, line, path.call(print, 'right')];
6+
const rightOperandPrinter = (node, path, print, options) => {
7+
const right =
8+
options.experimentalOperatorPosition === 'end'
9+
? [' ', node.operator, line, path.call(print, 'right')]
10+
: [line, node.operator, ' ', path.call(print, 'right')];
811

912
// If it's a single binary operation, avoid having a small right
1013
// operand like - 1 on its own line
@@ -23,6 +26,6 @@ export const createBinaryOperationPrinter =
2326

2427
return groupIfNecessary([
2528
path.call(print, 'left'),
26-
indentIfNecessary(rightOperandPrinter(node, path, print))
29+
indentIfNecessary(rightOperandPrinter(node, path, print, options))
2730
]);
2831
};

src/options.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,22 @@ const options: SupportOptions = {
4848
oppositeDescription:
4949
'Default behavior of ternaries; keep question marks on the same line as the consequent.'
5050
},
51+
experimentalOperatorPosition: {
52+
category: CATEGORY_JAVASCRIPT,
53+
type: 'choice',
54+
default: 'end',
55+
description: 'Where to print operators when binary expressions wrap lines.',
56+
choices: [
57+
{
58+
value: 'start',
59+
description: 'Print operators at the start of new lines.'
60+
},
61+
{
62+
value: 'end',
63+
description: 'Print operators at the end of previous lines.'
64+
}
65+
]
66+
},
5167
compiler: {
5268
category: CATEGORY_SOLIDITY,
5369
type: 'string',

src/slang-printers/create-binary-operation-printer.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,14 @@ const { group, line } = doc.builders;
1515
function rightOperandPrint(
1616
node: BinaryOperation,
1717
path: AstPath<BinaryOperation>,
18-
print: PrintFunction
18+
print: PrintFunction,
19+
options: ParserOptions<AstNode>
1920
): Doc {
20-
const rightOperand = [
21-
` ${node.operator}`,
22-
line,
23-
path.call(print, 'rightOperand')
24-
];
21+
const rightOperand =
22+
options.experimentalOperatorPosition === 'end'
23+
? [` ${node.operator}`, line, path.call(print, 'rightOperand')]
24+
: [line, `${node.operator} `, path.call(print, 'rightOperand')];
25+
2526
// If it's a single binary operation, avoid having a small right
2627
// operand like - 1 on its own line
2728
const shouldGroup =
@@ -54,6 +55,6 @@ export const createBinaryOperationPrinter =
5455

5556
return groupRules([
5657
path.call(print, 'leftOperand'),
57-
indentRules(rightOperandPrint(node, path, print))
58+
indentRules(rightOperandPrint(node, path, print, options))
5859
]);
5960
};

0 commit comments

Comments
 (0)