Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 36 additions & 3 deletions src/bun.js/ConsoleObject.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1922,8 +1922,18 @@ pub const Formatter = struct {
if (tag.cell.isHidden()) return;
if (ctx.i == 0) {
handleFirstProperty(ctx, globalThis, ctx.parent) catch return;
} else {
} else if (ctx.i > 1) {
this.printComma(Writer, writer_, enable_ansi_colors) catch unreachable;
} else {
// If this is the first property of an empty array, we don't need to print a comma
const should_print_comma = if (ctx.parent.isArray())
(ctx.parent.getLength(globalThis) catch 0) > 0
else
true;

if (should_print_comma) {
this.printComma(Writer, writer_, enable_ansi_colors) catch unreachable;
}
}

defer ctx.i += 1;
Expand Down Expand Up @@ -2385,8 +2395,31 @@ pub const Formatter = struct {
// }

if (len == 0) {
writer.writeAll("[]");
this.addForNewLine(2);
writer.writeAll("[");
this.addForNewLine(1);

const prev_quote_strings = this.quote_strings;
this.quote_strings = true;
defer this.quote_strings = prev_quote_strings;
if (!jsType.isArguments()) {
const Iterator = PropertyIterator(Writer, enable_ansi_colors);
var iter = Iterator{
.formatter = this,
.writer = writer_,
.always_newline = !this.single_line and (this.always_newline_scope or this.goodTimeForANewLine()),
.single_line = this.single_line,
.parent = value,
.i = 1,
};
try value.forEachPropertyNonIndexed(this.globalThis, &iter, Iterator.forEach);
if (this.failed) return;

// We need to print a space at the end if the empty array has custom properties
if (iter.i > 1) {
writer.writeAll(" ");
}
}
writer.writeAll("]");
return;
}

Expand Down
5 changes: 5 additions & 0 deletions test/js/web/console/console-log.expected.txt
Original file line number Diff line number Diff line change
Expand Up @@ -240,3 +240,8 @@ Hello NaN %j 1
Hello \5 6,
Hello %i 5 6
%d 1
[ field_1: "field_1" ]
[ 1, 2, 3, field_1: "field_1" ]
[]
[ field1: "field_1", field2: 2 ]
[ 1, 2, 3, field1: "field_1", field2: 2 ]
29 changes: 29 additions & 0 deletions test/js/web/console/console-log.js
Original file line number Diff line number Diff line change
Expand Up @@ -270,3 +270,32 @@ console.log("Hello %%i %i", 5, 6);

// doesn't go out of bounds when printing
console.log("%%d", 1);

// Test array with extra properties
{
// empty array with extra property
const emptyArrayWithItem = [];
emptyArrayWithItem["field_1"] = "field_1";
console.log(emptyArrayWithItem);

// Non-empty array with extra property
const nonEmptyArrayWithItem = [1, 2, 3];
nonEmptyArrayWithItem["field_1"] = "field_1";
console.log(nonEmptyArrayWithItem);

// empty array
const emptyArray = [];
console.log(emptyArray);

// Multiple properties on empty array
const emptyArrayWithItems = [];
emptyArrayWithItems.field1 = "field_1";
emptyArrayWithItems.field2 = 2;
console.log(emptyArrayWithItems);

// Multiple properties on non empty array
const nonEmptyArrayWithItems = [1, 2, 3];
nonEmptyArrayWithItems.field1 = "field_1";
nonEmptyArrayWithItems.field2 = 2;
console.log(nonEmptyArrayWithItems);
}