From 727f1403f71152ae19ba3c9aa3065223cba46e5d Mon Sep 17 00:00:00 2001 From: Martin Forsgren Date: Sat, 8 Feb 2020 18:12:24 +0100 Subject: [PATCH] Respect preserveComments option in tempate.ast() --- packages/babel-template/src/options.js | 2 +- packages/babel-template/test/index.js | 14 +++++++++++++- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/packages/babel-template/src/options.js b/packages/babel-template/src/options.js index 07230cf1cf0b..b1c6f38a6e86 100644 --- a/packages/babel-template/src/options.js +++ b/packages/babel-template/src/options.js @@ -131,7 +131,7 @@ export function validate(opts: mixed): TemplateOpts { placeholderWhitelist: placeholderWhitelist || undefined, placeholderPattern: placeholderPattern == null ? undefined : placeholderPattern, - preserveComments: preserveComments == null ? false : preserveComments, + preserveComments: preserveComments == null ? undefined : preserveComments, syntacticPlaceholders: syntacticPlaceholders == null ? undefined : syntacticPlaceholders, }; diff --git a/packages/babel-template/test/index.js b/packages/babel-template/test/index.js index a90db40540e4..65fc3d4080c3 100644 --- a/packages/babel-template/test/index.js +++ b/packages/babel-template/test/index.js @@ -28,6 +28,18 @@ describe("@babel/template", function() { expect(generator(output).code).toBe(comments); }); + it("should preserve comments with a flag", function() { + const output = template(comments, { preserveComments: true })(); + expect(generator(output).code).toBe(comments); + }); + + it("should preserve comments with a flag when using .ast", function() { + const output1 = template.ast(comments, { preserveComments: true }); + const output2 = template({ preserveComments: true }).ast(comments); + expect(generator(output1).code).toBe(comments); + expect(generator(output2).code).toBe(comments); + }); + describe("string-based", () => { it("should handle replacing values from an object", () => { const value = t.stringLiteral("some string value"); @@ -219,7 +231,7 @@ describe("@babel/template", function() { }); }); - describe.only(".syntacticPlaceholders", () => { + describe(".syntacticPlaceholders", () => { it("works in function body", () => { const output = template(`function f() %%A%%`)({ A: t.blockStatement([]),