Skip to content

Commit 254cff2

Browse files
authored
Cleaning up duplicated code into a const (#1210)
* storing printed node in const when it's used in a conditional operation * the `flat()` call never does anything * small cases where we gain from storing value in a variable * spotted a `printSeparatedItem` pattern that I missed
1 parent c6fd071 commit 254cff2

19 files changed

Lines changed: 78 additions & 79 deletions

src/clean.ts

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,5 @@
11
// Prettier offers a clean way to define ignored properties.
2-
const ignoredProperties = new Set([
3-
'loc',
4-
'range',
5-
'comments',
6-
// this function is defined at constructor time so it won't pass AST
7-
// comparisons.
8-
'isEmpty'
9-
]);
2+
const ignoredProperties = new Set(['loc', 'range', 'comments']);
103
// eslint-disable-next-line @typescript-eslint/no-empty-function
114
function clean(/* ast, newObj, parent */): void {}
125
clean.ignoredProperties = ignoredProperties;

src/slang-nodes/AssignmentExpression.ts

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

3333
print(path: AstPath<AssignmentExpression>, print: PrintFunction): Doc {
34+
const rightOperandVariant = this.rightOperand.variant;
35+
const rightOperand = path.call(print, 'rightOperand');
3436
return [
3537
path.call(print, 'leftOperand'),
3638
` ${this.operator}`,
37-
this.rightOperand.variant.kind !== TerminalKind.Identifier &&
38-
isBinaryOperation(this.rightOperand.variant)
39-
? group(indent([line, path.call(print, 'rightOperand')]))
40-
: [' ', path.call(print, 'rightOperand')]
39+
rightOperandVariant.kind !== TerminalKind.Identifier &&
40+
isBinaryOperation(rightOperandVariant)
41+
? group(indent([line, rightOperand]))
42+
: [' ', rightOperand]
4143
];
4244
}
4345
}

src/slang-nodes/ConditionalExpression.ts

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,26 +11,20 @@ import type { PrintFunction } from '../types.d.ts';
1111

1212
const { group, hardline, ifBreak, indent, line, softline } = doc.builders;
1313

14-
function fillTab({ useTabs, tabWidth }: ParserOptions<AstNode>): Doc {
15-
if (useTabs) return '\t';
16-
// For the odd case of `tabWidth` of 1 or 0 we initiate `fillTab` as a single
17-
// space.
18-
return tabWidth > 2 ? ' '.repeat(tabWidth - 1) : ' ';
19-
}
20-
2114
function experimentalTernaries(
2215
node: ConditionalExpression,
2316
path: AstPath<ConditionalExpression>,
2417
print: PrintFunction,
25-
options: ParserOptions<AstNode>
18+
{ useTabs, tabWidth }: ParserOptions<AstNode>
2619
): Doc {
2720
const grandparent = path.grandparent as StrictAstNode;
2821
const isNested = grandparent.kind === NonterminalKind.ConditionalExpression;
2922
const isNestedAsTrueExpression =
3023
isNested && grandparent.trueExpression.variant === node;
24+
const falseExpressionVariantKind = node.falseExpression.variant.kind;
3125
const falseExpressionInSameLine =
32-
node.falseExpression.variant.kind === NonterminalKind.TupleExpression ||
33-
node.falseExpression.variant.kind === NonterminalKind.ConditionalExpression;
26+
falseExpressionVariantKind === NonterminalKind.TupleExpression ||
27+
falseExpressionVariantKind === NonterminalKind.ConditionalExpression;
3428

3529
// If the `condition` breaks into multiple lines, we add parentheses,
3630
// unless it already is a `TupleExpression`.
@@ -56,14 +50,22 @@ function experimentalTernaries(
5650
{ id: groupId }
5751
);
5852

53+
// For the odd case of `tabWidth` of 1 or 0 we initiate `fillTab` as a single
54+
// space.
55+
const fillTab = useTabs
56+
? '\t'
57+
: tabWidth > 2
58+
? ' '.repeat(tabWidth - 1)
59+
: ' ';
60+
5961
const falseExpression = path.call(print, 'falseExpression');
6062
const falseExpressionDoc = [
6163
isNested ? hardline : line,
6264
':',
6365
falseExpressionInSameLine
6466
? [' ', falseExpression]
6567
: ifBreak(
66-
[fillTab(options), indent(falseExpression)],
68+
[fillTab, indent(falseExpression)],
6769
[' ', falseExpression],
6870
// We only add `fillTab` if we are sure the trueExpression is
6971
// indented.

src/slang-nodes/ContractSpecifiers.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,18 +28,16 @@ export class ContractSpecifiers extends SlangNode {
2828
}
2929

3030
print(path: AstPath<ContractSpecifiers>, print: PrintFunction): Doc {
31-
if (this.items.length === 0) return '';
32-
3331
const [specifier1, specifier2] = path.map(print, 'items');
32+
33+
if (typeof specifier1 === 'undefined') return '';
34+
3435
if (typeof specifier2 === 'undefined') return [' ', specifier1];
3536

3637
const groupId = Symbol('Slang.ContractSpecifiers.inheritance');
3738
return printSeparatedList(
3839
[group(specifier1, { id: groupId }), specifier2],
39-
{
40-
firstSeparator: line,
41-
separator: ifBreak('', softline, { groupId })
42-
}
40+
{ firstSeparator: line, separator: ifBreak('', softline, { groupId }) }
4341
);
4442
}
4543
}

src/slang-nodes/DoWhileStatement.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import type { AstPath, Doc, ParserOptions } from 'prettier';
1010
import type { AstNode } from './types.d.ts';
1111
import type { PrintFunction } from '../types.d.ts';
1212

13-
const { group, indent, line } = doc.builders;
13+
const { line } = doc.builders;
1414

1515
export class DoWhileStatement extends SlangNode {
1616
readonly kind = NonterminalKind.DoWhileStatement;
@@ -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+
: printSeparatedItem(body, { firstSeparator: 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
}

0 commit comments

Comments
 (0)