From 77d46bcb1a5de44726de283f2390bee0f76ee2e3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=B2=20Ribaudo?= Date: Thu, 4 Feb 2021 16:45:02 +0100 Subject: [PATCH] Respect the `jsescOption.minimal` generator option (#12755) --- packages/babel-generator/src/index.ts | 3 +-- packages/babel-generator/test/index.js | 24 ++++++++++++++++++++++++ 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/packages/babel-generator/src/index.ts b/packages/babel-generator/src/index.ts index 6bc9cc138d..6c1a396c70 100644 --- a/packages/babel-generator/src/index.ts +++ b/packages/babel-generator/src/index.ts @@ -58,7 +58,7 @@ function normalizeOptions(code, opts): Format { jsescOption: { quotes: "double", wrap: true, - minimal: true, + minimal: process.env.BABEL_8_BREAKING ? true : false, ...opts.jsescOption, }, recordAndTupleSyntaxType: opts.recordAndTupleSyntaxType, @@ -66,7 +66,6 @@ function normalizeOptions(code, opts): Format { if (!process.env.BABEL_8_BREAKING) { format.jsonCompatibleStrings = opts.jsonCompatibleStrings; - delete format.jsescOption.minimal; } if (format.minified) { diff --git a/packages/babel-generator/test/index.js b/packages/babel-generator/test/index.js index bf28092957..0993e5c352 100644 --- a/packages/babel-generator/test/index.js +++ b/packages/babel-generator/test/index.js @@ -700,6 +700,30 @@ describe("programmatic generation", function () { expect(output).toBe("export default (class {});"); }); }); + + describe("jsescOption.minimal", () => { + const string = t.stringLiteral("\u8868\u683C_\u526F\u672C"); + + it("true", () => { + const output = generate(string, { jsescOption: { minimal: true } }).code; + expect(output).toBe(`"表格_副本"`); + }); + + it("false", () => { + const output = generate(string, { jsescOption: { minimal: false } }).code; + expect(output).toBe(`"\\u8868\\u683C_\\u526F\\u672C"`); + }); + + it("default", () => { + const output = generate(string).code; + + if (process.env.BABEL_8_BREAKING) { + expect(output).toBe(`"表格_副本"`); + } else { + expect(output).toBe(`"\\u8868\\u683C_\\u526F\\u672C"`); + } + }); + }); }); describe("CodeGenerator", function () {