From d778427ec9c624246baa2553cec43254c8afbdbb Mon Sep 17 00:00:00 2001 From: Martin Forsgren Date: Tue, 11 Feb 2020 10:34:21 +0100 Subject: [PATCH] Respect preserveComments option in tempate.ast() (#11112) --- packages/babel-template/src/options.js | 2 +- packages/babel-template/test/index.js | 12 ++++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) 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 ebac1d142d7e..58e552e94049 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");