mirror of
https://github.com/oven-sh/bun
synced 2026-02-14 21:01:52 +00:00
Remove redundant typeof pattern checking in SideEffects.toBoolean
The checkTypeofPattern function and all typeof pattern checking in toBoolean was redundant dead code. It was never reached because typeof expressions are already evaluated to string literals in visitExpr.zig. For example: - typeof Bun → "object" or "undefined" (already evaluated) - typeof Bun === "undefined" → "object" === "undefined" (simple string comparison) By the time expressions reach toBoolean, there are no typeof operators left to check - they've all been converted to string literals. The proper solution in visitExpr.zig handles everything correctly. This removes ~100 lines of unreachable code and simplifies the codebase. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
@@ -731,52 +731,6 @@ pub const SideEffects = enum(u1) {
|
||||
return Result{ .ok = false, .value = false, .side_effects = SideEffects.could_have_side_effects };
|
||||
}
|
||||
|
||||
/// Helper function to check typeof patterns for DCE
|
||||
fn checkTypeofPattern(p: anytype, typeof_expr: Expr, compare_op: Op.Code) ?bool {
|
||||
|
||||
// Check for identifier (e.g., typeof Bun)
|
||||
if (typeof_expr.data == .e_identifier) {
|
||||
const ident = typeof_expr.data.e_identifier;
|
||||
if (p.define.forIdentifier(p.loadNameFromRef(ident.ref))) |define| {
|
||||
// Check if this is an is_truthy define (e.g., Bun is truthy)
|
||||
if (define.is_truthy()) {
|
||||
// typeof truthy_value !== "undefined" -> true
|
||||
// typeof truthy_value === "undefined" -> false
|
||||
const is_not_equal = compare_op == .bin_strict_ne or compare_op == .bin_loose_ne;
|
||||
return is_not_equal;
|
||||
}
|
||||
// Check if the define value is undefined (e.g., window is undefined)
|
||||
if (define.value == .e_undefined) {
|
||||
// typeof undefined_value === "undefined" -> true
|
||||
// typeof undefined_value !== "undefined" -> false
|
||||
const is_equal = compare_op == .bin_strict_eq or compare_op == .bin_loose_eq;
|
||||
return is_equal;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Check for dot expression (e.g., typeof globalThis.Bun)
|
||||
if (typeof_expr.data == .e_dot) {
|
||||
const dot = typeof_expr.data.e_dot;
|
||||
if (p.define.dots.get(dot.name)) |parts| {
|
||||
for (parts) |*define| {
|
||||
if (p.isDotDefineMatch(typeof_expr, define.parts)) {
|
||||
if (define.data.is_truthy()) {
|
||||
// typeof truthy_value !== "undefined" -> true
|
||||
// typeof truthy_value === "undefined" -> false
|
||||
const is_not_equal = compare_op == .bin_strict_ne or compare_op == .bin_loose_ne;
|
||||
return is_not_equal;
|
||||
}
|
||||
// Note: We don't check for e_undefined for dot expressions currently
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
pub fn toBoolean(p: anytype, exp: Expr.Data) Result {
|
||||
// Only do this check once.
|
||||
if (!p.options.features.dead_code_elimination) {
|
||||
@@ -811,39 +765,9 @@ pub const SideEffects = enum(u1) {
|
||||
}
|
||||
}
|
||||
|
||||
// Check for typeof patterns in binary expressions
|
||||
if (exp == .e_binary) {
|
||||
const e_ = exp.e_binary;
|
||||
switch (e_.op) {
|
||||
.bin_strict_eq, .bin_strict_ne, .bin_loose_eq, .bin_loose_ne => {
|
||||
// Check for: typeof X === "undefined" or typeof X !== "undefined"
|
||||
if (e_.left.data == .e_unary) {
|
||||
const unary = e_.left.data.e_unary;
|
||||
if (unary.op == .un_typeof and e_.right.data == .e_string) {
|
||||
const str = e_.right.data.e_string;
|
||||
if (str.eqlComptime("undefined")) {
|
||||
if (checkTypeofPattern(p, unary.value, e_.op)) |result| {
|
||||
return Result{ .ok = true, .value = result, .side_effects = .could_have_side_effects };
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
// Check for: "undefined" === typeof X or "undefined" !== typeof X
|
||||
if (e_.right.data == .e_unary) {
|
||||
const unary = e_.right.data.e_unary;
|
||||
if (unary.op == .un_typeof and e_.left.data == .e_string) {
|
||||
const str = e_.left.data.e_string;
|
||||
if (str.eqlComptime("undefined")) {
|
||||
if (checkTypeofPattern(p, unary.value, e_.op)) |result| {
|
||||
return Result{ .ok = true, .value = result, .side_effects = .could_have_side_effects };
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
else => {},
|
||||
}
|
||||
}
|
||||
// No need to check for typeof patterns here anymore!
|
||||
// typeof expressions are already evaluated to string literals in visitExpr.zig
|
||||
// e.g., typeof Bun becomes "object" or "undefined" before reaching here
|
||||
|
||||
return toBooleanWithoutDCECheck(exp);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user