Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
4 changes: 4 additions & 0 deletions src/js_parser/fold.rs
Original file line number Diff line number Diff line change
Expand Up @@ -483,6 +483,10 @@ impl<'a, const TYPESCRIPT: bool, const SCAN_ONLY: bool> P<'a, TYPESCRIPT, SCAN_O
name,
)
&& name != b"__proto__"
// "({ f: class {} }).f" assigns name "f"; inlining
// would drop that and expose the class to the outer
// NamedEvaluation position instead.
Comment thread
robobun marked this conversation as resolved.
Resolved
&& !value.is_anonymous_named()
{
return Some(value);
}
Expand Down
38 changes: 29 additions & 9 deletions src/js_parser/visit/visit_binary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,7 @@
// "(sideEffects(), 2)" => "(sideEffects(), 2)"
// "(0, this.fn)" => "this.fn"
// "(0, this.fn)()" => "(0, this.fn)()"
// "(0, class {})" => "(0, class {})"
if p.options.features.minify_syntax {
// If e_.left is itself a comma, its .left was already simplified
// by the previous unwind step; only simplify its .right to avoid
Expand All @@ -221,14 +222,24 @@
};
if let Some(simplified_left) = simplified_left {
if simplified_left.is_empty() {
return e_.right;
if e_.right.is_anonymous_named() {
e_.left = Expr {
data: prefill::data::ZERO,
loc: e_.left.loc,
};
} else {
return e_.right;
}
} else {
e_.left = simplified_left;
}
e_.left = simplified_left;
} else {
// The left operand has no side effects, but we need to preserve
// the comma operator semantics when used as a call target
if is_call_target && e_.right.has_value_for_this_in_call() {
// Keep the comma expression to strip "this" binding
// the comma operator semantics when used as a call target, and
// avoid exposing an anonymous function/class to NamedEvaluation.
Comment thread
robobun marked this conversation as resolved.
Resolved
if (is_call_target && e_.right.has_value_for_this_in_call())
|| e_.right.is_anonymous_named()
{
e_.left = Expr {
data: prefill::data::ZERO,
loc: e_.left.loc,
Expand Down Expand Up @@ -371,7 +382,10 @@
// "(null ?? fn)()" => "fn()"
// "(null ?? this.fn)" => "this.fn"
// "(null ?? this.fn)()" => "(0, this.fn)()"
if is_call_target && e_.right.has_value_for_this_in_call() {
// "(null ?? class {})" => "(0, class {})"
if (is_call_target && e_.right.has_value_for_this_in_call())
|| e_.right.is_anonymous_named()
{
return Expr::join_with_comma(
Expr {
data: ExprData::ENumber(E::Number::new(0.0)),
Expand All @@ -391,10 +405,13 @@
return e_.left;
} else if side_effects.ok && side_effects.side_effects == SideEffects::NoSideEffects
{
// "(0 || fn)()" => "fn()"
// "(0 || this.fn)" => "this.fn"
// "(0 || this.fn)()" => "(0, this.fn)()"
if is_call_target && e_.right.has_value_for_this_in_call() {
// "(0 || class {})" => "(0, class {})"
if (is_call_target && e_.right.has_value_for_this_in_call())
|| e_.right.is_anonymous_named()
{

Check failure on line 414 in src/js_parser/visit/visit_binary.rs

View check run for this annotation

Claude / Claude Code Review

Left-operand folds in ||/?? still expose anonymous fn/class to NamedEvaluation

The mirror `return e_.left` sites in these same match arms are unguarded: `(function(){}) || 0`, `(()=>{}) ?? 0`, and `(class{}) ?? 0` still fold to a bare anonymous fn/class and pick up the binding name. `to_boolean` returns `{ok, value:true}` for `EFunction`/`EArrow`/`EClass` (scan_side_effects.rs:925-933) and `to_null_or_undefined` returns `{ok, value:false}` for the same (815-826), so `BinLogicalOr` hits `return e_.left` at ~405 and `BinNullishCoalescing` at ~380 — both need the same `is_ano
Comment thread
claude[bot] marked this conversation as resolved.
Resolved
return Expr::join_with_comma(
Expr {
data: prefill::data::ZERO,
Expand All @@ -416,7 +433,10 @@
// "(1 && fn)()" => "fn()"
// "(1 && this.fn)" => "this.fn"
// "(1 && this.fn)()" => "(0, this.fn)()"
if is_call_target && e_.right.has_value_for_this_in_call() {
// "(1 && class {})" => "(0, class {})"
if (is_call_target && e_.right.has_value_for_this_in_call())
|| e_.right.is_anonymous_named()
{
return Expr::join_with_comma(
Expr {
data: prefill::data::ZERO,
Expand Down Expand Up @@ -712,7 +732,7 @@
if let Some(dot) = e_.left.data.e_dot() {
if let Some(obj) = dot.target.data.e_object() {
if obj.properties.len_u32() == 0 {
if dot.name != b"__proto__" {
if dot.name != b"__proto__" && !e_.right.is_anonymous_named() {
return e_.right;
}
}
Expand Down
31 changes: 27 additions & 4 deletions src/js_parser/visit/visit_expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1091,7 +1091,16 @@
if array.items.len_u32() == 1 && number.value() == 0.0 {
let inlined = *array.items.at(0);
if inlined.can_be_inlined_from_property_access() {
*e = inlined;
// [class {}][0] -> (0, class {})
*e = if inlined.is_anonymous_named() {
Expr {
data: prefill::data::ZERO,
loc: expr.loc,
}
.join_with_comma(inlined)
} else {
inlined
};
return;
}
}
Expand All @@ -1108,7 +1117,15 @@
return;
}
debug_assert!(inlined.can_be_inlined_from_property_access());
*e = inlined;
*e = if inlined.is_anonymous_named() {
Expr {
data: prefill::data::ZERO,
loc: expr.loc,
}
.join_with_comma(inlined)
} else {
inlined
};
return;
}
}
Expand Down Expand Up @@ -1491,13 +1508,16 @@
return;
}

// "(1 ? fn : 2)()" => "fn()"
// "(1 ? this.fn : 2)" => "this.fn"
// "(1 ? this.fn : 2)()" => "(0, this.fn)()"
if is_call_target && e_.yes.has_value_for_this_in_call() {
// "(1 ? class {} : 2)" => "(0, class {})"
if (is_call_target && e_.yes.has_value_for_this_in_call())
|| e_.yes.is_anonymous_named()
{
*e = p
.new_expr(E::Number::new(0.0), e_.test_.loc)
.join_with_comma(e_.yes);

Check failure on line 1520 in src/js_parser/visit/visit_expr.rs

View check run for this annotation

Claude / Claude Code Review

EIf CouldHaveSideEffects branch still exposes anonymous class/fn to NamedEvaluation

The `CouldHaveSideEffects` branch just above (lines 1504-1508, and 1535-1539 for the `no` arm) has the same bug: when `simplify_unused_expr` returns `None`, the `E::Missing` sentinel makes `join_with_comma` return `e_.yes`/`e_.no` bare, exposing the anonymous class to NamedEvaluation. `const A = [] ? class {} : 0` and `const C = typeof x ? class {} : 0` still give `.name === "A"`/`"C"`. Swap `E::Missing {}` for `prefill::data::ZERO` (or `E::Number::new(0.0)`) when `e_.yes`/`e_.no` `is_anonymous_
Comment thread
claude[bot] marked this conversation as resolved.
Resolved
return;
}

Expand All @@ -1522,7 +1542,10 @@
// "(1 ? fn : 2)()" => "fn()"
// "(1 ? this.fn : 2)" => "this.fn"
// "(1 ? this.fn : 2)()" => "(0, this.fn)()"
if is_call_target && e_.no.has_value_for_this_in_call() {
// "(0 ? 1 : class {})" => "(0, class {})"
if (is_call_target && e_.no.has_value_for_this_in_call())
|| e_.no.is_anonymous_named()
{
*e = p
.new_expr(E::Number::new(0.0), e_.test_.loc)
.join_with_comma(e_.no);
Expand Down
71 changes: 71 additions & 0 deletions test/bundler/transpiler/runtime-transpiler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -252,3 +252,74 @@ describe("unterminated string literals in large files", () => {
expect(exitCode).toBe(1);
});
});

describe("constant folding preserves NamedEvaluation semantics", () => {
// Folding a wrapper expression down to a bare anonymous class/function must
// not expose it to NamedEvaluation at the assignment/binding site.
test.concurrent("wrapped anonymous class/function .name stays empty", async () => {
const src = `
const A = (0, class {});
const B = true && class {};
const C = false || class {};
const D = null ?? class {};
const E = true ? class {} : 0;
const F = false ? 0 : class {};
const G = [class {}][0];
const fn = (0, function () {});
const ar = (0, () => {});
function param(P = (0, class {})) { return P.name; }
class Holder { field = (0, class {}); }
console.log(JSON.stringify({
A: A.name, B: B.name, C: C.name, D: D.name, E: E.name, F: F.name, G: G.name,
fn: fn.name, ar: ar.name, param: param(), field: new Holder().field.name,
}));
`;
await using proc = Bun.spawn({ cmd: [bunExe(), "-e", src], env: bunEnv, stderr: "pipe" });
const [stdout, stderr, exitCode] = await Promise.all([proc.stdout.text(), proc.stderr.text(), proc.exited]);
expect(stderr).toBe("");
expect(JSON.parse(stdout)).toEqual({
A: "",
B: "",
C: "",
D: "",
E: "",
F: "",
G: "",
fn: "",
ar: "",
param: "",
field: "",
});
expect(exitCode).toBe(0);
});

test.concurrent("({ prop: class {} }).prop keeps the property key as .name", async () => {
const src = `
const K = ({ prop: class {} }).prop;
const M = ({ prop: function () {} }).prop;
const N = ({ prop: () => {} }).prop;
const X = ({}).x ??= class {};
console.log(JSON.stringify({ K: K.name, M: M.name, N: N.name, X: X.name }));
`;
await using proc = Bun.spawn({ cmd: [bunExe(), "-e", src], env: bunEnv, stderr: "pipe" });
const [stdout, stderr, exitCode] = await Promise.all([proc.stdout.text(), proc.stderr.text(), proc.exited]);
expect(stderr).toBe("");
expect(JSON.parse(stdout)).toEqual({ K: "prop", M: "prop", N: "prop", X: "" });
expect(exitCode).toBe(0);
});

test.concurrent("direct anonymous class/function still gets the binding name", async () => {
const src = `
const A = class {};
const B = function () {};
const C = () => {};
const D = (0, class Named {});
console.log(JSON.stringify({ A: A.name, B: B.name, C: C.name, D: D.name }));
`;
await using proc = Bun.spawn({ cmd: [bunExe(), "-e", src], env: bunEnv, stderr: "pipe" });
const [stdout, stderr, exitCode] = await Promise.all([proc.stdout.text(), proc.stderr.text(), proc.exited]);
expect(stderr).toBe("");
expect(JSON.parse(stdout)).toEqual({ A: "A", B: "B", C: "C", D: "Named" });
expect(exitCode).toBe(0);
});
});
Loading