Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions src/slang-nodes/ContractMembers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,10 @@ export class ContractMembers extends SlangNode {
options: ParserOptions<AstNode>
): Doc {
return this.items.length > 0 || (this.comments?.length || 0) > 0
? printSeparatedItem(printPreservingEmptyLines(path, print, options), {
firstSeparator: hardline
})
? printSeparatedItem(
printPreservingEmptyLines(this, path, print, options),
{ firstSeparator: hardline }
)
: '';
}
}
7 changes: 4 additions & 3 deletions src/slang-nodes/InterfaceMembers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,10 @@ export class InterfaceMembers extends SlangNode {
options: ParserOptions<AstNode>
): Doc {
return this.items.length > 0 || (this.comments?.length || 0) > 0
? printSeparatedItem(printPreservingEmptyLines(path, print, options), {
firstSeparator: hardline
})
? printSeparatedItem(
printPreservingEmptyLines(this, path, print, options),
{ firstSeparator: hardline }
)
: '';
}
}
7 changes: 4 additions & 3 deletions src/slang-nodes/LibraryMembers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,10 @@ export class LibraryMembers extends SlangNode {
options: ParserOptions<AstNode>
): Doc {
return this.items.length > 0 || (this.comments?.length || 0) > 0
? printSeparatedItem(printPreservingEmptyLines(path, print, options), {
firstSeparator: hardline
})
? printSeparatedItem(
printPreservingEmptyLines(this, path, print, options),
{ firstSeparator: hardline }
)
: '';
}
}
13 changes: 6 additions & 7 deletions src/slang-nodes/MemberAccessExpression.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,18 @@ import { TerminalNode } from './TerminalNode.js';
import type * as ast from '@nomicfoundation/slang/ast';
import type { AstPath, Doc, ParserOptions } from 'prettier';
import type { CollectedMetadata, PrintFunction } from '../types.d.ts';
import type { AstNode } from './types.d.ts';
import type { AstNode, ChainableExpression } from './types.d.ts';

const { group, indent, label, softline } = doc.builders;

function isEndOfChain(
node: MemberAccessExpression,
node: ChainableExpression,
path: AstPath<Expression['variant']>
): boolean {
for (
let i = 1, current: Expression['variant'] = node, parent = path.getNode(i);
parent && isChainableExpression(parent);
i++, current = parent, parent = path.getNode(i)
) {
for (let i = 1, current = node, parent; ; i++, current = parent) {
parent = path.getNode(i)!;
if (!isChainableExpression(parent)) break;

switch (parent.kind) {
case NonterminalKind.MemberAccessExpression:
// If `parent` is a MemberAccessExpression we are not at the end
Expand Down
2 changes: 1 addition & 1 deletion src/slang-nodes/Parameters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export class Parameters extends SlangNode {
return printSeparatedList(path.map(print, 'items'), { grouped: false });
}

const parameterComments = printComments(path, options);
const parameterComments = printComments(this, path, options);

return parameterComments.length > 0
? printSeparatedItem(parameterComments)
Expand Down
2 changes: 1 addition & 1 deletion src/slang-nodes/PositionalArguments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export class PositionalArguments extends SlangNode {
if (this.items.length > 0) {
return printSeparatedList(path.map(print, 'items'));
}
const argumentComments = printComments(path, options);
const argumentComments = printComments(this, path, options);

return argumentComments.length > 0
? printSeparatedItem(argumentComments)
Expand Down
2 changes: 1 addition & 1 deletion src/slang-nodes/SourceUnitMembers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,6 @@ export class SourceUnitMembers extends SlangNode {
print: PrintFunction,
options: ParserOptions<AstNode>
): Doc {
return printPreservingEmptyLines(path, print, options);
return printPreservingEmptyLines(this, path, print, options);
}
}
7 changes: 4 additions & 3 deletions src/slang-nodes/Statements.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,10 @@ export class Statements extends SlangNode {
options: ParserOptions<AstNode>
): Doc {
return this.items.length > 0 || (this.comments?.length || 0) > 0
? printSeparatedItem(printPreservingEmptyLines(path, print, options), {
firstSeparator: hardline
})
? printSeparatedItem(
printPreservingEmptyLines(this, path, print, options),
{ firstSeparator: hardline }
)
: '';
}
}
7 changes: 4 additions & 3 deletions src/slang-nodes/YulStatements.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,10 @@ export class YulStatements extends SlangNode {
options: ParserOptions<AstNode>
): Doc {
return this.items.length > 0 || (this.comments?.length || 0) > 0
? printSeparatedItem(printPreservingEmptyLines(path, print, options), {
firstSeparator: hardline
})
? printSeparatedItem(
printPreservingEmptyLines(this, path, print, options),
{ firstSeparator: hardline }
)
: '';
}
}
5 changes: 5 additions & 0 deletions src/slang-nodes/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -494,6 +494,11 @@ export type BinaryOperation = Extract<
}
>;

export type ChainableExpression =
| FunctionCallExpression
| IndexAccessExpression
| MemberAccessExpression;

export type AstNode =
| StrictAstNode
| Comment
Expand Down
5 changes: 3 additions & 2 deletions src/slang-printers/create-binary-operation-printer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ export const createBinaryOperationPrinter =
path: AstPath<BinaryOperation>
) => (document: Doc) => Doc,
indentRulesBuilder: (
path: AstPath<BinaryOperation>
node: BinaryOperation,
path: AstPath<StrictAstNode>
) => (document: Doc) => Doc
) =>
(
Expand All @@ -52,7 +53,7 @@ export const createBinaryOperationPrinter =
options: ParserOptions<AstNode>
): Doc => {
const groupRules = groupRulesBuilder(path);
const indentRules = indentRulesBuilder(path);
const indentRules = indentRulesBuilder(node, path);

return groupRules([
path.call(print, 'leftOperand'),
Expand Down
11 changes: 5 additions & 6 deletions src/slang-printers/print-binary-operation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ const isStatementWithoutIndentedOperation = createKindCheckFunction([

export const shouldNotIndent = (
node: StrictAstNode,
path: AstPath<BinaryOperation>,
path: AstPath<StrictAstNode>,
index: number
): boolean =>
isStatementWithoutIndentedOperation(node) ||
Expand All @@ -42,15 +42,14 @@ export const shouldNotIndent = (

export const binaryIndentRulesBuilder =
(shouldIndent: (node: BinaryOperation) => boolean) =>
(path: AstPath<BinaryOperation>) =>
(node: BinaryOperation, path: AstPath<StrictAstNode>) =>
(document: Doc): Doc => {
for (let i = 1, node = path.node; ; i++) {
const parent = path.getNode(i) as StrictAstNode;
for (let i = 1, current = node, parent; ; i++, current = parent) {
parent = path.getNode(i)!;
if (shouldNotIndent(parent, path, i)) break;
if (!isBinaryOperation(parent)) return indent(document);
if (shouldIndent(parent)) return indent(document);
if (node === parent.rightOperand) break;
node = parent;
if (current === parent.rightOperand) break;
}
return document;
};
Expand Down
3 changes: 2 additions & 1 deletion src/slang-printers/print-comments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,11 @@ function isPrintable(comment: Comment): boolean {
}

export function printComments(
node: StrictAstNode,
path: AstPath<StrictAstNode>,
options: ParserOptions<AstNode>
): Doc[] {
if (path.node.comments === undefined) return [];
if (node.comments === undefined) return [];
return joinExisting(
line,
path.map((commentPath, index, comments: Comment[]) => {
Expand Down
11 changes: 5 additions & 6 deletions src/slang-printers/print-logical-operation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,18 @@ const { indent } = doc.builders;
const logicalGroupRulesBuilder = binaryGroupRulesBuilder(() => false);

const logicalIndentRulesBuilder =
(path: AstPath<BinaryOperation>) =>
(node: BinaryOperation, path: AstPath<StrictAstNode>) =>
(document: Doc): Doc => {
for (let i = 1, node = path.node; ; i++) {
const parent = path.getNode(i) as StrictAstNode;
for (let i = 1, current = node, parent; ; i++, current = parent) {
parent = path.getNode(i)!;
if (shouldNotIndent(parent, path, i)) break;
if (
parent.kind === NonterminalKind.ConditionalExpression &&
parent.operand === node
parent.operand === current
)
break;
if (!isBinaryOperation(parent)) return indent(document);
if (node === parent.rightOperand) break;
node = parent;
if (current === parent.rightOperand) break;
}
return document;
};
Expand Down
5 changes: 3 additions & 2 deletions src/slang-printers/print-preserving-empty-lines.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@ import type { PrintFunction } from '../types.d.ts';
const { hardline } = doc.builders;

export function printPreservingEmptyLines(
node: LineCollection,
path: AstPath<LineCollection>,
print: PrintFunction,
options: ParserOptions<AstNode>
): Doc {
return path.node.items.length > 0
return node.items.length > 0
? path.map((childPath) => {
const node = childPath.node;

Expand All @@ -35,5 +36,5 @@ export function printPreservingEmptyLines(
: ''
];
}, 'items')
: printComments(path, options);
: printComments(node, path, options);
}
3 changes: 2 additions & 1 deletion src/slang-utils/is-chainable-expression.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { NonterminalKind } from '@nomicfoundation/slang/cst';
import { createKindCheckFunction } from './create-kind-check-function.js';

import type { ChainableExpression } from '../slang-nodes/types.js';
import type { Expression } from '../slang-nodes/Expression.js';

export const isChainableExpression = createKindCheckFunction([
NonterminalKind.FunctionCallExpression,
NonterminalKind.IndexAccessExpression,
NonterminalKind.MemberAccessExpression
]) as (node: Expression['variant']) => boolean;
]) as (node: Expression['variant']) => node is ChainableExpression;