|
1 | 1 | import { doc } from 'prettier'; |
| 2 | +import { assignment } from '../assignment.js'; |
2 | 3 |
|
3 | | -const { line } = doc.builders; |
| 4 | +const { group, line } = doc.builders; |
4 | 5 |
|
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')]; |
| 6 | +export 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')]; |
| 11 | + |
| 12 | + // If it's a single binary operation, avoid having a small right |
| 13 | + // operand like - 1 on its own line |
| 14 | + const parent = path.getParentNode(); |
| 15 | + return node.left.type !== 'BinaryOperation' && |
| 16 | + (parent.type !== 'BinaryOperation' || assignment.match(parent.operator)) |
| 17 | + ? group(right) |
| 18 | + : right; |
| 19 | +}; |
0 commit comments