-
Notifications
You must be signed in to change notification settings - Fork 85
Expand file tree
/
Copy pathConditionalExpression.ts
More file actions
167 lines (146 loc) · 5.02 KB
/
ConditionalExpression.ts
File metadata and controls
167 lines (146 loc) · 5.02 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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
import { NonterminalKind } from '@nomicfoundation/slang/cst';
import { doc } from 'prettier';
import { printSeparatedItem } from '../slang-printers/print-separated-item.js';
import { extractVariant } from '../slang-utils/extract-variant.js';
import { SlangNode } from './SlangNode.js';
import { Expression } from './Expression.js';
import type * as ast from '@nomicfoundation/slang/ast';
import type { AstPath, Doc, ParserOptions } from 'prettier';
import type { CollectedMetadata, PrintFunction } from '../types.d.ts';
import type { PrintableNode } from './types.d.ts';
const { group, hardline, ifBreak, indent, line } = doc.builders;
function experimentalTernaries(
node: ConditionalExpression,
path: AstPath<PrintableNode>,
print: PrintFunction,
{ useTabs, tabWidth }: ParserOptions<PrintableNode>
): Doc {
const parent = path.parent!;
const isNested = parent.kind === NonterminalKind.ConditionalExpression;
const isNestedAsTrueExpression = isNested && parent.trueExpression === node;
const falseExpressionVariantKind = node.falseExpression.kind;
const falseExpressionInSameLine =
falseExpressionVariantKind === NonterminalKind.TupleExpression ||
falseExpressionVariantKind === NonterminalKind.ConditionalExpression;
// If the `condition` breaks into multiple lines, we add parentheses,
// unless it already is a `TupleExpression`.
const operand = print('operand');
const operandDoc = group([
node.operand.kind === NonterminalKind.TupleExpression
? operand
: ifBreak(['(', printSeparatedItem(operand), ')'], operand),
' ?'
]);
// 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,
print('trueExpression')
]);
const groupId = Symbol('Slang.ConditionalExpression.trueExpression');
const conditionAndTrueExpressionGroup = group(
[operandDoc, trueExpressionDoc],
{ id: groupId }
);
// For the odd case of `tabWidth` of 1 or 0 we initiate `fillTab` as a single
// space.
const fillTab = useTabs
? '\t'
: tabWidth > 2
? ' '.repeat(tabWidth - 1)
: ' ';
const falseExpression = print('falseExpression');
const falseExpressionDoc = [
isNested ? hardline : line,
':',
falseExpressionInSameLine
? [' ', falseExpression]
: ifBreak(
[fillTab, indent(falseExpression)],
[' ', falseExpression],
// We only add `fillTab` if we are sure the trueExpression is
// indented.
{ groupId }
)
];
return group([conditionAndTrueExpressionGroup, falseExpressionDoc]);
}
function traditionalTernaries(
path: AstPath<PrintableNode>,
print: PrintFunction
): Doc {
return group([
print('operand'),
indent([
// Nested trueExpression and falseExpression are always printed in a new
// line
path.parent!.kind === NonterminalKind.ConditionalExpression
? hardline
: line,
'? ',
print('trueExpression'),
line,
': ',
print('falseExpression')
])
]);
}
function getOperandSingleExpression(
operand: Expression['variant']
): Expression['variant'] | undefined {
return operand.kind === NonterminalKind.TupleExpression
? operand.items.getSingleExpression()
: undefined;
}
export class ConditionalExpression extends SlangNode {
readonly kind = NonterminalKind.ConditionalExpression;
operand: Expression['variant'];
trueExpression: Expression['variant'];
falseExpression: Expression['variant'];
constructor(
ast: ast.ConditionalExpression,
collected: CollectedMetadata,
options: ParserOptions<PrintableNode>
) {
super(ast, collected);
this.operand = extractVariant(
new Expression(ast.operand, collected, options)
);
this.trueExpression = extractVariant(
new Expression(ast.trueExpression, collected, options)
);
this.falseExpression = extractVariant(
new Expression(ast.falseExpression, collected, options)
);
this.updateMetadata(
this.operand,
this.trueExpression,
this.falseExpression
);
if (options.experimentalTernaries) {
// We can remove parentheses only because we are sure that the
// `condition` must be a single `bool` value.
const operandLoc = this.operand.loc;
for (let operandSingleExpression; ; ) {
operandSingleExpression = getOperandSingleExpression(this.operand);
if (
operandSingleExpression === undefined ||
operandSingleExpression.kind === NonterminalKind.ConditionalExpression
)
break;
this.operand = operandSingleExpression;
}
this.operand.loc = operandLoc;
}
}
print(
print: PrintFunction,
path: AstPath<ConditionalExpression>,
options: ParserOptions<PrintableNode>
): Doc {
return options.experimentalTernaries
? experimentalTernaries(this, path, print, options)
: traditionalTernaries(path, print);
}
}