Skip to content

Commit 9c366c7

Browse files
committed
computing lastPrintableIndex only once instead of checking in every loop
1 parent d7eed72 commit 9c366c7

3 files changed

Lines changed: 9 additions & 8 deletions

File tree

src/slang-printers/print-comments.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,20 +22,19 @@ export function printComments(
2222
options: ParserOptions<AstNode>
2323
): Doc[] {
2424
if (node.comments === undefined) return [];
25+
const lastPrintableIndex = node.comments.findLastIndex(isPrintable);
2526
return joinExisting(
2627
line,
27-
path.map((commentPath, index, comments: Comment[]) => {
28+
path.map((commentPath, index) => {
2829
const comment = commentPath.node;
2930
if (!isPrintable(comment)) {
3031
return '';
3132
}
3233
comment.printed = true;
33-
const isLast =
34-
index === comments.length - 1 ||
35-
comments.slice(index + 1).findIndex(isPrintable) === -1;
3634
return [
3735
printComment(commentPath),
38-
!isLast && util.isNextLineEmpty(options.originalText, locEnd(comment))
36+
index !== lastPrintableIndex &&
37+
util.isNextLineEmpty(options.originalText, locEnd(comment))
3938
? hardline
4039
: ''
4140
];

src/slangPrinter.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ function hasNodeIgnoreComment({ comments }: StrictAstNode): boolean {
2222
);
2323
}
2424

25-
function ignoreComments(path: AstPath<AstNode>): void {
25+
function ignoreComments(path: AstPath<StrictAstNode>): void {
2626
const node = path.node;
2727
// We ignore anything that is not an object
2828
if (node === null || typeof node !== 'object') return;
@@ -38,7 +38,9 @@ function ignoreComments(path: AstPath<AstNode>): void {
3838
break;
3939
// The key `comments` will contain every comment for this node.
4040
case 'comments':
41-
path.each((commentPath) => (commentPath.node.printed = true), key);
41+
if (node.comments !== undefined) {
42+
path.each((commentPath) => (commentPath.node.printed = true), key);
43+
}
4244
break;
4345
default:
4446
// If the value for that key is an Array or an Object we go deeper.

tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"noImplicitAny": true,
66
"strictPropertyInitialization": true,
77
"sourceMap": true,
8-
"target": "es6",
8+
"target": "es2023",
99
"module": "NodeNext",
1010
"moduleResolution": "NodeNext",
1111
"strict": true,

0 commit comments

Comments
 (0)