Skip to content

Commit e5e56cf

Browse files
committed
storing printed node in const when it's used in a conditional operation
1 parent 76056f3 commit e5e56cf

11 files changed

Lines changed: 37 additions & 28 deletions

src/slang-nodes/AssignmentExpression.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,14 @@ export class AssignmentExpression extends SlangNode {
3131
}
3232

3333
print(path: AstPath<AssignmentExpression>, print: PrintFunction): Doc {
34+
const rightOperand = path.call(print, 'rightOperand');
3435
return [
3536
path.call(print, 'leftOperand'),
3637
` ${this.operator}`,
3738
this.rightOperand.variant.kind !== TerminalKind.Identifier &&
3839
isBinaryOperation(this.rightOperand.variant)
39-
? group(indent([line, path.call(print, 'rightOperand')]))
40-
: [' ', path.call(print, 'rightOperand')]
40+
? group(indent([line, rightOperand]))
41+
: [' ', rightOperand]
4142
];
4243
}
4344
}

src/slang-nodes/DoWhileStatement.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,12 @@ export class DoWhileStatement extends SlangNode {
2929
}
3030

3131
print(path: AstPath<DoWhileStatement>, print: PrintFunction): Doc {
32+
const body = path.call(print, 'body');
3233
return [
3334
'do',
3435
this.body.variant.kind === NonterminalKind.Block
35-
? [' ', path.call(print, 'body'), ' ']
36-
: group([indent([line, path.call(print, 'body')]), line]),
36+
? [' ', body, ' ']
37+
: group([indent([line, body]), line]),
3738
'while (',
3839
printSeparatedItem(path.call(print, 'condition')),
3940
');'

src/slang-nodes/ElseBranch.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,12 @@ export class ElseBranch extends SlangNode {
3030
}
3131

3232
print(path: AstPath<ElseBranch>, print: PrintFunction): Doc {
33+
const body = path.call(print, 'body');
3334
return [
3435
'else',
3536
isIfStatementOrBlock(this.body.variant)
36-
? [' ', path.call(print, 'body')]
37-
: group(indent([line, path.call(print, 'body')]))
37+
? [' ', body]
38+
: group(indent([line, body]))
3839
];
3940
}
4041
}

src/slang-nodes/ForStatement.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ export class ForStatement extends SlangNode {
5050
const initialization = path.call(print, 'initialization');
5151
const condition = path.call(print, 'condition');
5252
const iterator = path.call(print, 'iterator');
53+
const body = path.call(print, 'body');
5354

5455
return [
5556
'for (',
@@ -61,8 +62,8 @@ export class ForStatement extends SlangNode {
6162
}),
6263
')',
6364
this.body.variant.kind === NonterminalKind.Block
64-
? [' ', path.call(print, 'body')]
65-
: group(indent([line, path.call(print, 'body')]))
65+
? [' ', body]
66+
: group(indent([line, body]))
6667
];
6768
}
6869
}

src/slang-nodes/FunctionCallExpression.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,21 +32,21 @@ export class FunctionCallExpression extends SlangNode {
3232
}
3333

3434
print(path: AstPath<FunctionCallExpression>, print: PrintFunction): Doc {
35-
const operandDoc = path.call(print, 'operand');
35+
const operand = path.call(print, 'operand');
3636
const argumentsDoc = path.call(print, 'arguments');
3737

3838
// If we are at the end of a MemberAccessChain we should indent the
3939
// arguments accordingly.
40-
if (isLabel(operandDoc) && operandDoc.label === 'MemberAccessChain') {
40+
if (isLabel(operand) && operand.label === 'MemberAccessChain') {
4141
const groupId = Symbol('Slang.FunctionCallExpression.operand');
4242
// We wrap the expression in a label in case there is an IndexAccess or
4343
// a FunctionCall following this IndexAccess.
4444
return label('MemberAccessChain', [
45-
group(operandDoc.contents, { id: groupId }),
45+
group(operand.contents, { id: groupId }),
4646
indentIfBreak(argumentsDoc, { groupId })
4747
]);
4848
}
4949

50-
return [operandDoc, argumentsDoc].flat();
50+
return [operand, argumentsDoc].flat();
5151
}
5252
}

src/slang-nodes/IfStatement.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,14 @@ export class IfStatement extends SlangNode {
3737

3838
print(path: AstPath<IfStatement>, print: PrintFunction): Doc {
3939
const { kind: bodyKind, comments: bodyComments } = this.body.variant;
40+
const body = path.call(print, 'body');
4041
return [
4142
'if (',
4243
printSeparatedItem(path.call(print, 'condition')),
4344
')',
4445
bodyKind === NonterminalKind.Block
45-
? [' ', path.call(print, 'body')]
46-
: group(indent([line, path.call(print, 'body')]), {
46+
? [' ', body]
47+
: group(indent([line, body]), {
4748
shouldBreak: bodyKind === NonterminalKind.IfStatement // `if` within `if`
4849
}),
4950
this.elseBranch

src/slang-nodes/IndexAccessExpression.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ export class IndexAccessExpression extends SlangNode {
3737
}
3838

3939
print(path: AstPath<IndexAccessExpression>, print: PrintFunction): Doc {
40-
const operandDoc = path.call(print, 'operand');
40+
const operand = path.call(print, 'operand');
4141
const indexDoc = [
4242
'[',
4343
printSeparatedItem([path.call(print, 'start'), path.call(print, 'end')]),
@@ -46,16 +46,16 @@ export class IndexAccessExpression extends SlangNode {
4646

4747
// If we are at the end of a MemberAccessChain we should indent the
4848
// arguments accordingly.
49-
if (isLabel(operandDoc) && operandDoc.label === 'MemberAccessChain') {
49+
if (isLabel(operand) && operand.label === 'MemberAccessChain') {
5050
const groupId = Symbol('Slang.IndexAccessExpression.operand');
5151
// We wrap the expression in a label in case there is an IndexAccess or
5252
// a FunctionCall following this IndexAccess.
5353
return label('MemberAccessChain', [
54-
group(operandDoc.contents, { id: groupId }),
54+
group(operand.contents, { id: groupId }),
5555
indentIfBreak(indexDoc, { groupId })
5656
]);
5757
}
5858

59-
return [operandDoc, indexDoc].flat();
59+
return [operand, indexDoc].flat();
6060
}
6161
}

src/slang-nodes/ReturnStatement.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,13 @@ function printExpression(
1717
options: ParserOptions<AstNode>
1818
): Doc {
1919
const expressionVariant = node.expression?.variant;
20+
const expression = path.call(print, 'expression');
2021
if (expressionVariant) {
2122
return expressionVariant.kind === NonterminalKind.TupleExpression ||
2223
(options.experimentalTernaries &&
2324
expressionVariant.kind === NonterminalKind.ConditionalExpression)
24-
? [' ', path.call(print, 'expression')]
25-
: group(indent([line, path.call(print, 'expression')]));
25+
? [' ', expression]
26+
: group(indent([line, expression]));
2627
}
2728
return '';
2829
}

src/slang-nodes/StateVariableDefinitionValue.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,9 @@ export class StateVariableDefinitionValue extends SlangNode {
3030
path: AstPath<StateVariableDefinitionValue>,
3131
print: PrintFunction
3232
): Doc {
33+
const value = path.call(print, 'value');
3334
return this.value.variant.kind === NonterminalKind.ArrayExpression
34-
? [' = ', path.call(print, 'value')]
35-
: group([' =', indent([line, path.call(print, 'value')])]);
35+
? [' = ', value]
36+
: group([' =', indent([line, value])]);
3637
}
3738
}

src/slang-nodes/WhileStatement.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,14 @@ export class WhileStatement extends SlangNode {
2929
}
3030

3131
print(path: AstPath<WhileStatement>, print: PrintFunction): Doc {
32+
const body = path.call(print, 'body');
3233
return [
3334
'while (',
3435
printSeparatedItem(path.call(print, 'condition')),
3536
')',
3637
this.body.variant.kind === NonterminalKind.Block
37-
? [' ', path.call(print, 'body')]
38-
: group(indent([line, path.call(print, 'body')]))
38+
? [' ', body]
39+
: group(indent([line, body]))
3940
];
4041
}
4142
}

0 commit comments

Comments
 (0)