Skip to content

Commit 1eca28a

Browse files
authored
cleaning up how we check for TupleExpressions with a single item (#1197)
* cleaning up how we check for `TupleExpression`s with a single item, so we minimise the amount of chained accesses and as a bonus removing the non-null assertions * cleanup * small size improvement * storing expression.variant instead of expression
1 parent cd80105 commit 1eca28a

3 files changed

Lines changed: 28 additions & 13 deletions

File tree

src/slang-nodes/ConditionalExpression.ts

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,14 @@ function traditionalTernaries(
100100
]);
101101
}
102102

103+
function getOperandSingleExpression({
104+
variant
105+
}: Expression): Expression | undefined {
106+
return variant.kind === NonterminalKind.TupleExpression
107+
? variant.items.getSingleExpression()
108+
: undefined;
109+
}
110+
103111
export class ConditionalExpression implements SlangNode {
104112
readonly kind = NonterminalKind.ConditionalExpression;
105113

@@ -133,13 +141,14 @@ export class ConditionalExpression implements SlangNode {
133141
// We can remove parentheses only because we are sure that the
134142
// `condition` must be a single `bool` value.
135143
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
144+
for (
145+
let operandSingleExpression = getOperandSingleExpression(this.operand);
146+
operandSingleExpression &&
147+
operandSingleExpression.variant.kind !==
148+
NonterminalKind.ConditionalExpression;
149+
operandSingleExpression = getOperandSingleExpression(this.operand)
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: 10 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;
@@ -29,11 +30,16 @@ export class TupleValues implements SlangNode {
2930
this.loc = metadata.loc;
3031
}
3132

33+
getSingleExpression(): Expression | undefined {
34+
const items = this.items;
35+
return items.length === 1 ? items[0].expression : undefined;
36+
}
37+
3238
print(path: AstPath<TupleValues>, print: PrintFunction): Doc {
33-
return this.items.length === 1 &&
34-
this.items[0].expression &&
35-
this.items[0].expression.variant.kind !== TerminalKind.Identifier &&
36-
isBinaryOperation(this.items[0].expression.variant)
39+
const singleExpressionVariant = this.getSingleExpression()?.variant;
40+
return singleExpressionVariant &&
41+
singleExpressionVariant.kind !== TerminalKind.Identifier &&
42+
isBinaryOperation(singleExpressionVariant)
3743
? path.map(print, 'items')
3844
: printSeparatedList(path.map(print, 'items'));
3945
}

0 commit comments

Comments
 (0)