|
1 | 1 | import { doc } from 'prettier'; |
| 2 | +import { printSeparatedItem } from '../common/printer-helpers.js'; |
2 | 3 |
|
3 | | -const { group, indent, line } = doc.builders; |
| 4 | +const { group, hardline, ifBreak, indent, line, softline } = doc.builders; |
4 | 5 |
|
5 | | -export const Conditional = { |
6 | | - print: ({ path, print }) => |
7 | | - group([ |
8 | | - path.call(print, 'condition'), |
9 | | - indent([ |
10 | | - line, |
11 | | - '? ', |
12 | | - path.call(print, 'trueExpression'), |
13 | | - line, |
14 | | - ': ', |
15 | | - path.call(print, 'falseExpression') |
16 | | - ]) |
| 6 | +let groupIndex = 0; |
| 7 | +const experimentalTernaries = (node, path, print, options) => { |
| 8 | + const parent = path.getParentNode(); |
| 9 | + const isNested = parent.type === 'Conditional'; |
| 10 | + const isNestedAsTrueExpression = isNested && parent.trueExpression === node; |
| 11 | + const falseExpressionIsNested = node.falseExpression.type === 'Conditional'; |
| 12 | + |
| 13 | + // If the `condition` breaks into multiple lines, we add parentheses, |
| 14 | + // unless it already is a `TupleExpression`. |
| 15 | + const condition = path.call(print, 'condition'); |
| 16 | + const conditionDoc = group([ |
| 17 | + node.condition.type === 'TupleExpression' |
| 18 | + ? condition |
| 19 | + : ifBreak(['(', printSeparatedItem(condition), ')'], condition), |
| 20 | + ' ?' |
| 21 | + ]); |
| 22 | + |
| 23 | + // To switch between "case-style" and "curious" ternaries we force a new line |
| 24 | + // before a nested `trueExpression` if the current `Conditional` is also a |
| 25 | + // `trueExpression`. |
| 26 | + const trueExpressionDoc = indent([ |
| 27 | + isNestedAsTrueExpression ? hardline : line, |
| 28 | + path.call(print, 'trueExpression') |
| 29 | + ]); |
| 30 | + |
| 31 | + const conditionAndTrueExpressionGroup = group( |
| 32 | + [conditionDoc, trueExpressionDoc], |
| 33 | + { id: `Conditional.trueExpressionDoc-${groupIndex}` } |
| 34 | + ); |
| 35 | + |
| 36 | + groupIndex += 1; |
| 37 | + |
| 38 | + // For the odd case of `tabWidth` of 1 or 0 we initiate `fillTab` as a single |
| 39 | + // space. |
| 40 | + let fillTab = ' '; |
| 41 | + if ( |
| 42 | + !falseExpressionIsNested && // avoid processing if it's not needed |
| 43 | + (options.tabWidth > 2 || options.useTabs) |
| 44 | + ) { |
| 45 | + fillTab = options.useTabs ? '\t' : ' '.repeat(options.tabWidth - 1); |
| 46 | + } |
| 47 | + |
| 48 | + // A nested `falseExpression` is always printed in a new line. |
| 49 | + const falseExpression = path.call(print, 'falseExpression'); |
| 50 | + const falseExpressionDoc = [ |
| 51 | + isNested ? hardline : line, |
| 52 | + ':', |
| 53 | + falseExpressionIsNested |
| 54 | + ? [' ', falseExpression] |
| 55 | + : ifBreak([fillTab, indent(falseExpression)], [' ', falseExpression], { |
| 56 | + // We only add `fillTab` if we are sure the trueExpression is indented |
| 57 | + groupId: conditionAndTrueExpressionGroup.id |
| 58 | + }) |
| 59 | + ]; |
| 60 | + |
| 61 | + const document = group([conditionAndTrueExpressionGroup, falseExpressionDoc]); |
| 62 | + |
| 63 | + return parent.type === 'VariableDeclarationStatement' |
| 64 | + ? indent([softline, document]) |
| 65 | + : document; |
| 66 | +}; |
| 67 | + |
| 68 | +const traditionalTernaries = (path, print) => |
| 69 | + group([ |
| 70 | + path.call(print, 'condition'), |
| 71 | + indent([ |
| 72 | + // Nested trueExpression and falseExpression are always printed in a new |
| 73 | + // line |
| 74 | + path.getParentNode().type === 'Conditional' ? hardline : line, |
| 75 | + '? ', |
| 76 | + path.call(print, 'trueExpression'), |
| 77 | + line, |
| 78 | + ': ', |
| 79 | + path.call(print, 'falseExpression') |
17 | 80 | ]) |
| 81 | + ]); |
| 82 | + |
| 83 | +export const Conditional = { |
| 84 | + print: ({ node, path, print, options }) => |
| 85 | + options.experimentalTernaries |
| 86 | + ? experimentalTernaries(node, path, print, options) |
| 87 | + : traditionalTernaries(path, print) |
18 | 88 | }; |
0 commit comments