Skip to content

Commit 569ec59

Browse files
committed
other destructuring and variable caching that where missing
1 parent bbe543e commit 569ec59

9 files changed

Lines changed: 37 additions & 42 deletions

src/index.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,8 @@ const parsers = {
5151
[antlrParserId]: antlrParser
5252
};
5353

54-
const antlrCanAttachComment = (node: { type: string }): boolean =>
55-
typeof node.type === 'string' &&
56-
node.type !== 'BlockComment' &&
57-
node.type !== 'LineComment';
54+
const antlrCanAttachComment = ({ type }: { type: string }): boolean =>
55+
typeof type === 'string' && type !== 'BlockComment' && type !== 'LineComment';
5856
const canAttachComment = (node: AstNode): boolean =>
5957
typeof node !== 'string' &&
6058
typeof node !== 'undefined' &&

src/slang-nodes/ConditionalExpression.ts

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

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

14-
function fillTab(options: ParserOptions<AstNode>): Doc {
15-
if (options.useTabs) return '\t';
14+
function fillTab({ useTabs, tabWidth }: ParserOptions<AstNode>): Doc {
15+
if (useTabs) return '\t';
1616
// For the odd case of `tabWidth` of 1 or 0 we initiate `fillTab` as a single
1717
// space.
18-
return options.tabWidth > 2 ? ' '.repeat(options.tabWidth - 1) : ' ';
18+
return tabWidth > 2 ? ' '.repeat(tabWidth - 1) : ' ';
1919
}
2020

2121
function experimentalTernaries(

src/slang-nodes/IfStatement.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,19 +47,20 @@ export class IfStatement implements SlangNode {
4747
}
4848

4949
print(path: AstPath<IfStatement>, print: PrintFunction): Doc {
50+
const { kind: bodyKind, comments: bodyComments } = this.body.variant;
5051
return [
5152
'if (',
5253
printSeparatedItem(path.call(print, 'condition')),
5354
')',
54-
this.body.variant.kind === NonterminalKind.Block
55+
bodyKind === NonterminalKind.Block
5556
? [' ', path.call(print, 'body')]
5657
: group(indent([line, path.call(print, 'body')]), {
57-
shouldBreak: this.body.variant.kind === NonterminalKind.IfStatement // `if` within `if`
58+
shouldBreak: bodyKind === NonterminalKind.IfStatement // `if` within `if`
5859
}),
5960
this.elseBranch
6061
? [
61-
this.body.variant.kind !== NonterminalKind.Block || // else on a new line if body is not a block
62-
this.body.variant.comments.some(
62+
bodyKind !== NonterminalKind.Block || // else on a new line if body is not a block
63+
bodyComments.some(
6364
(comment) =>
6465
!isBlockComment(comment) || comment.placement === 'ownLine'
6566
) // or if body has trailing single line comments or a block comment on a new line

src/slang-nodes/ReturnStatement.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,11 @@ function printExpression(
1616
print: PrintFunction,
1717
options: ParserOptions<AstNode>
1818
): Doc {
19-
if (node.expression) {
20-
return node.expression.variant.kind === NonterminalKind.TupleExpression ||
19+
const expressionVariant = node.expression?.variant;
20+
if (expressionVariant) {
21+
return expressionVariant.kind === NonterminalKind.TupleExpression ||
2122
(options.experimentalTernaries &&
22-
node.expression.variant.kind === NonterminalKind.ConditionalExpression)
23+
expressionVariant.kind === NonterminalKind.ConditionalExpression)
2324
? [' ', path.call(print, 'expression')]
2425
: group(indent([line, path.call(print, 'expression')]));
2526
}

src/slang-utils/create-hug-function.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,11 @@ export function createHugFunction(
1010
): (node: Expression) => Expression {
1111
const operators = new Set(huggableOperators);
1212
return (node: Expression): Expression => {
13+
const { variant } = node;
1314
if (
14-
node.variant.kind !== TerminalKind.Identifier &&
15-
isBinaryOperation(node.variant) &&
16-
operators.has(node.variant.operator)
15+
variant.kind !== TerminalKind.Identifier &&
16+
isBinaryOperation(variant) &&
17+
operators.has(variant.operator)
1718
) {
1819
const { loc } = node;
1920

src/slang-utils/metadata.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -144,11 +144,11 @@ export function updateMetadata(
144144
for (const childNode of childNodes) {
145145
if (typeof childNode === 'undefined' || Array.isArray(childNode))
146146
continue;
147-
const childLoc = childNode.loc;
147+
const { leadingOffset, start } = childNode.loc;
148148

149-
if (childLoc.start - childLoc.leadingOffset === loc.start) {
150-
loc.leadingOffset = childLoc.leadingOffset;
151-
loc.start += childLoc.leadingOffset;
149+
if (start - leadingOffset === loc.start) {
150+
loc.leadingOffset = leadingOffset;
151+
loc.start += leadingOffset;
152152
break;
153153
}
154154
}
@@ -158,11 +158,11 @@ export function updateMetadata(
158158
for (const childNode of childNodes.reverse()) {
159159
if (typeof childNode === 'undefined' || Array.isArray(childNode))
160160
continue;
161-
const childLoc = childNode.loc;
161+
const { trailingOffset, end } = childNode.loc;
162162

163-
if (childLoc.end + childLoc.trailingOffset === loc.end) {
164-
loc.trailingOffset = childLoc.trailingOffset;
165-
loc.end -= childLoc.trailingOffset;
163+
if (end + trailingOffset === loc.end) {
164+
loc.trailingOffset = trailingOffset;
165+
loc.end -= trailingOffset;
166166
break;
167167
}
168168
}

src/slang-utils/sort-contract-specifiers.ts

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,18 @@ import { NonterminalKind } from '@nomicfoundation/slang/cst';
33
import type { ContractSpecifier } from '../slang-nodes/ContractSpecifier.js';
44

55
export function sortContractSpecifiers(
6-
a: ContractSpecifier,
7-
b: ContractSpecifier
6+
{ variant: { kind: aKind } }: ContractSpecifier,
7+
{ variant: { kind: bKind } }: ContractSpecifier
88
): number {
9-
const aVariant = a.variant;
10-
const bVariant = b.variant;
11-
129
// OverrideSpecifiers before ModifierInvocation
1310
if (
14-
aVariant.kind === NonterminalKind.InheritanceSpecifier &&
15-
bVariant.kind === NonterminalKind.StorageLayoutSpecifier
11+
aKind === NonterminalKind.InheritanceSpecifier &&
12+
bKind === NonterminalKind.StorageLayoutSpecifier
1613
)
1714
return -1;
1815
if (
19-
bVariant.kind === NonterminalKind.InheritanceSpecifier &&
20-
aVariant.kind === NonterminalKind.StorageLayoutSpecifier
16+
bKind === NonterminalKind.InheritanceSpecifier &&
17+
aKind === NonterminalKind.StorageLayoutSpecifier
2118
)
2219
return 1;
2320

src/slang-utils/sort-function-attributes.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,9 @@ const visibilityKeyWords = new Set([
1212
const mutabilityKeyWords = new Set(['pure', 'constant', 'payable', 'view']);
1313

1414
export function sortFunctionAttributes(
15-
a: SortableAttribute,
16-
b: SortableAttribute
15+
{ variant: aVariant }: SortableAttribute,
16+
{ variant: bVariant }: SortableAttribute
1717
): number {
18-
const aVariant = a.variant;
19-
const bVariant = b.variant;
20-
2118
const aIsString = typeof aVariant === 'string';
2219
const bIsString = typeof bVariant === 'string';
2320

src/slangPrinter.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@ import type { AstPath, Doc, ParserOptions } from 'prettier';
55
import type { AstNode, StrictAstNode } from './slang-nodes/types.d.ts';
66
import type { PrintFunction } from './types.d.ts';
77

8-
function hasNodeIgnoreComment(node: StrictAstNode): boolean {
8+
function hasNodeIgnoreComment({ comments }: StrictAstNode): boolean {
99
// Prettier sets SourceUnit's comments to undefined after assigning comments
1010
// to each node.
1111
return (
12-
node.comments &&
13-
node.comments.some(
12+
comments &&
13+
comments.some(
1414
(comment) =>
1515
comment.value
1616
.slice(2, isBlockComment(comment) ? -2 : undefined)

0 commit comments

Comments
 (0)