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