Skip to content

Commit a7d2c61

Browse files
authored
adding support for experimentalOperatorPosition (#1162)
* support for experimentalOperatorPosition * fixing lint
1 parent a8bb692 commit a7d2c61

7 files changed

Lines changed: 1222 additions & 240 deletions

File tree

package-lock.json

Lines changed: 13 additions & 12 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,16 +88,16 @@
8888
"@eslint/js": "^9.28.0",
8989
"@types/jest": "^29.5.14",
9090
"@types/semver": "^7.7.0",
91-
"@typescript-eslint/eslint-plugin": "^8.33.1",
92-
"@typescript-eslint/parser": "^8.33.1",
91+
"@typescript-eslint/eslint-plugin": "^8.34.0",
92+
"@typescript-eslint/parser": "^8.34.0",
9393
"c8": "^10.1.3",
9494
"cross-env": "^7.0.3",
9595
"eslint": "^9.28.0",
9696
"eslint-config-prettier": "^10.1.5",
9797
"esm-utils": "^4.4.2",
9898
"globals": "^16.2.0",
9999
"jest": "^29.7.0",
100-
"jest-light-runner": "^0.7.8",
100+
"jest-light-runner": "^0.7.9",
101101
"jest-snapshot-serializer-ansi": "^2.2.1",
102102
"jest-snapshot-serializer-raw": "^2.0.0",
103103
"jest-watch-typeahead": "^2.2.2",

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 leftOperand = node.leftOperand.variant;
@@ -58,6 +59,6 @@ export const createBinaryOperationPrinter =
5859

5960
return groupRules([
6061
path.call(print, 'leftOperand'),
61-
indentRules(rightOperandPrint(node, path, print))
62+
indentRules(rightOperandPrint(node, path, print, options))
6263
]);
6364
};

0 commit comments

Comments
 (0)