diff --git a/packages/babel-generator/src/printer.ts b/packages/babel-generator/src/printer.ts index 491e096033b3..b5c37f6b5b58 100644 --- a/packages/babel-generator/src/printer.ts +++ b/packages/babel-generator/src/printer.ts @@ -130,11 +130,6 @@ class Printer { _indentInnerComments: boolean = true; generate(ast: t.Node) { - // Avoid inserting unexpected blank lines at the beginning and in the middle. - if (this.format.retainLines && ast.loc) { - this._buf._position.line = ast.loc.start.line; - } - this.print(ast); this._maybeAddAuxComment(); diff --git a/packages/babel-generator/test/index.js b/packages/babel-generator/test/index.js index b5816f24b710..f9816df02a35 100644 --- a/packages/babel-generator/test/index.js +++ b/packages/babel-generator/test/index.js @@ -1047,6 +1047,18 @@ describe("programmatic generation", function () { expect(output).toBe("() => void"); }); + it("generate a child node with retainLines", () => { + const node = parse("a;\n\nexpect(a).toMatchInlineSnapshot(`[1, 2]`);") + .program.body[1]; + const output = generate(node, { retainLines: true }).code; + + expect(output).toMatchInlineSnapshot(` + " + + expect(a).toMatchInlineSnapshot(\`[1, 2]\`);" + `); + }); + describe("directives", function () { it("preserves escapes", function () { const directive = t.directive(