0
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-10-29 08:58:01 -04:00

fix(cli/console): surround non alpha-numeric object keys with quotes (#7550)

This commit is contained in:
Casper Beyer 2020-09-18 23:25:09 +08:00 committed by GitHub
parent ec174170ba
commit 93e8e59a7e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 11 additions and 2 deletions

View file

@ -472,6 +472,15 @@
);
}
// Surround a string with quotes when it is required (e.g the string not a valid identifier).
function maybeQuoteString(string) {
if (/^[a-zA-Z_][a-zA-Z_0-9]*$/.test(string)) {
return replaceEscapeSequences(string);
}
return quoteString(string);
}
// Print strings when they are inside of arrays or objects with quotes
function inspectValueWithQuotes(
value,
@ -713,7 +722,7 @@
for (const key of stringKeys) {
entries.push(
`${replaceEscapeSequences(key)}: ${
`${maybeQuoteString(key)}: ${
inspectValueWithQuotes(
value[key],
ctx,

View file

@ -108,7 +108,7 @@ unitTest(
stringify(
{ "foo\b": "bar\n", "bar\r": "baz\t", "qux\0": "qux\0" },
),
`{ foo\\b: "bar\\n", bar\\r: "baz\\t", qux\\x00: "qux\\x00" }`,
`{ "foo\\b": "bar\\n", "bar\\r": "baz\\t", "qux\\x00": "qux\\x00" }`,
);
assertEquals(
stringify(new Set(["foo\n", "foo\r", "foo\0"])),