Skip to content
Closed
Show file tree
Hide file tree
Changes from 2 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
41 changes: 41 additions & 0 deletions src/js_parser/visit/visit_expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -891,6 +891,43 @@
let is_call_target = matches!(p.call_target, Data::EIndex(ct) if core::ptr::eq(&raw const *e_, &raw const *ct));
let is_delete_target = matches!(p.delete_target, Data::EIndex(dt) if core::ptr::eq(&raw const *e_, &raw const *dt));

// Check defines for a computed string-literal property access, e.g.
// `process.env["NODE_ENV"]`. This mirrors `e_dot`: it must not depend on
// minification, because the `a["b"]` => `a.b` rewrite below is gated on
// `minify_syntax` and cannot be the only route into the define lookup.
// `E::Index` has no side-effect flags, so only substitution and the
// `--drop` flag apply here.
let defines = p.define;
if let Some(mut s) = e_.index.data.e_string() {
if s.is_utf8() {
if let Some(parts) = defines.dots.get(s.slice(p.arena)) {
for define in parts.as_slice() {
if p.is_dot_define_match(expr, &define.parts) {
if in_.assign_target == js_ast::AssignTarget::None {
if !define.data.valueless() {
*e = p.value_for_define(
expr.loc,
in_.assign_target,
is_delete_target,
&define.data,
);
return;
}

if define.data.method_call_must_be_replaced_with_undefined()
&& in_
.property_access_for_method_call_maybe_should_replace_with_undefined
{
p.method_call_must_be_replaced_with_undefined = true;
}
}
break;
}
}
}
}
}

// "a['b']" => "a.b"
if p.options.features.minify_syntax {
if let Some(mut s) = e_.index.data.e_string() {
Expand Down Expand Up @@ -921,13 +958,17 @@
}

let has_chain_parent = e_.optional_chain == Some(js_ast::OptionalChain::Continuation);
// Propagate the `--drop` flag through the target like `e_dot` does, so
// `--drop=a.b` also removes `a.b["c"]()`.
p.visit_expr_in_out(
&mut e_.target,
ExprIn {
has_chain_parent,
property_access_for_method_call_maybe_should_replace_with_undefined: in_
.property_access_for_method_call_maybe_should_replace_with_undefined,
..Default::default()
},
);

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

View check run for this annotation

Claude / Claude Code Review

--drop flag leaks into nested call inside computed index

Propagating `property_access_for_method_call_maybe_should_replace_with_undefined` into the `e_index` target visit lets the target set the parser-global `p.method_call_must_be_replaced_with_undefined`, but `e_index` then visits the *index* expression at line 1053 with that flag still live. If the index contains a call — e.g. `console[getLevel()]("msg")` with `--drop=console` — the inner `e_call` consumes the flag and replaces itself with `undefined`, while the outer call survives, emitting `conso
Comment thread
robobun marked this conversation as resolved.
Resolved

match e_.index.data {
Data::EPrivateIdentifier(mut private) => {
Expand Down
30 changes: 30 additions & 0 deletions test/bundler/bundler_drop.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,36 @@ describe("bundler", () => {
run: { stdout: "undefined" },
drop: ["Bun"],
});
// `--drop` must also match computed string-literal property accesses.
itBundled("drop/ComputedFunctionCall", {
files: {
"/a.js": `console["log"]("hello");`,
},
run: { stdout: "" },
drop: ["console"],
backend: "api",
});
itBundled("drop/ComputedBecomesUndefined", {
files: {
"/a.js": `console.log(Bun["inspect"]["table"]());`,
},
run: { stdout: "undefined" },
drop: ["Bun.inspect.table"],
});
itBundled("drop/ComputedBecomesUndefinedNested1", {
files: {
"/a.js": `console.log(Bun["inspect"]["table"]());`,
},
run: { stdout: "undefined" },
drop: ["Bun.inspect"],
});
itBundled("drop/ComputedBecomesUndefinedNested2", {
files: {
"/a.js": `console.log(Bun["inspect"]["table"]());`,
},
run: { stdout: "undefined" },
drop: ["Bun"],
});
itBundled("drop/AssignTarget", {
files: {
"/a.js": `console.log(
Expand Down
17 changes: 17 additions & 0 deletions test/bundler/bundler_edgecase.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,23 @@ describe("bundler", () => {
NODE_ENV: "development",
},
});
// The bracket spelling must inline the same way as the dot spelling, and
// must not depend on --minify folding `a["b"]` into `a.b` first.
itBundled("edgecase/NodeEnvComputedPropertyAccess", {
files: {
"/entry.js": /* js */ `
capture(process.env.NODE_ENV);
capture(process.env["NODE_ENV"]);
capture(process["env"].NODE_ENV);
capture(process["env"]["NODE_ENV"]);
`,
},
target: "browser",
capture: ['"development"', '"development"', '"development"', '"development"'],
env: {
NODE_ENV: "development",
},
});

itBundled("edgecase/StarExternal", {
files: {
Expand Down
18 changes: 18 additions & 0 deletions test/bundler/transpiler/transpiler.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2530,6 +2530,24 @@ console.log(resolve.length)
expectPrinted_("Math.log('hi')", 'console.error("hi")');
});

// A computed string-literal member access must be substituted the same way
// as the dot form, without requiring --minify.
it("define matches computed string-literal member access", () => {
expectPrinted_(`export default process.env.NODE_ENV;`, `export default "development"`);
expectPrinted_(`export default process.env["NODE_ENV"];`, `export default "development"`);
expectPrinted_(`export default process["env"].NODE_ENV;`, `export default "development"`);
expectPrinted_(`export default process["env"]["NODE_ENV"];`, `export default "development"`);
expectPrinted_(`hello["earth"]('hi')`, 'hello.mars("hi")');
expectPrinted_(`Math["log"]('hi')`, 'console.error("hi")');

// Assignment targets are never substituted, same as the dot form.
expectPrinted_(`process.env["NODE_ENV"] = 1`, `process.env["NODE_ENV"] = 1`);

// Non-literal and non-matching computed keys are left alone.
expectPrinted_(`export default process.env[NODE_ENV];`, `export default process.env[NODE_ENV]`);
expectPrinted_(`export default process.env["OTHER"];`, `export default process.env["OTHER"]`);
});

it("jsx symbol should work", () => {
expectBunPrinted_(`var x = jsx; export default x;`, "var x = jsx;\nexport default x");
});
Expand Down
Loading