Skip to content

Commit 672f771

Browse files
committed
cleaning up how we check for TupleExpressions with a single item, so we minimise the amount of chained accesses and as a bonus removing the non-null assertions
1 parent 29f41b4 commit 672f771

3 files changed

Lines changed: 27 additions & 13 deletions

File tree

src/slang-nodes/ConditionalExpression.ts

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -133,13 +133,22 @@ export class ConditionalExpression implements SlangNode {
133133
// We can remove parentheses only because we are sure that the
134134
// `condition` must be a single `bool` value.
135135
const operandLoc = this.operand.loc;
136-
while (
137-
this.operand.variant.kind === NonterminalKind.TupleExpression &&
138-
this.operand.variant.items.items.length === 1 &&
139-
this.operand.variant.items.items[0].expression!.variant.kind !==
140-
NonterminalKind.ConditionalExpression
136+
137+
const getOperandSingleExpression = (): Expression | undefined => {
138+
const operandVariant = this.operand.variant;
139+
return operandVariant.kind === NonterminalKind.TupleExpression
140+
? operandVariant.items.getSingleExpression()
141+
: undefined;
142+
};
143+
144+
for (
145+
let operandSingleExpression = getOperandSingleExpression();
146+
operandSingleExpression &&
147+
operandSingleExpression.variant.kind !==
148+
NonterminalKind.ConditionalExpression;
149+
operandSingleExpression = getOperandSingleExpression()
141150
) {
142-
this.operand = this.operand.variant.items.items[0].expression!;
151+
this.operand = operandSingleExpression;
143152
}
144153
this.operand.loc = operandLoc;
145154
}

src/slang-nodes/MemberAccessExpression.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@ function isEndOfChain(
2424
path: AstPath<StrictAstNode>
2525
): boolean {
2626
for (
27-
let i = 2, current: StrictAstNode = node, grandparent = path.getNode(i)!;
28-
isChainableExpression(grandparent);
29-
i += 2, current = grandparent, grandparent = path.getNode(i)!
27+
let i = 2, current: StrictAstNode = node, grandparent = path.getNode(i);
28+
grandparent && isChainableExpression(grandparent);
29+
i += 2, current = grandparent, grandparent = path.getNode(i)
3030
) {
3131
switch (grandparent.kind) {
3232
case NonterminalKind.MemberAccessExpression:

src/slang-nodes/TupleValues.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import type * as ast from '@nomicfoundation/slang/ast';
88
import type { AstPath, Doc, ParserOptions } from 'prettier';
99
import type { AstNode } from './types.d.ts';
1010
import type { PrintFunction, SlangNode } from '../types.d.ts';
11+
import type { Expression } from './Expression.js';
1112

1213
export class TupleValues implements SlangNode {
1314
readonly kind = NonterminalKind.TupleValues;
@@ -32,11 +33,15 @@ export class TupleValues implements SlangNode {
3233
this.loc = metadata.loc;
3334
}
3435

36+
getSingleExpression(): Expression | undefined {
37+
return this.items.length === 1 ? this.items[0].expression : undefined;
38+
}
39+
3540
print(path: AstPath<TupleValues>, print: PrintFunction): Doc {
36-
return this.items.length === 1 &&
37-
this.items[0].expression &&
38-
this.items[0].expression.variant.kind !== TerminalKind.Identifier &&
39-
isBinaryOperation(this.items[0].expression.variant)
41+
const singleExpression = this.getSingleExpression();
42+
return singleExpression &&
43+
singleExpression.variant.kind !== TerminalKind.Identifier &&
44+
isBinaryOperation(singleExpression.variant)
4045
? path.map(print, 'items')
4146
: printSeparatedList(path.map(print, 'items'));
4247
}

0 commit comments

Comments
 (0)