Skip to content

Commit

Permalink
Improve generator behavior when comments:false (#15173)
Browse files Browse the repository at this point in the history
  • Loading branch information
liuxingbaoyu committed Nov 25, 2022
1 parent c78fc49 commit 65c8496
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 40 deletions.
10 changes: 10 additions & 0 deletions packages/babel-generator/src/index.ts
Expand Up @@ -109,6 +109,16 @@ function normalizeOptions(
format.indent.adjustMultilineComment = false;
}

const { auxiliaryCommentBefore, auxiliaryCommentAfter, shouldPrintComment } =
format;

if (auxiliaryCommentBefore && !shouldPrintComment(auxiliaryCommentBefore)) {
format.auxiliaryCommentBefore = undefined;
}
if (auxiliaryCommentAfter && !shouldPrintComment(auxiliaryCommentAfter)) {
format.auxiliaryCommentAfter = undefined;
}

return format;
}

Expand Down
82 changes: 45 additions & 37 deletions packages/babel-generator/src/printer.ts
Expand Up @@ -35,9 +35,15 @@ const enum COMMENT_TYPE {

const enum COMMENT_SKIP_NEWLINE {
DEFAULT,
SKIP_ALL,
SKIP_LEADING,
SKIP_TRAILING,
ALL,
LEADING,
TRAILING,
}

const enum PRINT_COMMENT_HINT {
SKIP,
ALLOW,
DEFER,
}

export type Format = {
Expand Down Expand Up @@ -906,48 +912,50 @@ class Printer {
}
}

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

if (this._printedComments.has(comment)) return false;
if (comment.ignore) return PRINT_COMMENT_HINT.SKIP;

const noLineTerminator = this._noLineTerminator;
if (this._printedComments.has(comment)) return PRINT_COMMENT_HINT.SKIP;

if (
noLineTerminator &&
this._noLineTerminator &&
(HAS_NEWLINE.test(comment.value) ||
HAS_BlOCK_COMMENT_END.test(comment.value))
) {
return true;
return PRINT_COMMENT_HINT.DEFER;
}

if (!this.format.shouldPrintComment(comment.value)) return false;

this._printedComments.add(comment);

if (!this.format.shouldPrintComment(comment.value)) {
return PRINT_COMMENT_HINT.SKIP;
}

return PRINT_COMMENT_HINT.ALLOW;
}

_printComment(comment: t.Comment, skipNewLines: COMMENT_SKIP_NEWLINE) {
const noLineTerminator = this._noLineTerminator;
const isBlockComment = comment.type === "CommentBlock";

// Add a newline before and after a block comment, unless explicitly
// disallowed
const printNewLines =
isBlockComment &&
skipNewLines !== COMMENT_SKIP_NEWLINE.SKIP_ALL &&
skipNewLines !== COMMENT_SKIP_NEWLINE.ALL &&
!this._noLineTerminator;

if (
printNewLines &&
this._buf.hasContent() &&
skipNewLines !== COMMENT_SKIP_NEWLINE.SKIP_LEADING
skipNewLines !== COMMENT_SKIP_NEWLINE.LEADING
) {
this.newline(1);
}
Expand Down Expand Up @@ -999,11 +1007,9 @@ class Printer {
this.newline(1, true);
}

if (printNewLines && skipNewLines !== COMMENT_SKIP_NEWLINE.SKIP_TRAILING) {
if (printNewLines && skipNewLines !== COMMENT_SKIP_NEWLINE.TRAILING) {
this.newline(1);
}

return false;
}

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

const printed = this._printedComments.has(comment);
if (hasLoc && comment.loc && !printed) {
const shouldPrint = this._shouldPrintComment(comment);
if (shouldPrint === PRINT_COMMENT_HINT.DEFER) {
hasLoc = false;
break;
}
if (hasLoc && comment.loc && shouldPrint === PRINT_COMMENT_HINT.ALLOW) {
const commentStartLine = comment.loc.start.line;
const commentEndLine = comment.loc.end.line;
if (type === COMMENT_TYPE.LEADING) {
Expand All @@ -1050,7 +1060,7 @@ class Printer {
lastLine = commentEndLine;

maybeNewline(offset);
this._printComment(comment, COMMENT_SKIP_NEWLINE.SKIP_ALL);
this._printComment(comment, COMMENT_SKIP_NEWLINE.ALL);

if (i + 1 === len) {
maybeNewline(
Expand All @@ -1064,7 +1074,7 @@ class Printer {
lastLine = commentEndLine;

maybeNewline(offset);
if (this._printComment(comment, COMMENT_SKIP_NEWLINE.SKIP_ALL)) break;
this._printComment(comment, COMMENT_SKIP_NEWLINE.ALL);

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

maybeNewline(offset);
this._printComment(comment, COMMENT_SKIP_NEWLINE.SKIP_ALL);
this._printComment(comment, COMMENT_SKIP_NEWLINE.ALL);
}
} else {
hasLoc = false;

if (printed) continue;
if (shouldPrint !== PRINT_COMMENT_HINT.ALLOW) {
continue;
}

if (len === 1) {
const singleLine = comment.loc
Expand All @@ -1099,13 +1110,11 @@ class Printer {
comment,
(shouldSkipNewline && node.type !== "ObjectExpression") ||
(singleLine && isFunction(parent, { body: node }))
? COMMENT_SKIP_NEWLINE.SKIP_ALL
? COMMENT_SKIP_NEWLINE.ALL
: COMMENT_SKIP_NEWLINE.DEFAULT,
);
} else if (shouldSkipNewline && type === COMMENT_TYPE.TRAILING) {
if (this._printComment(comment, COMMENT_SKIP_NEWLINE.SKIP_ALL)) {
break;
}
this._printComment(comment, COMMENT_SKIP_NEWLINE.ALL);
} else {
this._printComment(comment, COMMENT_SKIP_NEWLINE.DEFAULT);
}
Expand All @@ -1120,15 +1129,14 @@ class Printer {
// /*:: b: ?string*/
// }

const skippedDueToNewline = this._printComment(
this._printComment(
comment,
i === 0
? COMMENT_SKIP_NEWLINE.SKIP_LEADING
? COMMENT_SKIP_NEWLINE.LEADING
: i === len - 1
? COMMENT_SKIP_NEWLINE.SKIP_TRAILING
? COMMENT_SKIP_NEWLINE.TRAILING
: COMMENT_SKIP_NEWLINE.DEFAULT,
);
if (skippedDueToNewline) break;
} else {
this._printComment(comment, COMMENT_SKIP_NEWLINE.DEFAULT);
}
Expand Down
@@ -1,3 +1 @@
var test = (
) => {
};
var test = () => {};

0 comments on commit 65c8496

Please sign in to comment.