Skip to content

Commit e2b8464

Browse files
committed
support for experimentalOperatorPosition
1 parent e26ee1d commit e2b8464

File tree

7 files changed

+1219
-228
lines changed

7 files changed

+1219
-228
lines changed

src/binary-operator-printers/comparison.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import { doc } from 'prettier';
2+
import { rightOperandPrinter } from './printers/right-operand-printer.js';
23
import { logical } from './logical.js';
34

4-
const { group, indent, line } = doc.builders;
5+
const { group, indent } = doc.builders;
56

67
const indentIfNecessaryBuilder = (path) => (document) => {
78
let node = path.getNode();
@@ -20,18 +21,17 @@ const indentIfNecessaryBuilder = (path) => (document) => {
2021

2122
export const comparison = {
2223
match: (op) => ['<', '>', '<=', '>=', '==', '!='].includes(op),
23-
print: (node, path, print) => {
24+
print: (node, path, print, options) => {
2425
const indentIfNecessary = indentIfNecessaryBuilder(path);
2526

26-
const right = [node.operator, line, path.call(print, 'right')];
27+
const right = rightOperandPrinter(node, path, print, options);
2728
// If it's a single binary operation, avoid having a small right
2829
// operand like - 1 on its own line
2930
const shouldGroup =
3031
node.left.type !== 'BinaryOperation' &&
3132
path.getParentNode().type !== 'BinaryOperation';
3233
return group([
3334
path.call(print, 'left'),
34-
' ',
3535
indentIfNecessary(shouldGroup ? group(right) : right)
3636
]);
3737
}

src/binary-operator-printers/logical.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { doc } from 'prettier';
2+
import { rightOperandPrinter } from './printers/right-operand-printer.js';
23

3-
const { group, line, indent } = doc.builders;
4+
const { group, indent } = doc.builders;
45

56
const groupIfNecessaryBuilder = (path) => (document) =>
67
path.getParentNode().type === 'BinaryOperation' ? document : group(document);
@@ -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 = rightOperandPrinter(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/binary-operator-printers/printers/create-binary-operation-printer.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import { doc } from 'prettier';
22
import { assignment } from '../assignment.js';
33
import { comparison } from '../comparison.js';
4+
import { rightOperandPrinter } from './right-operand-printer.js';
45

5-
const { group, indent, line } = doc.builders;
6+
const { group, indent } = doc.builders;
67

78
const shouldGroupOrIndent = (node, matchers) =>
89
matchers.some((matcher) => matcher.match(node.operator));
@@ -41,11 +42,11 @@ export const createIndentIfNecessaryBuilder =
4142

4243
export const createBinaryOperationPrinter =
4344
(groupIfNecessaryBuilder, indentIfNecessaryBuilder) =>
44-
(node, path, print) => {
45+
(node, path, print, options) => {
4546
const groupIfNecessary = groupIfNecessaryBuilder(path);
4647
const indentIfNecessary = indentIfNecessaryBuilder(path);
4748

48-
const right = [' ', node.operator, line, path.call(print, 'right')];
49+
const right = rightOperandPrinter(node, path, print, options);
4950
// If it's a single binary operation, avoid having a small right
5051
// operand like - 1 on its own line
5152
const parent = path.getParentNode();
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { doc } from 'prettier';
2+
3+
const { line } = doc.builders;
4+
5+
export const rightOperandPrinter = (node, path, print, options) =>
6+
options.experimentalOperatorPosition === 'end'
7+
? [' ', node.operator, line, path.call(print, 'right')]
8+
: [line, node.operator, ' ', path.call(print, 'right')];

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)