diff --git a/cli/tests/unit/console_test.ts b/cli/tests/unit/console_test.ts index 39baaf1a36..4601281ffb 100644 --- a/cli/tests/unit/console_test.ts +++ b/cli/tests/unit/console_test.ts @@ -1939,6 +1939,71 @@ Deno.test(function inspectColors() { assertStringIncludes(Deno.inspect(1, { colors: true }), "\x1b["); }); +Deno.test(function inspectEmptyArray() { + const arr: string[] = []; + + assertEquals( + Deno.inspect(arr, { + compact: false, + trailingComma: true, + }), + "[\n]", + ); +}); + +Deno.test(function inspectDeepEmptyArray() { + const obj = { + arr: [], + }; + + assertEquals( + Deno.inspect(obj, { + compact: false, + trailingComma: true, + }), + `{ + arr: [ + ], +}`, + ); +}); + +Deno.test(function inspectEmptyMap() { + const map = new Map(); + + assertEquals( + Deno.inspect(map, { + compact: false, + trailingComma: true, + }), + "Map {\n}", + ); +}); + +Deno.test(function inspectEmptyMap() { + const set = new Set(); + + assertEquals( + Deno.inspect(set, { + compact: false, + trailingComma: true, + }), + "Set {\n}", + ); +}); + +Deno.test(function inspectEmptyMap() { + const typedArray = new Uint8Array(0); + + assertEquals( + Deno.inspect(typedArray, { + compact: false, + trailingComma: true, + }), + "Uint8Array(0) [\n]", + ); +}); + Deno.test(function inspectStringAbbreviation() { const LONG_STRING = "This is a really long string which will be abbreviated with ellipsis."; diff --git a/ext/console/02_console.js b/ext/console/02_console.js index 607da2db67..9b54a64a1e 100644 --- a/ext/console/02_console.js +++ b/ext/console/02_console.js @@ -465,12 +465,18 @@ const entryIndentation = `,\n${ StringPrototypeRepeat(DEFAULT_INDENT, level + 1) }`; - const closingIndentation = `${inspectOptions.trailingComma ? "," : ""}\n${ - StringPrototypeRepeat(DEFAULT_INDENT, level) - }`; + const closingDelimIndentation = StringPrototypeRepeat( + DEFAULT_INDENT, + level, + ); + const closingIndentation = `${ + inspectOptions.trailingComma ? "," : "" + }\n${closingDelimIndentation}`; let iContent; - if (options.group && entries.length > MIN_GROUP_LENGTH) { + if (entries.length === 0 && !inspectOptions.compact) { + iContent = `\n${closingDelimIndentation}`; + } else if (options.group && entries.length > MIN_GROUP_LENGTH) { const groups = groupEntries(entries, level, value); iContent = `${initIndentation}${ ArrayPrototypeJoin(groups, entryIndentation)