Skip to content

Commit 4705f34

Browse files
committed
fix console: print custom properties on empty arrays
NodeJS prints custom properties on empty arrays. This commit adds the same support to bun. Fixes #22790
1 parent d17134f commit 4705f34

File tree

3 files changed

+72
-3
lines changed

3 files changed

+72
-3
lines changed

src/bun.js/ConsoleObject.zig

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1922,8 +1922,18 @@ pub const Formatter = struct {
19221922
if (tag.cell.isHidden()) return;
19231923
if (ctx.i == 0) {
19241924
handleFirstProperty(ctx, globalThis, ctx.parent) catch return;
1925-
} else {
1925+
} else if (ctx.i > 1) {
19261926
this.printComma(Writer, writer_, enable_ansi_colors) catch unreachable;
1927+
} else {
1928+
// If this is the first property of an empty array, we don't need to print a comma
1929+
const should_print_comma = if (ctx.parent.isArray())
1930+
(ctx.parent.getLength(globalThis) catch 0) > 0
1931+
else
1932+
true;
1933+
1934+
if (should_print_comma) {
1935+
this.printComma(Writer, writer_, enable_ansi_colors) catch unreachable;
1936+
}
19271937
}
19281938

19291939
defer ctx.i += 1;
@@ -2385,8 +2395,33 @@ pub const Formatter = struct {
23852395
// }
23862396

23872397
if (len == 0) {
2388-
writer.writeAll("[]");
2389-
this.addForNewLine(2);
2398+
writer.writeAll("[");
2399+
this.addForNewLine(1);
2400+
2401+
const prev_quote_strings = this.quote_strings;
2402+
this.quote_strings = true;
2403+
defer this.quote_strings = prev_quote_strings;
2404+
if (!jsType.isArguments()) {
2405+
const Iterator = PropertyIterator(Writer, enable_ansi_colors);
2406+
var iter = Iterator{
2407+
.formatter = this,
2408+
.writer = writer_,
2409+
.always_newline = !this.single_line and (this.always_newline_scope or this.goodTimeForANewLine()),
2410+
.single_line = this.single_line,
2411+
.parent = value,
2412+
.i = 1,
2413+
};
2414+
try value.forEachPropertyNonIndexed(this.globalThis, &iter, Iterator.forEach);
2415+
if (this.failed) return;
2416+
2417+
// We need to print a space at the end if the empty array has custom properties
2418+
const keys = try value.keys(this.globalThis);
2419+
const num_keys = try keys.getLength(this.globalThis);
2420+
if (num_keys > 0) {
2421+
writer.writeAll(" ");
2422+
}
2423+
}
2424+
writer.writeAll("]");
23902425
return;
23912426
}
23922427

test/js/web/console/console-log.expected.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,3 +240,8 @@ Hello NaN %j 1
240240
Hello \5 6,
241241
Hello %i 5 6
242242
%d 1
243+
[ field_1: "field_1" ]
244+
[ 1, 2, 3, field_1: "field_1" ]
245+
[]
246+
[ field1: "field_1", field2: 2 ]
247+
[ 1, 2, 3, field1: "field_1", field2: 2 ]

test/js/web/console/console-log.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,3 +270,32 @@ console.log("Hello %%i %i", 5, 6);
270270

271271
// doesn't go out of bounds when printing
272272
console.log("%%d", 1);
273+
274+
// Test array with extra properties
275+
{
276+
// empty array with extra property
277+
const emptyArrayWithItem = [];
278+
emptyArrayWithItem["field_1"] = "field_1";
279+
console.log(emptyArrayWithItem);
280+
281+
// Non-empty array with extra property
282+
const nonEmptyArrayWithItem = [1, 2, 3];
283+
nonEmptyArrayWithItem["field_1"] = "field_1";
284+
console.log(nonEmptyArrayWithItem);
285+
286+
// empty array
287+
const emptyArray = [];
288+
console.log(emptyArray);
289+
290+
// Multiple properties on empty array
291+
const emptyArrayWithItems = [];
292+
emptyArrayWithItems.field1 = "field_1";
293+
emptyArrayWithItems.field2 = 2;
294+
console.log(emptyArrayWithItems);
295+
296+
// Multiple properties on non empty array
297+
const nonEmptyArrayWithItems = [1, 2, 3];
298+
nonEmptyArrayWithItems.field1 = "field_1";
299+
nonEmptyArrayWithItems.field2 = 2;
300+
console.log(nonEmptyArrayWithItems);
301+
}

0 commit comments

Comments
 (0)