mirror of
https://github.com/denoland/deno.git
synced 2025-01-03 12:58:54 -05:00
fix(cli/console): quote object symbol keys that are invalid identifiers (#7553)
This commit is contained in:
parent
c30c782c2c
commit
a33315aaa7
2 changed files with 32 additions and 1 deletions
|
@ -481,6 +481,19 @@
|
|||
return quoteString(string);
|
||||
}
|
||||
|
||||
// Surround a symbol's description in quotes when it is required (e.g the description has non printable characters).
|
||||
function maybeQuoteSymbol(symbol) {
|
||||
if (symbol.description === undefined) {
|
||||
return symbol.toString();
|
||||
}
|
||||
|
||||
if (/^[a-zA-Z_][a-zA-Z_.0-9]*$/.test(symbol.description)) {
|
||||
return symbol.toString();
|
||||
}
|
||||
|
||||
return `Symbol(${quoteString(symbol.description)})`;
|
||||
}
|
||||
|
||||
// Print strings when they are inside of arrays or objects with quotes
|
||||
function inspectValueWithQuotes(
|
||||
value,
|
||||
|
@ -734,7 +747,7 @@
|
|||
}
|
||||
for (const key of symbolKeys) {
|
||||
entries.push(
|
||||
`${replaceEscapeSequences(key.toString())}: ${
|
||||
`${maybeQuoteSymbol(key)}: ${
|
||||
inspectValueWithQuotes(
|
||||
value[key],
|
||||
ctx,
|
||||
|
|
|
@ -110,6 +110,24 @@ unitTest(
|
|||
),
|
||||
`{ "foo\\b": "bar\\n", "bar\\r": "baz\\t", "qux\\x00": "qux\\x00" }`,
|
||||
);
|
||||
assertEquals(
|
||||
stringify(
|
||||
{
|
||||
[Symbol("foo\b")]: `Symbol("foo\n")`,
|
||||
[Symbol("bar\n")]: `Symbol("bar\n")`,
|
||||
[Symbol("bar\r")]: `Symbol("bar\r")`,
|
||||
[Symbol("baz\t")]: `Symbol("baz\t")`,
|
||||
[Symbol("qux\0")]: `Symbol("qux\0")`,
|
||||
},
|
||||
),
|
||||
`{
|
||||
Symbol("foo\\b"): 'Symbol("foo\\n\")',
|
||||
Symbol("bar\\n"): 'Symbol("bar\\n\")',
|
||||
Symbol("bar\\r"): 'Symbol("bar\\r\")',
|
||||
Symbol("baz\\t"): 'Symbol("baz\\t\")',
|
||||
Symbol("qux\\x00"): 'Symbol(\"qux\\x00")'
|
||||
}`,
|
||||
);
|
||||
assertEquals(
|
||||
stringify(new Set(["foo\n", "foo\r", "foo\0"])),
|
||||
`Set { "foo\\n", "foo\\r", "foo\\x00" }`,
|
||||
|
|
Loading…
Reference in a new issue