Skip to content

Commit a662cb8

Browse files
committed
fix(core): keep comments inside static blocks and namespaces
Empty statements in those lists never own comments, but the parser only rehomed them for blocks, programs, and switch cases. A comment after the last statement, or the only semicolon, printed outside the block or was dropped. Treat static and module blocks as statement lists, and print a comment-only static body.
1 parent dc015d0 commit a662cb8

4 files changed

Lines changed: 115 additions & 19 deletions

File tree

‎packages/prettier-plugin/src/index.js‎

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3491,21 +3491,26 @@ function printTsrxNode(node, path, options, print, args) {
34913491

34923492
case 'StaticBlock': {
34933493
const printedIndexes = getPrintedStatementIndexes(node.body ?? []);
3494-
nodeContent =
3495-
printedIndexes.length > 0
3496-
? group([
3497-
'static {',
3498-
indent([
3499-
hardline,
3500-
join(
3501-
hardline,
3502-
printedIndexes.map((i) => path.call(print, 'body', i)),
3503-
),
3504-
]),
3505-
hardline,
3506-
'}',
3507-
])
3508-
: 'static {}';
3494+
if (printedIndexes.length === 0) {
3495+
// Same as an empty block: a comment-only body lives on `innerComments`.
3496+
nodeContent =
3497+
innerComments && innerComments.length > 0
3498+
? group(['static {', indent(printElementBodyComments(innerComments)), hardline, '}'])
3499+
: 'static {}';
3500+
break;
3501+
}
3502+
nodeContent = group([
3503+
'static {',
3504+
indent([
3505+
hardline,
3506+
join(
3507+
hardline,
3508+
printedIndexes.map((i) => path.call(print, 'body', i)),
3509+
),
3510+
]),
3511+
hardline,
3512+
'}',
3513+
]);
35093514
break;
35103515
}
35113516

‎packages/prettier-plugin/src/index.test.js‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7758,6 +7758,39 @@ log()
77587758
'switch (x) {\n case 1:\n a; ; // c\n b;\n}',
77597759
'switch (x) {\n case 1:\n a; // c\n b;\n}',
77607760
],
7761+
[
7762+
'class A {\n static {\n a; ; // c\n }\n b() {}\n}',
7763+
'class A {\n static {\n a; // c\n }\n b() {}\n}',
7764+
],
7765+
[
7766+
'class A {\n static {\n a; ; // c\n }\n}',
7767+
'class A {\n static {\n a; // c\n }\n}',
7768+
],
7769+
[
7770+
'class A {\n static {\n a;\n\n // c\n }\n}',
7771+
'class A {\n static {\n a;\n\n // c\n }\n}',
7772+
],
7773+
[
7774+
'class A {\n static {\n ; // c\n }\n b() {}\n}',
7775+
'class A {\n static {\n // c\n }\n b() {}\n}',
7776+
],
7777+
['class A {\n static {\n // c\n }\n}', 'class A {\n static {\n // c\n }\n}'],
7778+
[
7779+
'class A {\n static {\n ; // c\n } // d\n b() {}\n}',
7780+
'class A {\n static {\n // c\n } // d\n b() {}\n}',
7781+
],
7782+
['namespace N {\n a; ; // c\n}\nb;', 'namespace N {\n a; // c\n}\nb;'],
7783+
['namespace N {\n a; ; // c\n}', 'namespace N {\n a; // c\n}'],
7784+
['namespace N {\n ; // c\n}\nb;', 'namespace N {\n // c\n}\nb;'],
7785+
['namespace N {\n ; // c\n}', 'namespace N {\n // c\n}'],
7786+
['namespace N {\n // c\n}', 'namespace N {\n // c\n}'],
7787+
['namespace N {\n ; // c\n} // d\nb;', 'namespace N {\n // c\n} // d\nb;'],
7788+
[
7789+
'namespace N {\n export const a = 1; ; // c\n export const b = 2;\n}',
7790+
'namespace N {\n export const a = 1; // c\n export const b = 2;\n}',
7791+
],
7792+
['declare namespace N {\n ; // c\n}', 'declare namespace N {\n // c\n}'],
7793+
['module M {\n a; ; // c\n}', 'module M {\n a; // c\n}'],
77617794
])('formats %j like Prettier', async (source, expected) => {
77627795
expect(await format(source)).toBeWithNewline(expected);
77637796
});

‎packages/tsrx/src/parse/index.js‎

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -650,8 +650,16 @@ export function get_comment_handlers(source, comments, index = 0) {
650650
return;
651651
}
652652
}
653-
if (node.type === 'BlockStatement' && hasOnlyEmptyStatements(node.body)) {
654-
// Collect all comments that fall within this empty block
653+
if (
654+
(node.type === 'BlockStatement' ||
655+
node.type === 'StaticBlock' ||
656+
node.type === 'TSModuleBlock') &&
657+
hasOnlyEmptyStatements(node.body)
658+
) {
659+
// Collect all comments that fall within this empty block. A
660+
// `static` block and a module block are statement lists too, so a
661+
// comment after their only `;` (or their only comment) belongs
662+
// inside them.
655663
while (
656664
comments[0] &&
657665
comments[0].start < /** @type {AST.NodeWithLocation} */ (node).end &&
@@ -660,7 +668,15 @@ export function get_comment_handlers(source, comments, index = 0) {
660668
const comment = /** @type {AST.CommentWithLocation} */ (comments.shift());
661669
(node.innerComments ||= []).push(comment);
662670
}
663-
if (node.innerComments && node.innerComments.length > 0) {
671+
// A BlockStatement's parent owns a comment after `}`. A static
672+
// block or module block is the list entry, so that comment still
673+
// attaches below — but only when one remains. Falling through
674+
// with nothing left reads `comments[0]`.
675+
if (
676+
node.innerComments &&
677+
node.innerComments.length > 0 &&
678+
(node.type === 'BlockStatement' || !comments[0])
679+
) {
664680
return;
665681
}
666682
}
@@ -727,7 +743,9 @@ export function get_comment_handlers(source, comments, index = 0) {
727743
if (
728744
parent.type === 'BlockStatement' ||
729745
parent.type === 'Program' ||
730-
parent.type === 'ClassBody'
746+
parent.type === 'ClassBody' ||
747+
parent.type === 'StaticBlock' ||
748+
parent.type === 'TSModuleBlock'
731749
) {
732750
node_array = parent.body;
733751
} else if (parent.type === 'SwitchStatement') {

‎packages/tsrx/tests/utils/parser.test.js‎

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5961,4 +5961,44 @@ describe('comments around empty statements', () => {
59615961

59625962
expect(values(statement.consequent.trailingComments)).toEqual([' c']);
59635963
});
5964+
5965+
it('keeps a comment after the last statement inside a static block', () => {
5966+
const ast = parseModule('class A {\n\tstatic {\n\t\ta; ; // c\n\t}\n\tb() {}\n}', 'App.tsrx');
5967+
const members = firstStatement(ast, 'ClassDeclaration').body.body;
5968+
const staticBlock = as_type(members[0], 'StaticBlock');
5969+
const [statement] = staticBlock.body;
5970+
5971+
expect(values(statement.trailingComments)).toEqual([' c']);
5972+
expect(staticBlock.trailingComments).toBeUndefined();
5973+
expect(members[1].leadingComments).toBeUndefined();
5974+
});
5975+
5976+
it('keeps the comments of a static block that has only empty statements', () => {
5977+
const ast = parseModule('class A {\n\tstatic {\n\t\t; // c\n\t} // d\n\tb() {}\n}', 'App.tsrx');
5978+
const members = firstStatement(ast, 'ClassDeclaration').body.body;
5979+
const staticBlock = as_type(members[0], 'StaticBlock');
5980+
5981+
expect(values(staticBlock.innerComments)).toEqual([' c']);
5982+
expect(values(staticBlock.trailingComments)).toEqual([' d']);
5983+
expect(members[1].leadingComments).toBeUndefined();
5984+
});
5985+
5986+
it('keeps a comment after the last statement inside a namespace', () => {
5987+
const ast = parseModule('namespace N {\n\ta; ; // c\n}\nb;', 'App.tsrx');
5988+
const block = as_type(firstStatement(ast, 'TSModuleDeclaration').body, 'TSModuleBlock');
5989+
const [statement, next] = [block.body[0], ast.body[1]];
5990+
5991+
expect(values(statement.trailingComments)).toEqual([' c']);
5992+
expect(next.leadingComments).toBeUndefined();
5993+
});
5994+
5995+
it('keeps the comments of a namespace that has only empty statements', () => {
5996+
const ast = parseModule('namespace N {\n\t; // c\n} // d\nb;', 'App.tsrx');
5997+
const declaration = firstStatement(ast, 'TSModuleDeclaration');
5998+
const block = as_type(declaration.body, 'TSModuleBlock');
5999+
6000+
expect(values(block.innerComments)).toEqual([' c']);
6001+
expect(values(declaration.trailingComments)).toEqual([' d']);
6002+
expect(ast.body[1].leadingComments).toBeUndefined();
6003+
});
59646004
});

0 commit comments

Comments
 (0)