summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBen Heidemann <56122437+bcheidemann@users.noreply.github.com>2022-04-26 12:04:28 +0100
committerGitHub <noreply@github.com>2022-04-26 13:04:28 +0200
commitdc4ab1d9340a58f81beab24ef211e900acbb9ad8 (patch)
treeee341f04c26976129c113096a71ca49290b9a694
parente24b8f075ead5ceab192eed67caaa9f3ba0fbfa4 (diff)
feat(ext/console): Compact empty iterables when calling Deno.inspect with compact false (#14387)
-rw-r--r--cli/tests/unit/console_test.ts65
-rw-r--r--ext/console/02_console.js14
2 files changed, 75 insertions, 4 deletions
diff --git a/cli/tests/unit/console_test.ts b/cli/tests/unit/console_test.ts
index 39baaf1a3..4601281ff 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 607da2db6..9b54a64a1 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)