Respect preserveComments option in tempate.ast() (#11112)

This commit is contained in:
Martin Forsgren 2020-02-11 10:34:21 +01:00 committed by GitHub
parent 3907396bd8
commit bc308a1b15
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 13 additions and 1 deletions

View File

@ -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,
};

View File

@ -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");