Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 15 additions & 6 deletions src/slang-nodes/ConditionalExpression.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,14 @@ function traditionalTernaries(
]);
}

function getOperandSingleExpression({
variant
}: Expression): Expression | undefined {
return variant.kind === NonterminalKind.TupleExpression
? variant.items.getSingleExpression()
: undefined;
}

export class ConditionalExpression implements SlangNode {
readonly kind = NonterminalKind.ConditionalExpression;

Expand Down Expand Up @@ -133,13 +141,14 @@ export class ConditionalExpression implements SlangNode {
// 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
for (
let operandSingleExpression = getOperandSingleExpression(this.operand);
operandSingleExpression &&
operandSingleExpression.variant.kind !==
NonterminalKind.ConditionalExpression;
operandSingleExpression = getOperandSingleExpression(this.operand)
) {
this.operand = this.operand.variant.items.items[0].expression!;
this.operand = operandSingleExpression;
}
this.operand.loc = operandLoc;
}
Expand Down
6 changes: 3 additions & 3 deletions src/slang-nodes/MemberAccessExpression.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ function isEndOfChain(
path: AstPath<StrictAstNode>
): boolean {
for (
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here I'm just getting rid of the non-null assertions

let i = 2, current: StrictAstNode = node, grandparent = path.getNode(i)!;
isChainableExpression(grandparent);
i += 2, current = grandparent, grandparent = path.getNode(i)!
let i = 2, current: StrictAstNode = node, grandparent = path.getNode(i);
grandparent && isChainableExpression(grandparent);
i += 2, current = grandparent, grandparent = path.getNode(i)
) {
switch (grandparent.kind) {
case NonterminalKind.MemberAccessExpression:
Expand Down
13 changes: 9 additions & 4 deletions src/slang-nodes/TupleValues.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import type * as ast from '@nomicfoundation/slang/ast';
import type { AstPath, Doc, ParserOptions } from 'prettier';
import type { AstNode } from './types.d.ts';
import type { PrintFunction, SlangNode } from '../types.d.ts';
import type { Expression } from './Expression.js';

export class TupleValues implements SlangNode {
readonly kind = NonterminalKind.TupleValues;
Expand All @@ -32,11 +33,15 @@ export class TupleValues implements SlangNode {
this.loc = metadata.loc;
}

getSingleExpression(): Expression | undefined {
return this.items.length === 1 ? this.items[0].expression : undefined;
}

print(path: AstPath<TupleValues>, print: PrintFunction): Doc {
return this.items.length === 1 &&
this.items[0].expression &&
this.items[0].expression.variant.kind !== TerminalKind.Identifier &&
isBinaryOperation(this.items[0].expression.variant)
const singleExpression = this.getSingleExpression();
return singleExpression &&
singleExpression.variant.kind !== TerminalKind.Identifier &&
isBinaryOperation(singleExpression.variant)
? path.map(print, 'items')
: printSeparatedList(path.map(print, 'items'));
}
Expand Down