Skip to content

Commit 91768c0

Browse files
committed
support for experimentalOperatorPosition
1 parent 7063199 commit 91768c0

File tree

7 files changed

+1221
-233
lines changed

7 files changed

+1221
-233
lines changed

src/binary-operator-printers/arithmetic.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,21 +30,25 @@ export const indentIfNecessaryBuilder = (path) => (document) => {
3030
}
3131
};
3232

33+
export const rightOperand = (node, path, print, options) =>
34+
options.experimentalOperatorPosition === 'end'
35+
? [' ', node.operator, line, path.call(print, 'right')]
36+
: [line, node.operator, ' ', path.call(print, 'right')];
37+
3338
export const arithmetic = {
3439
match: (op) => ['+', '-', '*', '/', '%'].includes(op),
35-
print: (node, path, print) => {
40+
print: (node, path, print, options) => {
3641
const groupIfNecessary = groupIfNecessaryBuilder(path);
3742
const indentIfNecessary = indentIfNecessaryBuilder(path);
3843

39-
const right = [node.operator, line, path.call(print, 'right')];
44+
const right = rightOperand(node, path, print, options);
4045
// If it's a single binary operation, avoid having a small right
4146
// operand like - 1 on its own line
4247
const shouldGroup =
4348
node.left.type !== 'BinaryOperation' &&
4449
path.getParentNode().type !== 'BinaryOperation';
4550
return groupIfNecessary([
4651
path.call(print, 'left'),
47-
' ',
4852
indentIfNecessary(shouldGroup ? group(right) : right)
4953
]);
5054
}

src/binary-operator-printers/comparison.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { doc } from 'prettier';
2+
import { rightOperand } from './arithmetic.js';
23

34
const { group, indent, line } = doc.builders;
45

@@ -18,18 +19,17 @@ const indentIfNecessaryBuilder = (path) => (document) => {
1819

1920
export const comparison = {
2021
match: (op) => ['<', '>', '<=', '>=', '==', '!='].includes(op),
21-
print: (node, path, print) => {
22+
print: (node, path, print, options) => {
2223
const indentIfNecessary = indentIfNecessaryBuilder(path);
2324

24-
const right = [node.operator, line, path.call(print, 'right')];
25+
const right = rightOperand(node, path, print, options);
2526
// If it's a single binary operation, avoid having a small right
2627
// operand like - 1 on its own line
2728
const shouldGroup =
2829
node.left.type !== 'BinaryOperation' &&
2930
path.getParentNode().type !== 'BinaryOperation';
3031
return group([
3132
path.call(print, 'left'),
32-
' ',
3333
indentIfNecessary(shouldGroup ? group(right) : right)
3434
]);
3535
}

src/binary-operator-printers/exponentiation.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import { doc } from 'prettier';
2-
import { indentIfNecessaryBuilder } from './arithmetic.js';
2+
import { indentIfNecessaryBuilder, rightOperand } from './arithmetic.js';
33

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

66
export const exponentiation = {
77
match: (op) => op === '**',
8-
print: (node, path, print) => {
8+
print: (node, path, print, options) => {
99
const indentIfNecessary = indentIfNecessaryBuilder(path);
10-
const right = [' ', node.operator, line, path.call(print, 'right')];
10+
const right = rightOperand(node, path, print, options);
1111
// If it's a single binary operation, avoid having a small right
1212
// operand like - 1 on its own line
1313
const shouldGroup =

src/binary-operator-printers/logical.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { doc } from 'prettier';
2+
import { rightOperand } from './arithmetic.js';
23

34
const { group, line, indent } = doc.builders;
45

@@ -30,15 +31,14 @@ export const logical = {
3031
const groupIfNecessary = groupIfNecessaryBuilder(path);
3132
const indentIfNecessary = indentIfNecessaryBuilder(path, options);
3233

33-
const right = [node.operator, line, path.call(print, 'right')];
34+
const right = rightOperand(node, path, print, options);
3435
// If it's a single binary operation, avoid having a small right
3536
// operand like - 1 on its own line
3637
const shouldGroup =
3738
node.left.type !== 'BinaryOperation' &&
3839
path.getParentNode().type !== 'BinaryOperation';
3940
return groupIfNecessary([
4041
path.call(print, 'left'),
41-
' ',
4242
indentIfNecessary(shouldGroup ? group(right) : right)
4343
]);
4444
}

src/options.js

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

0 commit comments

Comments
 (0)