Skip to content

Commit 5ff26e2

Browse files
committed
reorganise ConditionalExpression
1 parent 9a6fdea commit 5ff26e2

1 file changed

Lines changed: 20 additions & 26 deletions

File tree

src/slang-nodes/ConditionalExpression.ts

Lines changed: 20 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
/* eslint-disable no-nested-ternary */
21
import { doc } from 'prettier';
32
import { NonterminalKind } from '@nomicfoundation/slang/kinds/index.js';
43
import { printSeparatedItem } from '../slang-printers/print-separated-item.js';
@@ -12,6 +11,13 @@ import type { PrintFunction, SlangNode } from '../types';
1211

1312
const { group, hardline, ifBreak, indent, line, softline } = doc.builders;
1413

14+
function fillTab(options: ParserOptions<AstNode>): Doc {
15+
if (options.useTabs) return '\t';
16+
// For the odd case of `tabWidth` of 1 or 0 we initiate `fillTab` as a single
17+
// space.
18+
return options.tabWidth > 2 ? ' '.repeat(options.tabWidth - 1) : ' ';
19+
}
20+
1521
function experimentalTernaries(
1622
node: ConditionalExpression,
1723
path: AstPath<ConditionalExpression>,
@@ -22,16 +28,11 @@ function experimentalTernaries(
2228
const isNested = grandparent.kind === NonterminalKind.ConditionalExpression;
2329
const isNestedAsTrueExpression =
2430
isNested && grandparent.trueExpression.variant === node;
25-
let falseExpressionIsTuple = false;
26-
let falseExpressionInSameLine = false;
27-
if (typeof node.falseExpression.variant !== 'string') {
28-
falseExpressionIsTuple =
29-
node.falseExpression.variant.kind === NonterminalKind.TupleExpression;
30-
falseExpressionInSameLine =
31-
falseExpressionIsTuple ||
31+
const falseExpressionInSameLine =
32+
typeof node.falseExpression.variant !== 'string' &&
33+
(node.falseExpression.variant.kind === NonterminalKind.TupleExpression ||
3234
node.falseExpression.variant.kind ===
33-
NonterminalKind.ConditionalExpression;
34-
}
35+
NonterminalKind.ConditionalExpression);
3536

3637
// If the `condition` breaks into multiple lines, we add parentheses,
3738
// unless it already is a `TupleExpression`.
@@ -57,26 +58,20 @@ function experimentalTernaries(
5758
{ id: Symbol('Slang.ConditionalExpression.trueExpression') }
5859
);
5960

60-
// For the odd case of `tabWidth` of 1 or 0 we initiate `fillTab` as a single
61-
// space.
62-
let fillTab = ' ';
63-
if (
64-
!falseExpressionInSameLine && // avoid processing if it's not needed
65-
(options.tabWidth > 2 || options.useTabs)
66-
) {
67-
fillTab = options.useTabs ? '\t' : ' '.repeat(options.tabWidth - 1);
68-
}
69-
7061
const falseExpression = path.call(print, 'falseExpression');
7162
const falseExpressionDoc = [
7263
isNested ? hardline : line,
7364
':',
7465
falseExpressionInSameLine
7566
? [' ', falseExpression]
76-
: ifBreak([fillTab, indent(falseExpression)], [' ', falseExpression], {
77-
// We only add `fillTab` if we are sure the trueExpression is indented
78-
groupId: conditionAndTrueExpressionGroup.id
79-
})
67+
: ifBreak(
68+
[fillTab(options), indent(falseExpression)],
69+
[' ', falseExpression],
70+
{
71+
// We only add `fillTab` if we are sure the trueExpression is indented
72+
groupId: conditionAndTrueExpressionGroup.id
73+
}
74+
)
8075
];
8176

8277
const document = group([conditionAndTrueExpressionGroup, falseExpressionDoc]);
@@ -87,7 +82,6 @@ function experimentalTernaries(
8782
}
8883

8984
function traditionalTernaries(
90-
node: ConditionalExpression,
9185
path: AstPath<ConditionalExpression>,
9286
print: PrintFunction
9387
): Doc {
@@ -177,6 +171,6 @@ export class ConditionalExpression implements SlangNode {
177171
): Doc {
178172
return options.experimentalTernaries
179173
? experimentalTernaries(this, path, print, options)
180-
: traditionalTernaries(this, path, print);
174+
: traditionalTernaries(path, print);
181175
}
182176
}

0 commit comments

Comments
 (0)