diff --git a/packages/babel-generator/src/printer.ts b/packages/babel-generator/src/printer.ts index e7d719727752..1d1c30c33712 100644 --- a/packages/babel-generator/src/printer.ts +++ b/packages/babel-generator/src/printer.ts @@ -1017,7 +1017,12 @@ class Printer { hasLoc = false; if (len === 1) { + const singleLine = comment.loc + ? comment.loc.start.line === comment.loc.end.line + : !comment.value.includes("\n"); + const shouldSkipNewline = + singleLine && !isStatement(node) && !isClassBody(parent) && !isTSInterfaceBody(parent); @@ -1025,11 +1030,12 @@ class Printer { if (type === COMMENT_TYPE.LEADING) { this._printComment( comment, - shouldSkipNewline || isFunction(parent, { body: node }) + (shouldSkipNewline && node.type !== "ObjectExpression") || + (singleLine && isFunction(parent, { body: node })) ? COMMENT_SKIP_NEWLINE.SKIP_ALL : COMMENT_SKIP_NEWLINE.DEFAULT, ); - } else if (type === COMMENT_TYPE.TRAILING && shouldSkipNewline) { + } else if (shouldSkipNewline && type === COMMENT_TYPE.TRAILING) { this._printComment(comment, COMMENT_SKIP_NEWLINE.SKIP_ALL); } else { this._printComment(comment, COMMENT_SKIP_NEWLINE.DEFAULT); diff --git a/packages/babel-generator/test/index.js b/packages/babel-generator/test/index.js index db317db3f56f..68e043eb7a70 100644 --- a/packages/babel-generator/test/index.js +++ b/packages/babel-generator/test/index.js @@ -573,6 +573,128 @@ describe("generation", function () { }" `); }); + + it("comments without loc2", () => { + const ast = parse( + ` + (function (_templateFactory) { + "use strict"; + + const template = (0, _templateFactory.createTemplateFactory)( + /*{{somevalue}}*/ + { + "id": null, + "block": "[[[1,[34,0]]],[],false,[\\"somevalue\\"]]", + "moduleName": "(unknown template module)", + "isStrictMode": false + }); + }); + + const template = (0, _templateFactory.createTemplateFactory)( + /* + {{somevalue}} + */ + { + "id": null, + "block": "[[[1,[34,0]]],[],false,[\\"somevalue\\"]]", + "moduleName": "(unknown template module)", + "isStrictMode": false + }); + `, + { sourceType: "module" }, + ); + + for (const comment of ast.comments) { + comment.loc = undefined; + } + + expect(generate(ast).code).toMatchInlineSnapshot(` + "(function (_templateFactory) { + \\"use strict\\"; + + const template = (0, _templateFactory.createTemplateFactory)( + /*{{somevalue}}*/ + { + \\"id\\": null, + \\"block\\": \\"[[[1,[34,0]]],[],false,[\\\\\\"somevalue\\\\\\"]]\\", + \\"moduleName\\": \\"(unknown template module)\\", + \\"isStrictMode\\": false + }); + }); + const template = (0, _templateFactory.createTemplateFactory)( + /* + {{somevalue}} + */ + { + \\"id\\": null, + \\"block\\": \\"[[[1,[34,0]]],[],false,[\\\\\\"somevalue\\\\\\"]]\\", + \\"moduleName\\": \\"(unknown template module)\\", + \\"isStrictMode\\": false + });" + `); + }); + + it("comments without node.loc", () => { + const ast = parse( + ` + (function (_templateFactory) { + "use strict"; + + const template = (0, _templateFactory.createTemplateFactory)( + /*{{somevalue}}*/ + { + "id": null, + "block": "[[[1,[34,0]]],[],false,[\\"somevalue\\"]]", + "moduleName": "(unknown template module)", + "isStrictMode": false + }); + }); + + const template = (0, _templateFactory.createTemplateFactory)( + /* + {{somevalue}} + */ + { + "id": null, + "block": "[[[1,[34,0]]],[],false,[\\"somevalue\\"]]", + "moduleName": "(unknown template module)", + "isStrictMode": false + }); + `, + { sourceType: "module" }, + ); + + const ast2 = t.cloneNode(ast, true, true); + + for (let i = 0; i < ast.comments.length; i++) { + ast2.comments[i].loc = ast.comments[i].loc; + } + + expect(generate(ast2).code).toMatchInlineSnapshot(` + "(function (_templateFactory) { + \\"use strict\\"; + + const template = (0, _templateFactory.createTemplateFactory)( + /*{{somevalue}}*/ + { + \\"id\\": null, + \\"block\\": \\"[[[1,[34,0]]],[],false,[\\\\\\"somevalue\\\\\\"]]\\", + \\"moduleName\\": \\"(unknown template module)\\", + \\"isStrictMode\\": false + }); + }); + const template = (0, _templateFactory.createTemplateFactory)( + /* + {{somevalue}} + */ + { + \\"id\\": null, + \\"block\\": \\"[[[1,[34,0]]],[],false,[\\\\\\"somevalue\\\\\\"]]\\", + \\"moduleName\\": \\"(unknown template module)\\", + \\"isStrictMode\\": false + });" + `); + }); }); describe("programmatic generation", function () {