Skip to content

Commit 1dd9844

Browse files
authored
fix: comment.loc may be null or undefined (#15033)
1 parent 2bbc722 commit 1dd9844

2 files changed

Lines changed: 14 additions & 3 deletions

File tree

packages/babel-generator/src/printer.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -950,9 +950,9 @@ class Printer {
950950
for (let i = 0; i < len; i++) {
951951
const comment = comments[i];
952952

953-
if (hasLoc && "loc" in comment && !this._printedComments.has(comment)) {
954-
const commentStartLine = comment.loc?.start.line;
955-
const commentEndLine = comment.loc?.end.line;
953+
if (hasLoc && comment.loc && !this._printedComments.has(comment)) {
954+
const commentStartLine = comment.loc.start.line;
955+
const commentEndLine = comment.loc.end.line;
956956
if (type === COMMENT_TYPE.LEADING) {
957957
let offset = 0;
958958
if (i === 0) {

packages/babel-generator/test/index.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -472,6 +472,17 @@ describe("generation", function () {
472472

473473
expect(generate(ast).code).toBe("/*#__PURE__*/a();\n/*#__PURE__*/b();");
474474
});
475+
476+
it("comments with null or undefined loc", () => {
477+
const code = "/*#__PURE__*/ /*#__PURE__*/";
478+
479+
const ast = parse(code);
480+
481+
ast.comments[0].loc = null;
482+
ast.comments[1].loc = undefined;
483+
484+
expect(generate(ast).code).toBe("/*#__PURE__*/\n/*#__PURE__*/");
485+
});
475486
});
476487

477488
describe("programmatic generation", function () {

0 commit comments

Comments
 (0)