Skip to content

Commit 08030a9

Browse files
committed
no need to initiate an empty comments array for every single node, prettier will create them if needed and only a few places need to check if it's defined
1 parent f6cc475 commit 08030a9

9 files changed

Lines changed: 21 additions & 14 deletions

File tree

src/slang-nodes/ContractMembers.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ export class ContractMembers extends SlangNode {
3030
print: PrintFunction,
3131
options: ParserOptions<AstNode>
3232
): Doc {
33-
return this.items.length > 0 || this.comments.length > 0
33+
return this.items.length > 0 || (this.comments?.length || 0) > 0
3434
? printSeparatedItem(printPreservingEmptyLines(path, print, options), {
3535
firstSeparator: hardline
3636
})

src/slang-nodes/IfStatement.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ export class IfStatement extends SlangNode {
5151
this.elseBranch
5252
? [
5353
bodyKind !== NonterminalKind.Block || // else on a new line if body is not a block
54-
bodyComments.some(
54+
bodyComments?.some(
5555
(comment) =>
5656
!isBlockComment(comment) || comment.placement === 'ownLine'
5757
) // or if body has trailing single line comments or a block comment on a new line

src/slang-nodes/InterfaceMembers.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ export class InterfaceMembers extends SlangNode {
3030
print: PrintFunction,
3131
options: ParserOptions<AstNode>
3232
): Doc {
33-
return this.items.length > 0 || this.comments.length > 0
33+
return this.items.length > 0 || (this.comments?.length || 0) > 0
3434
? printSeparatedItem(printPreservingEmptyLines(path, print, options), {
3535
firstSeparator: hardline
3636
})

src/slang-nodes/LibraryMembers.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ export class LibraryMembers extends SlangNode {
3030
print: PrintFunction,
3131
options: ParserOptions<AstNode>
3232
): Doc {
33-
return this.items.length > 0 || this.comments.length > 0
33+
return this.items.length > 0 || (this.comments?.length || 0) > 0
3434
? printSeparatedItem(printPreservingEmptyLines(path, print, options), {
3535
firstSeparator: hardline
3636
})

src/slang-nodes/SlangNode.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ function reversedIterator<T>(children: T[]): Iterable<T> {
3737
}
3838

3939
export class SlangNode {
40-
comments: Comment[] = [];
40+
comments?: Comment[];
4141

4242
loc: AstLocation;
4343

src/slang-nodes/Statements.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ export class Statements extends SlangNode {
3030
print: PrintFunction,
3131
options: ParserOptions<AstNode>
3232
): Doc {
33-
return this.items.length > 0 || this.comments.length > 0
33+
return this.items.length > 0 || (this.comments?.length || 0) > 0
3434
? printSeparatedItem(printPreservingEmptyLines(path, print, options), {
3535
firstSeparator: hardline
3636
})

src/slang-nodes/YulStatements.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ export class YulStatements extends SlangNode {
3030
print: PrintFunction,
3131
options: ParserOptions<AstNode>
3232
): Doc {
33-
return this.items.length > 0 || this.comments.length > 0
33+
return this.items.length > 0 || (this.comments?.length || 0) > 0
3434
? printSeparatedItem(printPreservingEmptyLines(path, print, options), {
3535
firstSeparator: hardline
3636
})

src/slang-printers/print-comments.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,11 @@ import { printComment } from '../slang-comments/printer.js';
33
import { joinExisting } from '../slang-utils/join-existing.js';
44

55
import type { AstPath, Doc, ParserOptions } from 'prettier';
6-
import type { AstNode, Comment } from '../slang-nodes/types.d.ts';
6+
import type {
7+
AstNode,
8+
Comment,
9+
StrictAstNode
10+
} from '../slang-nodes/types.d.ts';
711
import { locEnd } from '../slang-utils/loc.js';
812

913
const { hardline, line } = doc.builders;
@@ -13,9 +17,10 @@ function isPrintable(comment: Comment): boolean {
1317
}
1418

1519
export function printComments(
16-
path: AstPath<AstNode>,
20+
path: AstPath<StrictAstNode>,
1721
options: ParserOptions<AstNode>
1822
): Doc[] {
23+
if (typeof path.node.comments === 'undefined') return [];
1924
return joinExisting(
2025
line,
2126
path.map((commentPath, index, comments: Comment[]) => {

src/slangPrinter.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,13 @@ import type { PrintFunction } from './types.d.ts';
88
function hasNodeIgnoreComment({ comments }: StrictAstNode): boolean {
99
// Prettier sets SourceUnit's comments to undefined after assigning comments
1010
// to each node.
11-
return comments?.some(
12-
(comment) =>
13-
comment.value
14-
.slice(2, isBlockComment(comment) ? -2 : undefined)
15-
.trim() === 'prettier-ignore'
11+
return Boolean(
12+
comments?.some(
13+
(comment) =>
14+
comment.value
15+
.slice(2, isBlockComment(comment) ? -2 : undefined)
16+
.trim() === 'prettier-ignore'
17+
)
1618
);
1719
}
1820

0 commit comments

Comments
 (0)