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

chore: asserts that all comments are printed #15158

Closed
wants to merge 2 commits into from
Closed
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
28 changes: 27 additions & 1 deletion packages/babel-generator/src/index.ts
Expand Up @@ -24,9 +24,12 @@ class Generator extends Printer {
const map = opts.sourceMaps ? new SourceMap(opts, code) : null;
super(format, map);

this.opts = opts;
this.ast = ast;
}

opts: GeneratorOptions;

ast: t.Node;

/**
Expand All @@ -36,7 +39,30 @@ class Generator extends Printer {
*/

generate() {
return super.generate(this.ast);
const code = super.generate(this.ast);

const comments = (this.ast as { comments?: t.Comment[] }).comments;
if (comments?.length) {
let num = 0;
let last;
for (const v of comments) {
if (!this._printedComments.has(v) && !v.ignore) {
last = v;
num++;
}
}
if (num > 0) {
console.error(
`[@babel/generator] Error: There are ${num} comments that were not printed normally, please open an issue on babel repo.\nThe last is \`${
last.value
}\` at ${
this.opts.filename || this.opts.sourceFileName || "unknown file"
}:${last.loc?.start.line}:${last.loc?.start.column}`,
);
}
}

return code;
}
}

Expand Down
4 changes: 2 additions & 2 deletions packages/babel-generator/src/printer.ts
Expand Up @@ -908,10 +908,10 @@ class Printer {
return true;
}

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

this._printedComments.add(comment);

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

const isBlockComment = comment.type === "CommentBlock";

// Add a newline before and after a block comment, unless explicitly
Expand Down