Skip to content

Commit 65c8496

Browse files
authored
Improve generator behavior when comments:false (#15173)
1 parent c78fc49 commit 65c8496

File tree

3 files changed

+56
-40
lines changed

3 files changed

+56
-40
lines changed

packages/babel-generator/src/index.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,16 @@ function normalizeOptions(
109109
format.indent.adjustMultilineComment = false;
110110
}
111111

112+
const { auxiliaryCommentBefore, auxiliaryCommentAfter, shouldPrintComment } =
113+
format;
114+
115+
if (auxiliaryCommentBefore && !shouldPrintComment(auxiliaryCommentBefore)) {
116+
format.auxiliaryCommentBefore = undefined;
117+
}
118+
if (auxiliaryCommentAfter && !shouldPrintComment(auxiliaryCommentAfter)) {
119+
format.auxiliaryCommentAfter = undefined;
120+
}
121+
112122
return format;
113123
}
114124

packages/babel-generator/src/printer.ts

Lines changed: 45 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,15 @@ const enum COMMENT_TYPE {
3535

3636
const enum COMMENT_SKIP_NEWLINE {
3737
DEFAULT,
38-
SKIP_ALL,
39-
SKIP_LEADING,
40-
SKIP_TRAILING,
38+
ALL,
39+
LEADING,
40+
TRAILING,
41+
}
42+
43+
const enum PRINT_COMMENT_HINT {
44+
SKIP,
45+
ALLOW,
46+
DEFER,
4147
}
4248

4349
export type Format = {
@@ -906,48 +912,50 @@ class Printer {
906912
}
907913
}
908914

909-
// Returns `true` if the comment cannot be printed in this position due to
915+
// Returns `PRINT_COMMENT_HINT.DEFER` if the comment cannot be printed in this position due to
910916
// line terminators, signaling that the print comments loop can stop and
911-
// resume printing comments at the next posisble position. This happens when
917+
// resume printing comments at the next possible position. This happens when
912918
// printing inner comments, since if we have an inner comment with a multiline
913919
// there is at least one inner position where line terminators are allowed.
914-
_printComment(
915-
comment: t.Comment,
916-
skipNewLines: COMMENT_SKIP_NEWLINE,
917-
): boolean {
920+
_shouldPrintComment(comment: t.Comment): PRINT_COMMENT_HINT {
918921
// Some plugins (such as flow-strip-types) use this to mark comments as removed using the AST-root 'comments' property,
919922
// where they can't manually mutate the AST node comment lists.
920-
if (comment.ignore) return false;
921-
922-
if (this._printedComments.has(comment)) return false;
923+
if (comment.ignore) return PRINT_COMMENT_HINT.SKIP;
923924

924-
const noLineTerminator = this._noLineTerminator;
925+
if (this._printedComments.has(comment)) return PRINT_COMMENT_HINT.SKIP;
925926

926927
if (
927-
noLineTerminator &&
928+
this._noLineTerminator &&
928929
(HAS_NEWLINE.test(comment.value) ||
929930
HAS_BlOCK_COMMENT_END.test(comment.value))
930931
) {
931-
return true;
932+
return PRINT_COMMENT_HINT.DEFER;
932933
}
933934

934-
if (!this.format.shouldPrintComment(comment.value)) return false;
935-
936935
this._printedComments.add(comment);
937936

937+
if (!this.format.shouldPrintComment(comment.value)) {
938+
return PRINT_COMMENT_HINT.SKIP;
939+
}
940+
941+
return PRINT_COMMENT_HINT.ALLOW;
942+
}
943+
944+
_printComment(comment: t.Comment, skipNewLines: COMMENT_SKIP_NEWLINE) {
945+
const noLineTerminator = this._noLineTerminator;
938946
const isBlockComment = comment.type === "CommentBlock";
939947

940948
// Add a newline before and after a block comment, unless explicitly
941949
// disallowed
942950
const printNewLines =
943951
isBlockComment &&
944-
skipNewLines !== COMMENT_SKIP_NEWLINE.SKIP_ALL &&
952+
skipNewLines !== COMMENT_SKIP_NEWLINE.ALL &&
945953
!this._noLineTerminator;
946954

947955
if (
948956
printNewLines &&
949957
this._buf.hasContent() &&
950-
skipNewLines !== COMMENT_SKIP_NEWLINE.SKIP_LEADING
958+
skipNewLines !== COMMENT_SKIP_NEWLINE.LEADING
951959
) {
952960
this.newline(1);
953961
}
@@ -999,11 +1007,9 @@ class Printer {
9991007
this.newline(1, true);
10001008
}
10011009

1002-
if (printNewLines && skipNewLines !== COMMENT_SKIP_NEWLINE.SKIP_TRAILING) {
1010+
if (printNewLines && skipNewLines !== COMMENT_SKIP_NEWLINE.TRAILING) {
10031011
this.newline(1);
10041012
}
1005-
1006-
return false;
10071013
}
10081014

10091015
_printComments(
@@ -1028,8 +1034,12 @@ class Printer {
10281034
for (let i = 0; i < len; i++) {
10291035
const comment = comments[i];
10301036

1031-
const printed = this._printedComments.has(comment);
1032-
if (hasLoc && comment.loc && !printed) {
1037+
const shouldPrint = this._shouldPrintComment(comment);
1038+
if (shouldPrint === PRINT_COMMENT_HINT.DEFER) {
1039+
hasLoc = false;
1040+
break;
1041+
}
1042+
if (hasLoc && comment.loc && shouldPrint === PRINT_COMMENT_HINT.ALLOW) {
10331043
const commentStartLine = comment.loc.start.line;
10341044
const commentEndLine = comment.loc.end.line;
10351045
if (type === COMMENT_TYPE.LEADING) {
@@ -1050,7 +1060,7 @@ class Printer {
10501060
lastLine = commentEndLine;
10511061

10521062
maybeNewline(offset);
1053-
this._printComment(comment, COMMENT_SKIP_NEWLINE.SKIP_ALL);
1063+
this._printComment(comment, COMMENT_SKIP_NEWLINE.ALL);
10541064

10551065
if (i + 1 === len) {
10561066
maybeNewline(
@@ -1064,7 +1074,7 @@ class Printer {
10641074
lastLine = commentEndLine;
10651075

10661076
maybeNewline(offset);
1067-
if (this._printComment(comment, COMMENT_SKIP_NEWLINE.SKIP_ALL)) break;
1077+
this._printComment(comment, COMMENT_SKIP_NEWLINE.ALL);
10681078

10691079
if (i + 1 === len) {
10701080
maybeNewline(Math.min(1, nodeEndLine - lastLine)); // TODO: Improve here when inner comments processing is stronger
@@ -1076,12 +1086,13 @@ class Printer {
10761086
lastLine = commentEndLine;
10771087

10781088
maybeNewline(offset);
1079-
this._printComment(comment, COMMENT_SKIP_NEWLINE.SKIP_ALL);
1089+
this._printComment(comment, COMMENT_SKIP_NEWLINE.ALL);
10801090
}
10811091
} else {
10821092
hasLoc = false;
1083-
1084-
if (printed) continue;
1093+
if (shouldPrint !== PRINT_COMMENT_HINT.ALLOW) {
1094+
continue;
1095+
}
10851096

10861097
if (len === 1) {
10871098
const singleLine = comment.loc
@@ -1099,13 +1110,11 @@ class Printer {
10991110
comment,
11001111
(shouldSkipNewline && node.type !== "ObjectExpression") ||
11011112
(singleLine && isFunction(parent, { body: node }))
1102-
? COMMENT_SKIP_NEWLINE.SKIP_ALL
1113+
? COMMENT_SKIP_NEWLINE.ALL
11031114
: COMMENT_SKIP_NEWLINE.DEFAULT,
11041115
);
11051116
} else if (shouldSkipNewline && type === COMMENT_TYPE.TRAILING) {
1106-
if (this._printComment(comment, COMMENT_SKIP_NEWLINE.SKIP_ALL)) {
1107-
break;
1108-
}
1117+
this._printComment(comment, COMMENT_SKIP_NEWLINE.ALL);
11091118
} else {
11101119
this._printComment(comment, COMMENT_SKIP_NEWLINE.DEFAULT);
11111120
}
@@ -1120,15 +1129,14 @@ class Printer {
11201129
// /*:: b: ?string*/
11211130
// }
11221131

1123-
const skippedDueToNewline = this._printComment(
1132+
this._printComment(
11241133
comment,
11251134
i === 0
1126-
? COMMENT_SKIP_NEWLINE.SKIP_LEADING
1135+
? COMMENT_SKIP_NEWLINE.LEADING
11271136
: i === len - 1
1128-
? COMMENT_SKIP_NEWLINE.SKIP_TRAILING
1137+
? COMMENT_SKIP_NEWLINE.TRAILING
11291138
: COMMENT_SKIP_NEWLINE.DEFAULT,
11301139
);
1131-
if (skippedDueToNewline) break;
11321140
} else {
11331141
this._printComment(comment, COMMENT_SKIP_NEWLINE.DEFAULT);
11341142
}
Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1 @@
1-
var test = (
2-
) => {
3-
};
1+
var test = () => {};

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