-
Notifications
You must be signed in to change notification settings - Fork 85
Expand file tree
/
Copy pathConditionalExpression.ts
More file actions
157 lines (134 loc) · 5.02 KB
/
ConditionalExpression.ts
File metadata and controls
157 lines (134 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
import { doc } from 'prettier';
import { NonterminalKind } from '@nomicfoundation/slang/cst';
import { printSeparatedItem } from '../slang-printers/print-separated-item.js';
import { getNodeMetadata, updateMetadata } from '../slang-utils/metadata.js';
import { Expression } from './Expression.js';
import type * as ast from '@nomicfoundation/slang/ast';
import type { AstPath, Doc, ParserOptions } from 'prettier';
import type { AstNode, StrictAstNode } from './types.d.ts';
import type { PrintFunction, SlangNode } from '../types.d.ts';
const { group, hardline, ifBreak, indent, line, softline } = doc.builders;
function fillTab(options: ParserOptions<AstNode>): Doc {
if (options.useTabs) return '\t';
// For the odd case of `tabWidth` of 1 or 0 we initiate `fillTab` as a single
// space.
return options.tabWidth > 2 ? ' '.repeat(options.tabWidth - 1) : ' ';
}
function experimentalTernaries(
node: ConditionalExpression,
path: AstPath<ConditionalExpression>,
print: PrintFunction,
options: ParserOptions<AstNode>
): Doc {
const grandparent = path.getNode(2) as StrictAstNode;
const isNested = grandparent.kind === NonterminalKind.ConditionalExpression;
const isNestedAsTrueExpression =
isNested && grandparent.trueExpression.variant === node;
const falseExpressionInSameLine =
node.falseExpression.variant.kind === NonterminalKind.TupleExpression ||
node.falseExpression.variant.kind === NonterminalKind.ConditionalExpression;
// If the `condition` breaks into multiple lines, we add parentheses,
// unless it already is a `TupleExpression`.
const operand = path.call(print, 'operand');
const operandDoc = group([
node.operand.variant.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,
path.call(print, 'trueExpression')
]);
const conditionAndTrueExpressionGroup = group(
[operandDoc, trueExpressionDoc],
{ id: Symbol('Slang.ConditionalExpression.trueExpression') }
);
const falseExpression = path.call(print, 'falseExpression');
const falseExpressionDoc = [
isNested ? hardline : line,
':',
falseExpressionInSameLine
? [' ', falseExpression]
: ifBreak(
[fillTab(options), indent(falseExpression)],
[' ', falseExpression],
{
// We only add `fillTab` if we are sure the trueExpression is indented
groupId: conditionAndTrueExpressionGroup.id
}
)
];
const document = group([conditionAndTrueExpressionGroup, falseExpressionDoc]);
return grandparent.kind === NonterminalKind.VariableDeclarationValue
? group(indent([softline, document]))
: document;
}
function traditionalTernaries(
path: AstPath<ConditionalExpression>,
print: PrintFunction
): Doc {
return group([
path.call(print, 'operand'),
indent([
// Nested trueExpression and falseExpression are always printed in a new
// line
(path.getNode(2) as StrictAstNode).kind ===
NonterminalKind.ConditionalExpression
? hardline
: line,
'? ',
path.call(print, 'trueExpression'),
line,
': ',
path.call(print, 'falseExpression')
])
]);
}
export class ConditionalExpression implements SlangNode {
readonly kind = NonterminalKind.ConditionalExpression;
comments;
loc;
operand: Expression;
trueExpression: Expression;
falseExpression: Expression;
constructor(ast: ast.ConditionalExpression, options: ParserOptions<AstNode>) {
let metadata = getNodeMetadata(ast);
this.operand = new Expression(ast.operand, options);
this.trueExpression = new Expression(ast.trueExpression, options);
this.falseExpression = new Expression(ast.falseExpression, options);
metadata = updateMetadata(metadata, [
this.operand,
this.trueExpression,
this.falseExpression
]);
this.comments = metadata.comments;
this.loc = metadata.loc;
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;
while (
this.operand.variant.kind === NonterminalKind.TupleExpression &&
this.operand.variant.items.items.length === 1 &&
this.operand.variant.items.items[0].expression!.variant.kind !==
NonterminalKind.ConditionalExpression
) {
this.operand = this.operand.variant.items.items[0].expression!;
}
this.operand.loc = operandLoc;
}
}
print(
path: AstPath<ConditionalExpression>,
print: PrintFunction,
options: ParserOptions<AstNode>
): Doc {
return options.experimentalTernaries
? experimentalTernaries(this, path, print, options)
: traditionalTernaries(path, print);
}
}