Skip to content

Commit bea7a43

Browse files
Automatically print inner comments (#15080)
1 parent 3e094bd commit bea7a43

File tree

18 files changed

+97
-72
lines changed

18 files changed

+97
-72
lines changed

packages/babel-generator/src/generators/base.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,10 @@ export function File(this: Printer, node: t.File) {
1212
}
1313

1414
export function Program(this: Printer, node: t.Program) {
15-
this.printInnerComments(node, false);
15+
// An empty Program doesn't have any inner tokens, so
16+
// we must explicitly print its inner comments.
17+
this.noIndentInnerCommentsHere();
18+
this.printInnerComments();
1619

1720
const directivesLen = node.directives?.length;
1821
if (directivesLen) {
@@ -30,7 +33,6 @@ export function Program(this: Printer, node: t.Program) {
3033

3134
export function BlockStatement(this: Printer, node: t.BlockStatement) {
3235
this.token("{");
33-
this.printInnerComments(node);
3436

3537
const directivesLen = node.directives?.length;
3638
if (directivesLen) {

packages/babel-generator/src/generators/classes.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ export function ClassDeclaration(
3535
}
3636

3737
this.word("class");
38-
this.printInnerComments(node);
3938

4039
if (node.id) {
4140
this.space();
@@ -67,7 +66,6 @@ export { ClassDeclaration as ClassExpression };
6766

6867
export function ClassBody(this: Printer, node: t.ClassBody) {
6968
this.token("{");
70-
this.printInnerComments(node);
7169
if (node.body.length === 0) {
7270
this.token("}");
7371
} else {
@@ -137,7 +135,6 @@ export function ClassAccessorProperty(
137135
this.tsPrintClassMemberModifiers(node);
138136

139137
this.word("accessor");
140-
this.printInnerComments(node);
141138
this.space();
142139

143140
if (node.computed) {

packages/babel-generator/src/generators/expressions.ts

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,14 @@ export function UnaryExpression(this: Printer, node: t.UnaryExpression) {
2626
}
2727

2828
export function DoExpression(this: Printer, node: t.DoExpression) {
29-
if (node.async) {
30-
this.word("async");
31-
this.ensureNoLineTerminator(() => {
32-
this.printInnerComments(node);
29+
// ensure no line terminator between `async` and `do`
30+
this.ensureNoLineTerminator(() => {
31+
if (node.async) {
32+
this.word("async");
3333
this.space();
34-
});
35-
}
36-
this.word("do");
34+
}
35+
this.word("do");
36+
});
3737
this.space();
3838
this.print(node.body, node);
3939
}
@@ -234,9 +234,8 @@ export function YieldExpression(this: Printer, node: t.YieldExpression) {
234234

235235
if (node.delegate) {
236236
this.ensureNoLineTerminator(() => {
237-
this.printInnerComments(node);
237+
this.token("*");
238238
});
239-
this.token("*");
240239
if (node.argument) {
241240
this.space();
242241
// line terminators are allowed after yield*
@@ -362,12 +361,11 @@ export function V8IntrinsicIdentifier(
362361

363362
export function ModuleExpression(this: Printer, node: t.ModuleExpression) {
364363
this.word("module");
364+
this.space();
365365
// ensure no line terminator between `module` and `{`
366366
this.ensureNoLineTerminator(() => {
367-
this.printInnerComments(node);
368-
this.space();
367+
this.token("{");
369368
});
370-
this.token("{");
371369
this.indent();
372370
const { body } = node;
373371
if (body.body.length || body.directives.length) {

packages/babel-generator/src/generators/jsx.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,9 @@ export function JSXClosingElement(this: Printer, node: t.JSXClosingElement) {
101101
this.token(">");
102102
}
103103

104-
export function JSXEmptyExpression(this: Printer, node: t.JSXEmptyExpression) {
105-
this.printInnerComments(node);
104+
export function JSXEmptyExpression(this: Printer) {
105+
// This node is empty, so forcefully print its inner comments.
106+
this.printInnerComments();
106107
}
107108

108109
export function JSXFragment(this: Printer, node: t.JSXFragment) {

packages/babel-generator/src/generators/methods.ts

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,6 @@ export function _parameters(
3434
this.space();
3535
}
3636
}
37-
if (paramLength === 0) {
38-
this.printInnerComments(parent);
39-
}
4037
}
4138

4239
export function _param(
@@ -89,9 +86,6 @@ export function _methodHead(this: Printer, node: t.Method | t.TSDeclareMethod) {
8986
kind === "init"
9087
) {
9188
if (node.generator) {
92-
if (node.async) {
93-
this.printInnerComments(node);
94-
}
9589
this.token("*");
9690
this._noLineTerminator = _noLineTerminator;
9791
}
@@ -102,7 +96,6 @@ export function _methodHead(this: Printer, node: t.Method | t.TSDeclareMethod) {
10296
this._noLineTerminator = _noLineTerminator;
10397
this.print(key, node);
10498
this.token("]");
105-
this.printInnerComments(node);
10699
} else {
107100
this.print(key, node);
108101
this._noLineTerminator = _noLineTerminator;
@@ -141,11 +134,20 @@ export function _functionHead(
141134
) {
142135
if (node.async) {
143136
this.word("async");
137+
// We prevent inner comments from being printed here,
138+
// so that they are always consistently printed in the
139+
// same place regardless of the function type.
140+
this._endsWithInnerRaw = false;
144141
this.space();
145142
}
146143
this.word("function");
147-
if (node.generator) this.token("*");
148-
this.printInnerComments(node);
144+
if (node.generator) {
145+
// We prevent inner comments from being printed here,
146+
// so that they are always consistently printed in the
147+
// same place regardless of the function type.
148+
this._endsWithInnerRaw = false;
149+
this.token("*");
150+
}
149151

150152
this.space();
151153
if (node.id) {
@@ -196,7 +198,10 @@ export function ArrowFunctionExpression(
196198
this._predicate(node);
197199
this.ensureNoLineTerminator(() => {
198200
this.space();
199-
this.printInnerComments(node);
201+
// When printing (x)/*1*/=>{}, we remove the parentheses
202+
// and thus there aren't two contiguous inner tokens.
203+
// We forcefully print inner comments here.
204+
this.printInnerComments();
200205
this.token("=>");
201206
});
202207

packages/babel-generator/src/generators/modules.ts

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,18 @@ export function ExportNamespaceSpecifier(
6666
this.print(node.exported, node);
6767
}
6868

69+
export function _printAssertions(
70+
this: Printer,
71+
node: Extract<t.Node, { assertions?: t.ImportAttribute[] }>,
72+
) {
73+
this.space();
74+
this.token("{");
75+
this.space();
76+
this.printList(node.assertions, node);
77+
this.space();
78+
this.token("}");
79+
}
80+
6981
export function ExportAllDeclaration(
7082
this: Printer,
7183
node: t.ExportAllDeclaration | t.DeclareExportAllDeclaration,
@@ -88,7 +100,7 @@ export function ExportAllDeclaration(
88100
this.word("assert");
89101
});
90102
// @ts-expect-error Fixme: assertions is not defined in DeclareExportAllDeclaration
91-
this.printAssertions(node);
103+
this._printAssertions(node);
92104
} else {
93105
this.print(node.source, node);
94106
}
@@ -162,7 +174,7 @@ export function ExportNamedDeclaration(
162174
this.space();
163175
this.word("assert");
164176
});
165-
this.printAssertions(node);
177+
this._printAssertions(node);
166178
} else {
167179
this.print(node.source, node);
168180
}
@@ -186,7 +198,7 @@ export function ExportDefaultDeclaration(
186198
}
187199

188200
this.word("export");
189-
this.printInnerComments(node);
201+
this.noIndentInnerCommentsHere();
190202
this.space();
191203
this.word("default");
192204
this.space();
@@ -201,11 +213,11 @@ export function ImportDeclaration(this: Printer, node: t.ImportDeclaration) {
201213

202214
const isTypeKind = node.importKind === "type" || node.importKind === "typeof";
203215
if (isTypeKind) {
204-
this.printInnerComments(node, false);
216+
this.noIndentInnerCommentsHere();
205217
this.word(node.importKind);
206218
this.space();
207219
} else if (node.module) {
208-
this.printInnerComments(node, false);
220+
this.noIndentInnerCommentsHere();
209221
this.word("module");
210222
this.space();
211223
}
@@ -250,7 +262,7 @@ export function ImportDeclaration(this: Printer, node: t.ImportDeclaration) {
250262
this.space();
251263
this.word("assert");
252264
});
253-
this.printAssertions(node);
265+
this._printAssertions(node);
254266
} else {
255267
this.print(node.source, node);
256268
}

packages/babel-generator/src/generators/statements.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ function ForXStatement(this: Printer, node: t.ForXStatement) {
103103
this.word("await");
104104
this.space();
105105
}
106-
this.printInnerComments(node, false);
106+
this.noIndentInnerCommentsHere();
107107
this.token("(");
108108
this.print(node.left, node);
109109
this.space();

packages/babel-generator/src/generators/types.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ export function ObjectExpression(this: Printer, node: t.ObjectExpression) {
2222
const props = node.properties;
2323

2424
this.token("{");
25-
this.printInnerComments(node);
2625

2726
if (props.length) {
2827
this.space();
@@ -86,7 +85,6 @@ export function ArrayExpression(this: Printer, node: t.ArrayExpression) {
8685
const len = elems.length;
8786

8887
this.token("[");
89-
this.printInnerComments(node);
9088

9189
for (let i = 0; i < elems.length; i++) {
9290
const elem = elems[i];
@@ -132,7 +130,6 @@ export function RecordExpression(this: Printer, node: t.RecordExpression) {
132130
}
133131

134132
this.token(startToken);
135-
this.printInnerComments(node);
136133

137134
if (props.length) {
138135
this.space();
@@ -161,7 +158,6 @@ export function TupleExpression(this: Printer, node: t.TupleExpression) {
161158
}
162159

163160
this.token(startToken);
164-
this.printInnerComments(node);
165161

166162
for (let i = 0; i < elems.length; i++) {
167163
const elem = elems[i];

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy