Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
bf5e9bb
use the node and grandparent getters instead of getNode() and getNode(2)
Janther Jun 11, 2025
938025f
small refactor
Janther Jun 11, 2025
3b5433b
using destructuring when possible
Janther Jun 11, 2025
7964354
`i` should start at 2 instead of 0 because it's always used as `i + 2`
Janther Jun 18, 2025
0bf26d0
small optimisations when we only need the destructured `node`
Janther Jun 18, 2025
7e3e7fd
variable is only used within the for loop
Janther Jun 18, 2025
52bf673
more accurate type to avoid type assertion and destructure
Janther Jun 18, 2025
c9c0cee
access the variant directly in the for..of loops
Janther Jun 24, 2025
f54b69b
since using the constant `groupId` there is no need for the group to …
Janther Jun 24, 2025
e36919f
other destructuring and variable caching that where missing
Janther Jun 26, 2025
5018791
using existing helper
Janther Jun 26, 2025
c39e874
reordering import
Janther Jun 27, 2025
f77f647
cleaning up extra check for existence of variable
Janther Jun 28, 2025
e6e60ad
missing types annotations
Janther Jun 29, 2025
1100fda
fixing some file extensions
Janther Jul 4, 2025
9371c3e
`offset` by definition equals to `initialOffset + parent.textLength.u…
Janther Jul 7, 2025
291b5df
only check once for the value of a boolean
Janther Jul 7, 2025
da8595c
using an actual `for...in` loop instead of storing the keys in an arr…
Janther Jul 7, 2025
7d3e81f
null check is not needed
Janther Jul 8, 2025
3c9719b
undo the destructuring of single variable that did not improve the fi…
Janther Jul 9, 2025
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
6 changes: 2 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,8 @@ const parsers = {
[antlrParserId]: antlrParser
};

const antlrCanAttachComment = (node: { type: string }): boolean =>
typeof node.type === 'string' &&
node.type !== 'BlockComment' &&
node.type !== 'LineComment';
const antlrCanAttachComment = ({ type }: { type: string }): boolean =>
typeof type === 'string' && type !== 'BlockComment' && type !== 'LineComment';
const canAttachComment = (node: AstNode): boolean =>
typeof node !== 'string' &&
typeof node !== 'undefined' &&
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { util } from 'prettier';

import type { Comment, CollectionNode } from '../../slang-nodes/types.d.ts';
import type { Comment, Collection } from '../../slang-nodes/types.d.ts';

const { addDanglingComment, addLeadingComment } = util;

export default function addCollectionNodeFirstComment(
node: CollectionNode,
export default function addCollectionFirstComment(
node: Collection,
comment: Comment
): void {
if (node.items.length === 0) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { util } from 'prettier';

import type { Comment, CollectionNode } from '../../slang-nodes/types.d.ts';
import type { Comment, Collection } from '../../slang-nodes/types.d.ts';

const { addDanglingComment, addTrailingComment } = util;

export default function addCollectionNodeLastComment(
node: CollectionNode,
export default function addCollectionLastComment(
node: Collection,
comment: Comment
): void {
if (node.items.length === 0) {
Expand Down
8 changes: 4 additions & 4 deletions src/slang-comments/handlers/handle-block-comments.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { NonterminalKind } from '@nomicfoundation/slang/cst';
import addCollectionNodeFirstComment from './add-collection-node-first-comment.js';
import addCollectionNodeLastComment from './add-collection-node-last-comment.js';
import addCollectionFirstComment from './add-collection-first-comment.js';
import addCollectionLastComment from './add-collection-last-comment.js';

import type { HandlerParams } from './types.d.ts';

Expand All @@ -15,12 +15,12 @@ export default function handleBlockComments({
}

if (precedingNode?.kind === NonterminalKind.Statements) {
addCollectionNodeLastComment(precedingNode, comment);
addCollectionLastComment(precedingNode, comment);
return true;
}

if (followingNode?.kind === NonterminalKind.Statements) {
addCollectionNodeFirstComment(followingNode, comment);
addCollectionFirstComment(followingNode, comment);
return true;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { NonterminalKind } from '@nomicfoundation/slang/cst';
import { util } from 'prettier';
import { locEnd } from '../../slang-utils/loc.js';
import addCollectionNodeLastComment from './add-collection-node-last-comment.js';
import addCollectionLastComment from './add-collection-last-comment.js';

import type { HandlerParams } from './types.d.ts';

Expand Down Expand Up @@ -36,7 +36,7 @@ export default function handleContractDefinitionComments({

// The comment is at the end of the body of the ContractDefinition.
if (precedingNode?.kind === NonterminalKind.ContractMembers) {
addCollectionNodeLastComment(precedingNode, comment);
addCollectionLastComment(precedingNode, comment);
return true;
}

Expand All @@ -52,7 +52,7 @@ export default function handleContractDefinitionComments({
// If the last ContractSpecifier's an InheritanceSpecifier, the comment
// is appended to the last InheritanceType.
if (lastContractSpecifier.kind === NonterminalKind.InheritanceSpecifier) {
addCollectionNodeLastComment(lastContractSpecifier.types, comment);
addCollectionLastComment(lastContractSpecifier.types, comment);
return true;
}
if (
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { NonterminalKind } from '@nomicfoundation/slang/cst';
import { util } from 'prettier';
import addCollectionNodeLastComment from './add-collection-node-last-comment.js';
import addCollectionLastComment from './add-collection-last-comment.js';

import type { HandlerParams } from './types.d.ts';

Expand All @@ -20,7 +20,7 @@ export default function handleContractSpecifiersComments({
precedingNode.kind === NonterminalKind.ContractSpecifier
) {
if (precedingNode.variant.kind === NonterminalKind.InheritanceSpecifier) {
addCollectionNodeLastComment(precedingNode.variant.types, comment);
addCollectionLastComment(precedingNode.variant.types, comment);
return true;
}
if (precedingNode.variant.kind === NonterminalKind.StorageLayoutSpecifier) {
Expand Down
4 changes: 2 additions & 2 deletions src/slang-comments/handlers/handle-else-branch-comments.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { NonterminalKind } from '@nomicfoundation/slang/cst';
import { util } from 'prettier';
import addCollectionNodeFirstComment from './add-collection-node-first-comment.js';
import addCollectionFirstComment from './add-collection-first-comment.js';

import type { HandlerParams } from './types.d.ts';

Expand All @@ -20,7 +20,7 @@ export default function handleElseBranchComments({
followingNode.variant.kind === NonterminalKind.IfStatement
) {
if (followingNode.variant.body.variant.kind === NonterminalKind.Block) {
addCollectionNodeFirstComment(
addCollectionFirstComment(
followingNode.variant.body.variant.statements,
comment
);
Expand Down
9 changes: 3 additions & 6 deletions src/slang-comments/handlers/handle-if-statement-comments.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { NonterminalKind } from '@nomicfoundation/slang/cst';
import { util } from 'prettier';
import { locEnd } from '../../slang-utils/loc.js';
import addCollectionNodeFirstComment from './add-collection-node-first-comment.js';
import addCollectionFirstComment from './add-collection-first-comment.js';

import type { HandlerParams } from './types.d.ts';

Expand Down Expand Up @@ -45,10 +45,7 @@ export default function handleIfStatementComments({

if (followingNode.kind === NonterminalKind.IfStatement) {
if (followingNode.body.variant.kind === NonterminalKind.Block) {
addCollectionNodeFirstComment(
followingNode.body.variant.statements,
comment
);
addCollectionFirstComment(followingNode.body.variant.statements, comment);
} else {
addLeadingComment(followingNode.body.variant, comment);
}
Expand All @@ -60,7 +57,7 @@ export default function handleIfStatementComments({
// if (a) /* comment */ true
if (enclosingNode.body === followingNode) {
if (followingNode.variant.kind === NonterminalKind.Block) {
addCollectionNodeFirstComment(followingNode.variant.statements, comment);
addCollectionFirstComment(followingNode.variant.statements, comment);
} else {
addLeadingComment(followingNode, comment);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { NonterminalKind } from '@nomicfoundation/slang/cst';
import { util } from 'prettier';
import { locEnd } from '../../slang-utils/loc.js';
import addCollectionNodeFirstComment from './add-collection-node-first-comment.js';
import addCollectionNodeLastComment from './add-collection-node-last-comment.js';
import addCollectionFirstComment from './add-collection-first-comment.js';
import addCollectionLastComment from './add-collection-last-comment.js';

import type { HandlerParams } from './types.d.ts';

Expand All @@ -24,7 +24,7 @@ export default function handleInterfaceDefinitionComments({

// The comment is at the end of the body of the InterfaceDefinition.
if (precedingNode?.kind === NonterminalKind.InterfaceMembers) {
addCollectionNodeLastComment(precedingNode, comment);
addCollectionLastComment(precedingNode, comment);
return true;
}

Expand All @@ -33,7 +33,7 @@ export default function handleInterfaceDefinitionComments({
nextCharacter === '{' &&
followingNode?.kind === NonterminalKind.InterfaceMembers
) {
addCollectionNodeFirstComment(followingNode, comment);
addCollectionFirstComment(followingNode, comment);
return true;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { NonterminalKind } from '@nomicfoundation/slang/cst';
import { util } from 'prettier';
import { locEnd } from '../../slang-utils/loc.js';
import addCollectionNodeFirstComment from './add-collection-node-first-comment.js';
import addCollectionNodeLastComment from './add-collection-node-last-comment.js';
import addCollectionFirstComment from './add-collection-first-comment.js';
import addCollectionLastComment from './add-collection-last-comment.js';

import type { HandlerParams } from './types.d.ts';

Expand All @@ -24,7 +24,7 @@ export default function handleLibraryDefinitionComments({

// The comment is at the end of the body of the ContractDefinition.
if (precedingNode?.kind === NonterminalKind.LibraryMembers) {
addCollectionNodeLastComment(precedingNode, comment);
addCollectionLastComment(precedingNode, comment);
return true;
}

Expand All @@ -33,7 +33,7 @@ export default function handleLibraryDefinitionComments({
nextCharacter === '{' &&
followingNode?.kind === NonterminalKind.LibraryMembers
) {
addCollectionNodeFirstComment(followingNode, comment);
addCollectionFirstComment(followingNode, comment);
return true;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { NonterminalKind } from '@nomicfoundation/slang/cst';
import { util } from 'prettier';
import { locEnd } from '../../slang-utils/loc.js';
import addCollectionNodeFirstComment from './add-collection-node-first-comment.js';
import addCollectionFirstComment from './add-collection-first-comment.js';

import type { HandlerParams } from './types.d.ts';

Expand Down Expand Up @@ -34,7 +34,7 @@ export default function handleModifierInvocationComments({
if (followingNode.variant.arguments.items.length === 0) {
addTrailingComment(enclosingNode, comment);
} else {
addCollectionNodeFirstComment(followingNode.variant.arguments, comment);
addCollectionFirstComment(followingNode.variant.arguments, comment);
}
return true;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { NonterminalKind } from '@nomicfoundation/slang/cst';
import addCollectionNodeFirstComment from './add-collection-node-first-comment.js';
import addCollectionNodeLastComment from './add-collection-node-last-comment.js';
import addCollectionFirstComment from './add-collection-first-comment.js';
import addCollectionLastComment from './add-collection-last-comment.js';

import type { HandlerParams } from './types.d.ts';

Expand All @@ -15,12 +15,12 @@ export default function handleBlockComments({
}

if (precedingNode?.kind === NonterminalKind.Parameters) {
addCollectionNodeLastComment(precedingNode, comment);
addCollectionLastComment(precedingNode, comment);
return true;
}

if (followingNode?.kind === NonterminalKind.Parameters) {
addCollectionNodeFirstComment(followingNode, comment);
addCollectionFirstComment(followingNode, comment);
return true;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { NonterminalKind } from '@nomicfoundation/slang/cst';
import addCollectionNodeFirstComment from './add-collection-node-first-comment.js';
import addCollectionNodeLastComment from './add-collection-node-last-comment.js';
import addCollectionFirstComment from './add-collection-first-comment.js';
import addCollectionLastComment from './add-collection-last-comment.js';

import type { HandlerParams } from './types.d.ts';

Expand All @@ -15,12 +15,12 @@ export default function handlePositionalArgumentsDeclarationComments({
}

if (precedingNode?.kind === NonterminalKind.PositionalArguments) {
addCollectionNodeLastComment(precedingNode, comment);
addCollectionLastComment(precedingNode, comment);
return true;
}

if (followingNode?.kind === NonterminalKind.PositionalArguments) {
addCollectionNodeFirstComment(followingNode, comment);
addCollectionFirstComment(followingNode, comment);
return true;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { NonterminalKind } from '@nomicfoundation/slang/cst';
import { util } from 'prettier';
import { locEnd } from '../../slang-utils/loc.js';
import addCollectionNodeFirstComment from './add-collection-node-first-comment.js';
import addCollectionFirstComment from './add-collection-first-comment.js';

import type { HandlerParams } from './types.d.ts';

Expand Down Expand Up @@ -37,7 +37,7 @@ export default function handleWhileStatementComments({

if (enclosingNode.body === followingNode) {
if (followingNode.variant.kind === NonterminalKind.Block) {
addCollectionNodeFirstComment(followingNode.variant.statements, comment);
addCollectionFirstComment(followingNode.variant.statements, comment);
} else {
addLeadingComment(followingNode, comment);
}
Expand Down
4 changes: 1 addition & 3 deletions src/slang-comments/printer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@ import { isComment } from '../slang-utils/is-comment.js';
import type { AstPath, Doc } from 'prettier';
import type { AstNode } from '../slang-nodes/types.d.ts';

export function printComment(commentPath: AstPath<AstNode>): Doc {
const comment = commentPath.getNode()!;

export function printComment({ node: comment }: AstPath<AstNode>): Doc {
if (isComment(comment)) {
return comment.print();
}
Expand Down
6 changes: 3 additions & 3 deletions src/slang-nodes/BitwiseOrExpression.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,11 @@ export class BitwiseOrExpression implements SlangNode {

loc;

leftOperand;
leftOperand: Expression;

operator;
operator: string;

rightOperand;
rightOperand: Expression;

constructor(ast: ast.BitwiseOrExpression, options: ParserOptions<AstNode>) {
let metadata = getNodeMetadata(ast);
Expand Down
20 changes: 10 additions & 10 deletions src/slang-nodes/ConditionalExpression.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ import type { PrintFunction, SlangNode } from '../types.d.ts';

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

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

function experimentalTernaries(
Expand All @@ -24,7 +24,7 @@ function experimentalTernaries(
print: PrintFunction,
options: ParserOptions<AstNode>
): Doc {
const grandparent = path.getNode(2) as StrictAstNode;
const grandparent = path.grandparent as StrictAstNode;
const isNested = grandparent.kind === NonterminalKind.ConditionalExpression;
const isNestedAsTrueExpression =
isNested && grandparent.trueExpression.variant === node;
Expand All @@ -50,9 +50,10 @@ function experimentalTernaries(
path.call(print, 'trueExpression')
]);

const groupId = Symbol('Slang.ConditionalExpression.trueExpression');
const conditionAndTrueExpressionGroup = group(
[operandDoc, trueExpressionDoc],
{ id: Symbol('Slang.ConditionalExpression.trueExpression') }
{ id: groupId }
);

const falseExpression = path.call(print, 'falseExpression');
Expand All @@ -64,10 +65,9 @@ function experimentalTernaries(
: ifBreak(
[fillTab(options), indent(falseExpression)],
[' ', falseExpression],
{
// We only add `fillTab` if we are sure the trueExpression is indented
groupId: conditionAndTrueExpressionGroup.id
}
// We only add `fillTab` if we are sure the trueExpression is
// indented.
{ groupId }
)
];

Expand All @@ -87,7 +87,7 @@ function traditionalTernaries(
indent([
// Nested trueExpression and falseExpression are always printed in a new
// line
(path.getNode(2) as StrictAstNode).kind ===
(path.grandparent as StrictAstNode).kind ===
NonterminalKind.ConditionalExpression
? hardline
: line,
Expand Down
Loading