Merge pull request #1056 from djpohly/pretty-u2028-u2029

This commit is contained in:
Brian Donovan 2022-02-06 10:19:18 -08:00 committed by GitHub
commit cb7ff9a333
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 26 additions and 4 deletions

View file

@ -3086,19 +3086,25 @@ function getPossibleRaw(node:
}
}
function jsSafeStringify(str: string) {
return JSON.stringify(str).replace(/[\u2028\u2029]/g, function (m) {
return "\\u" + m.charCodeAt(0).toString(16);
});
}
function nodeStr(str: string, options: any) {
isString.assert(str);
switch (options.quote) {
case "auto": {
const double = JSON.stringify(str);
const single = swapQuotes(JSON.stringify(swapQuotes(str)));
const double = jsSafeStringify(str);
const single = swapQuotes(jsSafeStringify(swapQuotes(str)));
return double.length > single.length ? single : double;
}
case "single":
return swapQuotes(JSON.stringify(swapQuotes(str)));
return swapQuotes(jsSafeStringify(swapQuotes(str)));
case "double":
default:
return JSON.stringify(str);
return jsSafeStringify(str);
}
}

View file

@ -660,6 +660,22 @@ describe("printer", function () {
);
});
it("pretty-prints U+2028 and U+2029 as Unicode escapes", function () {
const ast = parse('"\\u2028";');
const printer = new Printer();
assert.strictEqual(
printer.printGenerically(ast).code,
'"\\u2028";',
);
const ast2 = parse('"\\u2029";');
const printer2 = new Printer();
assert.strictEqual(
printer2.printGenerically(ast2).code,
'"\\u2029";',
);
});
it("should print block comments at head of class once", function () {
// Given.
const ast = parse(