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

fix: comment.loc may be null or undefined #15033

Merged
merged 1 commit into from Oct 10, 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
6 changes: 3 additions & 3 deletions packages/babel-generator/src/printer.ts
Expand Up @@ -950,9 +950,9 @@ class Printer {
for (let i = 0; i < len; i++) {
const comment = comments[i];

if (hasLoc && "loc" in comment && !this._printedComments.has(comment)) {
const commentStartLine = comment.loc?.start.line;
const commentEndLine = comment.loc?.end.line;
if (hasLoc && comment.loc && !this._printedComments.has(comment)) {
const commentStartLine = comment.loc.start.line;
const commentEndLine = comment.loc.end.line;
if (type === COMMENT_TYPE.LEADING) {
let offset = 0;
if (i === 0) {
Expand Down
11 changes: 11 additions & 0 deletions packages/babel-generator/test/index.js
Expand Up @@ -472,6 +472,17 @@ describe("generation", function () {

expect(generate(ast).code).toBe("/*#__PURE__*/a();\n/*#__PURE__*/b();");
});

it("comments with null or undefined loc", () => {
const code = "/*#__PURE__*/ /*#__PURE__*/";

const ast = parse(code);

ast.comments[0].loc = null;
ast.comments[1].loc = undefined;

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

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