Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve generator behavior when comments:false #15173

Merged
merged 2 commits into from Nov 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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;
}

Comment on lines +112 to +121
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since shouldPrintComment does not pass any information other than the comment content, it is safe to call it only once.

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 @@ -903,48 +909,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 @@ -996,11 +1004,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 @@ -1025,8 +1031,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 @@ -1047,7 +1057,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 @@ -1061,7 +1071,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 @@ -1073,12 +1083,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 @@ -1096,13 +1107,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 @@ -1117,15 +1126,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 = () => {};