From bd2176ffb0f350ebd932790765bf7cc461761394 Mon Sep 17 00:00:00 2001 From: Yash Singh Date: Mon, 4 Mar 2024 17:46:19 -0800 Subject: [PATCH] fix: format specifier without characters in between (#9209) * fix: format specifier without spaces in between, resolves #9208 * chore: test in console-log.test.ts --- src/bun.js/ConsoleObject.zig | 7 +++++++ .../util/node-inspect-tests/parallel/util-format.test.js | 5 +++++ test/js/web/console/console-log.expected.txt | 1 + test/js/web/console/console-log.js | 2 ++ 4 files changed, 15 insertions(+) diff --git a/src/bun.js/ConsoleObject.zig b/src/bun.js/ConsoleObject.zig index 44f02c83f5..a43dfc3091 100644 --- a/src/bun.js/ConsoleObject.zig +++ b/src/bun.js/ConsoleObject.zig @@ -1226,7 +1226,13 @@ pub const Formatter = struct { var i: u32 = 0; var len: u32 = @as(u32, @truncate(slice.len)); var any_non_ascii = false; + var hit_percent = false; while (i < len) : (i += 1) { + if (hit_percent) { + i = 0; + hit_percent = false; + } + switch (slice[i]) { '%' => { i += 1; @@ -1251,6 +1257,7 @@ pub const Formatter = struct { any_non_ascii = false; slice = slice[@min(slice.len, i + 1)..]; i = 0; + hit_percent = true; len = @as(u32, @truncate(slice.len)); const next_value = this.remaining_values[0]; this.remaining_values = this.remaining_values[1..]; diff --git a/test/js/node/util/node-inspect-tests/parallel/util-format.test.js b/test/js/node/util/node-inspect-tests/parallel/util-format.test.js index 045671cf7c..1671f192f3 100644 --- a/test/js/node/util/node-inspect-tests/parallel/util-format.test.js +++ b/test/js/node/util/node-inspect-tests/parallel/util-format.test.js @@ -59,7 +59,9 @@ test("no assertion failures", () => { assert.strictEqual(util.format("%d", Infinity), "Infinity"); assert.strictEqual(util.format("%d", -Infinity), "-Infinity"); assert.strictEqual(util.format("%d %d", 42, 43), "42 43"); + assert.strictEqual(util.format("%d%d", 42, 43), "4243"); assert.strictEqual(util.format("%d %d", 42), "42 %d"); + assert.strictEqual(util.format("%d%d", 42), "42%d"); assert.strictEqual(util.format("%d", 1180591620717411303424), "1.1805916207174113e+21"); assert.strictEqual(util.format("%d", 1180591620717411303424n), "1180591620717411303424n"); assert.strictEqual( @@ -151,7 +153,9 @@ test("no assertion failures", () => { assert.strictEqual(util.format("%s", -0), "-0"); assert.strictEqual(util.format("%s", "-0.0"), "-0.0"); assert.strictEqual(util.format("%s %s", 42, 43), "42 43"); + assert.strictEqual(util.format("%s%s", 42, 43), "4243"); assert.strictEqual(util.format("%s %s", 42), "42 %s"); + assert.strictEqual(util.format("%s%s", 42), "42%s"); assert.strictEqual(util.format("%s", 42n), "42n"); assert.strictEqual(util.format("%s", Symbol("foo")), "Symbol(foo)"); assert.strictEqual(util.format("%s", true), "true"); @@ -239,6 +243,7 @@ test("no assertion failures", () => { assert.strictEqual(util.format("%j", 42), "42"); assert.strictEqual(util.format("%j", "42"), '"42"'); assert.strictEqual(util.format("%j %j", 42, 43), "42 43"); + assert.strictEqual(util.format("%j%j", 42, null), "42null"); assert.strictEqual(util.format("%j %j", 42), "42 %j"); // Object format specifier diff --git a/test/js/web/console/console-log.expected.txt b/test/js/web/console/console-log.expected.txt index bcb85c590f..6c06e1ba18 100644 --- a/test/js/web/console/console-log.expected.txt +++ b/test/js/web/console/console-log.expected.txt @@ -48,6 +48,7 @@ FooWithProp { /FooRegex/ Is it a bug or a feature that formatting numbers like 123 is colored String 123 should be 2nd word, 456 == 456 and percent s %s == What okay +123456 without space should work { foo: { name: "baz", diff --git a/test/js/web/console/console-log.js b/test/js/web/console/console-log.js index 34a744eee4..c378cf0dc4 100644 --- a/test/js/web/console/console-log.js +++ b/test/js/web/console/console-log.js @@ -53,6 +53,8 @@ console.log("Is it a bug or a feature that formatting numbers like %d is colored console.log("String %s should be 2nd word, 456 == %s and percent s %s == %s", "123", "456", "%s", "What", "okay"); +console.log("%s%s without space should work", "123", "456"); + const infinteLoop = { foo: { name: "baz",