aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar Liz <accs@liz3.net> 2023-09-27 04:47:37 +0200
committerGravatar GitHub <noreply@github.com> 2023-09-26 19:47:37 -0700
commitd7b43f8ea17694641a355d5659c00563e60d0acf (patch)
tree5a4115c423ebd6c404fc4e1407914d7ff64acf76
parent648d5aecf3980997c0672746374068033e632e1d (diff)
downloadbun-d7b43f8ea17694641a355d5659c00563e60d0acf.tar.gz
bun-d7b43f8ea17694641a355d5659c00563e60d0acf.tar.zst
bun-d7b43f8ea17694641a355d5659c00563e60d0acf.zip
fix: support console.dir options object correctly (#6059)
* fix: support console,dir options object correctly `console.dir` can be passed a second argument which is a object of options. This implements that logic with the currently supported properties: `depth` and `colors`. I used node as a reference for implementation details. Fixes: https://github.com/oven-sh/bun/issues/6039 * style: format zig file * fix: implement changes from review Implements changes requested from review, like adding more test cases and refactoring code style.
-rw-r--r--src/bun.js/bindings/exports.zig36
-rw-r--r--test/js/web/console/console-log.expected.txt18
-rw-r--r--test/js/web/console/console-log.js4
3 files changed, 50 insertions, 8 deletions
diff --git a/src/bun.js/bindings/exports.zig b/src/bun.js/bindings/exports.zig
index 10326bc7c..784a6289c 100644
--- a/src/bun.js/bindings/exports.zig
+++ b/src/bun.js/bindings/exports.zig
@@ -983,22 +983,42 @@ pub const ZigConsoleClient = struct {
else
console.writer;
var writer = buffered_writer.writer();
-
const Writer = @TypeOf(writer);
- if (len > 0)
+
+ var print_length = len;
+ var print_options: FormatOptions = .{
+ .enable_colors = enable_colors,
+ .add_newline = true,
+ .flush = true,
+ };
+
+ if (message_type == .Dir and len >= 2) {
+ print_length = 1;
+ var opts = vals[1];
+ if (opts.isObject()) {
+ if (opts.get(global, "depth")) |depth_prop| {
+ if (depth_prop.isNumber())
+ print_options.max_depth = depth_prop.toU16();
+ if (depth_prop.isNull() or depth_prop.toInt64() == std.math.maxInt(i64))
+ print_options.max_depth = std.math.maxInt(u16);
+ }
+ if (opts.get(global, "colors")) |colors_prop| {
+ if (colors_prop.isBoolean())
+ print_options.enable_colors = colors_prop.toBoolean();
+ }
+ }
+ }
+
+ if (print_length > 0)
format(
level,
global,
vals,
- len,
+ print_length,
@TypeOf(buffered_writer.unbuffered_writer.context),
Writer,
writer,
- .{
- .enable_colors = enable_colors,
- .add_newline = true,
- .flush = true,
- },
+ print_options,
)
else if (message_type == .Log) {
_ = console.writer.write("\n") catch 0;
diff --git a/test/js/web/console/console-log.expected.txt b/test/js/web/console/console-log.expected.txt
index 78afa857b..39db03722 100644
--- a/test/js/web/console/console-log.expected.txt
+++ b/test/js/web/console/console-log.expected.txt
@@ -57,3 +57,21 @@ String 123 should be 2nd word, 456 == 456 and percent s %s == What okay
}
}
}
+{
+ "1": [Object ...]
+}
+{
+ "1": [Object ...]
+}
+{
+ "1": {
+ "2": [Object ...]
+ }
+}
+{
+ "1": {
+ "2": {
+ "3": 3
+ }
+ }
+}
diff --git a/test/js/web/console/console-log.js b/test/js/web/console/console-log.js
index a70d5e061..95f419781 100644
--- a/test/js/web/console/console-log.js
+++ b/test/js/web/console/console-log.js
@@ -72,3 +72,7 @@ const nestedObject = {
},
};
console.log(nestedObject);
+console.dir({ 1: { 2: { 3: 3 } } }, { depth: 0, colors: false }, "Some ignored arg");
+console.dir({ 1: { 2: { 3: 3 } } }, { depth: -1, colors: false }, "Some ignored arg");
+console.dir({ 1: { 2: { 3: 3 } } }, { depth: 1.2, colors: false }, "Some ignored arg");
+console.dir({ 1: { 2: { 3: 3 } } }, { depth: Infinity, colors: false }, "Some ignored arg");