Compare commits

..

1 Commits

Author SHA1 Message Date
Claude Bot
bcded111d2 fix(test): show empty string keys in object diffs
`Identifier::isEmpty()` returns true for both null identifiers and
empty string `""` identifiers. This caused `forEachPropertyOrdered` and
`forEachPropertyImpl` to skip properties with empty string keys when
iterating over object properties for display.

Replace `property.isEmpty()` with `property.isNull()` to only skip
null identifiers while preserving legitimate empty string property keys.
Also remove redundant `key.len == 0` check in `forEachPropertyImpl`.

Closes #18028

Co-Authored-By: Claude <noreply@anthropic.com>
2026-02-19 10:11:21 +00:00
4 changed files with 69 additions and 117 deletions

View File

@@ -137,10 +137,7 @@ pub fn canMergeSymbols(
if (Symbol.isKindHoistedOrFunction(new) and
Symbol.isKindHoistedOrFunction(existing) and
(scope.kind == .entry or scope.kind == .function_body or scope.kind == .function_args or
(new == existing and Symbol.isKindHoisted(existing) and
// In strict mode, block-scoped function declarations behave like `let` bindings
// and duplicates are a SyntaxError (ES2015+ B.3.2.1, 14.1.2).
!(scope.strict_mode != .sloppy_mode and new == .hoisted_function))))
(new == existing and Symbol.isKindHoisted(existing))))
{
return .replace_with_new;
}

View File

@@ -5138,7 +5138,7 @@ restart:
RETURN_IF_EXCEPTION(scope, void());
for (auto& property : properties) {
if (property.isEmpty() || property.isNull()) [[unlikely]]
if (property.isNull()) [[unlikely]]
continue;
// ignore constructor
@@ -5169,9 +5169,6 @@ restart:
ZigString key = toZigString(property.isSymbol() && !property.isPrivateName() ? property.impl() : property.string());
if (key.len == 0)
continue;
JSC::JSValue propertyValue = jsUndefined();
if ((slot.attributes() & PropertyAttribute::DontEnum) != 0) {
@@ -5312,7 +5309,7 @@ extern "C" [[ZIG_EXPORT(nothrow)]] bool JSC__isBigIntInInt64Range(JSC::EncodedJS
auto clientData = WebCore::clientData(vm);
for (auto property : vector) {
if (property.isEmpty() || property.isNull()) [[unlikely]]
if (property.isNull()) [[unlikely]]
continue;
// ignore constructor

View File

@@ -1,108 +0,0 @@
import { expect, test } from "bun:test";
import { bunEnv, bunExe } from "harness";
// https://github.com/oven-sh/bun/issues/14273
// In strict mode, duplicate plain function declarations in a block scope
// should be a SyntaxError (they behave like `let` bindings per ES2015+ spec).
test("strict mode: duplicate function declarations in block scope is SyntaxError", async () => {
const cases = [
// Basic block scope
`"use strict"; { function f(){} function f(){} }`,
// Nested block
`"use strict"; { { function f(){} function f(){} } }`,
// Inside if
`"use strict"; if(true){ function f(){} function f(){} }`,
// Inside for
`"use strict"; for(;;){ function f(){} function f(){} break; }`,
// Inside switch case
`"use strict"; switch(1){ case 1: function f(){} function f(){} }`,
// Function body with "use strict"
`function outer(){ "use strict"; { function f(){} function f(){} } }`,
];
for (const code of cases) {
await using proc = Bun.spawn({
cmd: [bunExe(), "-e", code],
env: bunEnv,
stderr: "pipe",
});
const [stderr, exitCode] = await Promise.all([proc.stderr.text(), proc.exited]);
expect({
code,
exitCode,
hasError: stderr.includes("has already been declared"),
}).toEqual({
code,
exitCode: 1,
hasError: true,
});
}
}, 30_000);
test("sloppy mode: duplicate function declarations in block scope is allowed", async () => {
await using proc = Bun.spawn({
cmd: [bunExe(), "-e", `{ function f(){} function f(){} }`],
env: bunEnv,
stderr: "pipe",
});
const [stderr, exitCode] = await Promise.all([proc.stderr.text(), proc.exited]);
expect(stderr).toBe("");
expect(exitCode).toBe(0);
});
test("strict mode: duplicate async function declarations in block scope is SyntaxError", async () => {
await using proc = Bun.spawn({
cmd: [bunExe(), "-e", `"use strict"; { async function f(){} async function f(){} }`],
env: bunEnv,
stderr: "pipe",
});
const [stderr, exitCode] = await Promise.all([proc.stderr.text(), proc.exited]);
expect(stderr).toInclude("has already been declared");
expect(exitCode).toBe(1);
});
test("strict mode: duplicate generator function declarations in block scope is SyntaxError", async () => {
await using proc = Bun.spawn({
cmd: [bunExe(), "-e", `"use strict"; { function* f(){} function* f(){} }`],
env: bunEnv,
stderr: "pipe",
});
const [stderr, exitCode] = await Promise.all([proc.stderr.text(), proc.exited]);
expect(stderr).toInclude("has already been declared");
expect(exitCode).toBe(1);
});
test("strict mode: duplicate function declarations at top level is allowed", async () => {
await using proc = Bun.spawn({
cmd: [bunExe(), "-e", `"use strict"; function f(){} function f(){}`],
env: bunEnv,
stderr: "pipe",
});
const [stderr, exitCode] = await Promise.all([proc.stderr.text(), proc.exited]);
expect(stderr).toBe("");
expect(exitCode).toBe(0);
});
test("strict mode: duplicate var declarations in block scope is allowed", async () => {
await using proc = Bun.spawn({
cmd: [bunExe(), "-e", `"use strict"; { var x = 1; var x = 2; }`],
env: bunEnv,
stderr: "pipe",
});
const [stderr, exitCode] = await Promise.all([proc.stderr.text(), proc.exited]);
expect(stderr).toBe("");
expect(exitCode).toBe(0);
});

View File

@@ -0,0 +1,66 @@
import { expect, test } from "bun:test";
import { bunEnv, bunExe, tempDir } from "harness";
// https://github.com/oven-sh/bun/issues/18028
// Object diff in bun test should display empty string keys correctly.
test("toStrictEqual diff shows empty string keys", () => {
expect({
val: { "": "value" },
}).toStrictEqual({
val: { "": "value" },
});
});
test("toEqual diff shows empty string keys", () => {
expect({ "": "hello" }).toEqual({ "": "hello" });
});
test("empty string key with various value types", () => {
expect({ "": 0 }).toEqual({ "": 0 });
expect({ "": null }).toEqual({ "": null });
expect({ "": "" }).toEqual({ "": "" });
expect({ "": false }).toEqual({ "": false });
expect({ "": undefined }).toEqual({ "": undefined });
});
test("empty string key mixed with other keys", () => {
expect({ foo: "bar", "": "value" }).toEqual({ foo: "bar", "": "value" });
});
test("toStrictEqual fails and shows diff with empty string key", async () => {
using dir = tempDir("issue-18028", {
"test.test.ts": `
import { test, expect } from "bun:test";
test("diff", () => {
expect({ val: {} }).toStrictEqual({ val: { "": "value" } });
});
`,
});
await using proc = Bun.spawn({
cmd: [bunExe(), "test", "test.test.ts"],
cwd: String(dir),
env: { ...bunEnv, FORCE_COLOR: "0" },
stdout: "pipe",
stderr: "pipe",
});
const [stderr, exitCode] = await Promise.all([proc.stderr.text(), proc.exited]);
// The diff section should show non-zero changed lines
expect(stderr).toContain("- Expected - 3");
expect(stderr).toContain("+ Received + 1");
// The diff should include the empty string key
expect(stderr).toContain('"": "value"');
expect(exitCode).toBe(1);
});
test("console.log shows empty string keys", () => {
const result = Bun.spawnSync({
cmd: [bunExe(), "-e", 'console.log({ "": "value", foo: "bar" })'],
env: { ...bunEnv, NO_COLOR: "1" },
});
const stdout = result.stdout.toString();
expect(stdout).toContain('"": "value"');
});