Skip to content

Commit

Permalink
fix: don't deduplicate comments with same start index (#13169)
Browse files Browse the repository at this point in the history
  • Loading branch information
gzzhanghao committed Apr 21, 2021
1 parent adb5ada commit 66181db
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
6 changes: 0 additions & 6 deletions packages/babel-generator/src/printer.ts
Expand Up @@ -44,7 +44,6 @@ class Printer {
_printStack: Array<t.Node> = [];
_indent: number = 0;
_insideAux: boolean = false;
_printedCommentStarts: any = {};
_parenPushNewlineState: any = null;
_noLineTerminator: boolean = false;
_printAuxAfterOnNextUserNode: boolean = false;
Expand Down Expand Up @@ -598,11 +597,6 @@ class Printer {
if (this._printedComments.has(comment)) return;
this._printedComments.add(comment);

if (comment.start != null) {
if (this._printedCommentStarts[comment.start]) return;
this._printedCommentStarts[comment.start] = true;
}

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

// Add a newline before and after a block comment, unless explicitly
Expand Down
14 changes: 14 additions & 0 deletions packages/babel-generator/test/index.js
Expand Up @@ -348,6 +348,20 @@ describe("generation", function () {
const output = generate(type).code;
expect(output).toBe("(infer T)[]");
});

it("should not deduplicate comments with same start index", () => {
const code1 = "/*#__PURE__*/ a();";
const code2 = "/*#__PURE__*/ b();";

const ast1 = parse(code1).program;
const ast2 = parse(code2).program;

const ast = t.program([...ast1.body, ...ast2.body]);

expect(generate(ast).code).toBe(
"/*#__PURE__*/\na();\n\n/*#__PURE__*/\nb();",
);
});
});

describe("programmatic generation", function () {
Expand Down

0 comments on commit 66181db

Please sign in to comment.