Skip to content

Commit a0cb08b

Browse files
committed
small cases where we gain from storing value in a variable
1 parent d99ba8b commit a0cb08b

9 files changed

Lines changed: 37 additions & 40 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: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,13 @@ export class AssignmentExpression extends SlangNode {
3131
}
3232

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

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: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,10 @@ 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');

src/slang-nodes/ModifierDefinition.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,9 @@ export class ModifierDefinition extends SlangNode {
3636
this.updateMetadata(this.parameters, this.attributes, this.body);
3737

3838
if (!this.parameters) {
39+
const attributesLoc = this.attributes.loc;
3940
const parametersOffset =
40-
this.attributes.loc.start - this.attributes.loc.leadingOffset;
41+
attributesLoc.start - attributesLoc.leadingOffset;
4142
const parametersLoc = {
4243
start: parametersOffset,
4344
end: parametersOffset,

src/slang-nodes/ModifierInvocation.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,12 @@ export class ModifierInvocation extends SlangNode {
2727
}
2828

2929
cleanModifierInvocationArguments(): void {
30+
const argumentsVariant = this.arguments?.variant;
3031
if (
31-
this.arguments &&
32-
this.arguments.variant.kind ===
32+
argumentsVariant &&
33+
argumentsVariant.kind ===
3334
NonterminalKind.PositionalArgumentsDeclaration &&
34-
this.arguments.variant.isEmpty()
35+
argumentsVariant.isEmpty
3536
) {
3637
delete this.arguments;
3738
}

src/slang-nodes/PositionalArgumentsDeclaration.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,9 @@ export class PositionalArgumentsDeclaration extends SlangNode {
2727

2828
// We need to check the comments at this point because they will be removed
2929
// from this node into the root node.
30-
const empty =
30+
this.isEmpty =
3131
this.arguments.items.length === 0 && // no arguments
3232
!this.comments.some((comment) => isBlockComment(comment)); // no block comments
33-
34-
this.isEmpty = (): boolean => empty;
3533
}
3634

3735
print(

src/slang-nodes/ReturnStatement.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@ function printExpression(
1616
print: PrintFunction,
1717
options: ParserOptions<AstNode>
1818
): Doc {
19-
const expressionVariant = node.expression?.variant;
19+
const expressionVariantKind = node.expression?.variant.kind;
2020
const expression = path.call(print, 'expression');
21-
if (expressionVariant) {
22-
return expressionVariant.kind === NonterminalKind.TupleExpression ||
21+
if (expressionVariantKind) {
22+
return expressionVariantKind === NonterminalKind.TupleExpression ||
2323
(options.experimentalTernaries &&
24-
expressionVariant.kind === NonterminalKind.ConditionalExpression)
24+
expressionVariantKind === NonterminalKind.ConditionalExpression)
2525
? [' ', expression]
2626
: group(indent([line, expression]));
2727
}

src/slang-printers/create-binary-operation-printer.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,24 +13,24 @@ import type { PrintFunction } from '../types.d.ts';
1313
const { group, line } = doc.builders;
1414

1515
function rightOperandPrint(
16-
node: BinaryOperation,
16+
{ operator, leftOperand }: BinaryOperation,
1717
path: AstPath<BinaryOperation>,
1818
print: PrintFunction,
1919
options: ParserOptions<AstNode>
2020
): Doc {
2121
const rightOperand = path.call(print, 'rightOperand');
2222
const rightOperandDoc =
2323
options.experimentalOperatorPosition === 'end'
24-
? [` ${node.operator}`, line, rightOperand]
25-
: [line, `${node.operator} `, rightOperand];
24+
? [` ${operator}`, line, rightOperand]
25+
: [line, `${operator} `, rightOperand];
2626

2727
// If there's only a single binary expression, we want to create a group in
2828
// order to avoid having a small right part like -1 be on its own line.
29-
const leftOperand = node.leftOperand.variant;
29+
const leftOperandVariant = leftOperand.variant;
3030
const grandparentNode = path.grandparent as StrictAstNode;
3131
const shouldGroup =
32-
(leftOperand.kind === TerminalKind.Identifier ||
33-
!isBinaryOperation(leftOperand)) &&
32+
(leftOperandVariant.kind === TerminalKind.Identifier ||
33+
!isBinaryOperation(leftOperandVariant)) &&
3434
(!isBinaryOperation(grandparentNode) ||
3535
grandparentNode.kind === NonterminalKind.AssignmentExpression);
3636

0 commit comments

Comments
 (0)