Commit 7a19927
implement typeof undefined minification optimization (#22278)
## Summary
Implements the `typeof undefined === 'u'` minification optimization from
esbuild in Bun's minifier, and fixes dead code elimination (DCE) for
typeof comparisons with string literals.
### Part 1: Minification Optimization
This optimization transforms:
- `typeof x === "undefined"` → `typeof x > "u"`
- `typeof x !== "undefined"` → `typeof x < "u"`
- `typeof x == "undefined"` → `typeof x > "u"`
- `typeof x != "undefined"` → `typeof x < "u"`
Also handles flipped operands (`"undefined" === typeof x`).
### Part 2: DCE Fix for Typeof Comparisons
Fixed dead code elimination to properly handle typeof comparisons with
strings (e.g., `typeof x <= 'u'`). These patterns can now be correctly
eliminated when they reference unbound identifiers that would throw
ReferenceErrors.
## Before/After
### Minification
Before:
```javascript
console.log(typeof x === "undefined");
```
After:
```javascript
console.log(typeof x > "u");
```
### Dead Code Elimination
Before (incorrectly kept):
```javascript
var REMOVE_1 = typeof x <= 'u' ? x : null;
```
After (correctly eliminated):
```javascript
// removed
```
## Implementation
### Minification
- Added `tryOptimizeTypeofUndefined` function in
`src/ast/visitBinaryExpression.zig`
- Handles all 4 equality operators and both operand orders
- Only optimizes when both sides match the expected pattern (typeof
expression + "undefined" string)
- Replaces "undefined" with "u" and changes operators to `>` (for
equality) or `<` (for inequality)
### DCE Improvements
- Extended `isSideEffectFreeUnboundIdentifierRef` in `src/ast/P.zig` to
handle comparison operators (`<`, `>`, `<=`, `>=`)
- Added comparison operators to `simplifyUnusedExpr` in
`src/ast/SideEffects.zig`
- Now correctly identifies when typeof comparisons guard against
undefined references
## Test Plan
✅ Added comprehensive test in `test/bundler/bundler_minify.test.ts` that
verifies:
- All 8 variations work correctly (4 operators × 2 operand orders)
- Cases that shouldn't be optimized are left unchanged
- Matches esbuild's behavior exactly using inline snapshots
✅ DCE test `dce/DCETypeOfCompareStringGuardCondition` now passes:
- Correctly eliminates dead code with typeof comparison patterns
- Maintains compatibility with esbuild's DCE behavior
🤖 Generated with [Claude Code](https://claude.ai/code)
Co-Authored-By: Claude <[email protected]>
---------
Co-authored-by: Claude Bot <[email protected]>
Co-authored-by: Claude <[email protected]>
Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
Co-authored-by: Jarred Sumner <[email protected]>
Co-authored-by: Dylan Conway <[email protected]>1 parent 63c4d8f commit 7a19927
File tree
13 files changed
+450
-76
lines changed- src
- ast
- bun.js
- test/bundler
- esbuild
13 files changed
+450
-76
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
98 | 98 | | |
99 | 99 | | |
100 | 100 | | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
101 | 138 | | |
102 | 139 | | |
103 | 140 | | |
| |||
940 | 977 | | |
941 | 978 | | |
942 | 979 | | |
| 980 | + | |
| 981 | + | |
| 982 | + | |
| 983 | + | |
| 984 | + | |
| 985 | + | |
| 986 | + | |
| 987 | + | |
| 988 | + | |
| 989 | + | |
| 990 | + | |
| 991 | + | |
| 992 | + | |
| 993 | + | |
| 994 | + | |
| 995 | + | |
| 996 | + | |
| 997 | + | |
| 998 | + | |
| 999 | + | |
| 1000 | + | |
| 1001 | + | |
| 1002 | + | |
| 1003 | + | |
943 | 1004 | | |
944 | 1005 | | |
945 | 1006 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
647 | 647 | | |
648 | 648 | | |
649 | 649 | | |
| 650 | + | |
| 651 | + | |
| 652 | + | |
| 653 | + | |
| 654 | + | |
| 655 | + | |
| 656 | + | |
| 657 | + | |
| 658 | + | |
| 659 | + | |
| 660 | + | |
| 661 | + | |
| 662 | + | |
| 663 | + | |
| 664 | + | |
| 665 | + | |
| 666 | + | |
| 667 | + | |
| 668 | + | |
| 669 | + | |
| 670 | + | |
| 671 | + | |
| 672 | + | |
650 | 673 | | |
651 | 674 | | |
652 | 675 | | |
653 | 676 | | |
654 | 677 | | |
655 | 678 | | |
656 | 679 | | |
| 680 | + | |
| 681 | + | |
| 682 | + | |
| 683 | + | |
| 684 | + | |
| 685 | + | |
| 686 | + | |
| 687 | + | |
| 688 | + | |
| 689 | + | |
| 690 | + | |
| 691 | + | |
| 692 | + | |
| 693 | + | |
657 | 694 | | |
658 | 695 | | |
659 | 696 | | |
| |||
1407 | 1444 | | |
1408 | 1445 | | |
1409 | 1446 | | |
1410 | | - | |
| 1447 | + | |
| 1448 | + | |
| 1449 | + | |
| 1450 | + | |
| 1451 | + | |
| 1452 | + | |
| 1453 | + | |
1411 | 1454 | | |
1412 | 1455 | | |
1413 | 1456 | | |
1414 | | - | |
| 1457 | + | |
1415 | 1458 | | |
1416 | 1459 | | |
1417 | 1460 | | |
| |||
1873 | 1916 | | |
1874 | 1917 | | |
1875 | 1918 | | |
1876 | | - | |
1877 | | - | |
1878 | | - | |
1879 | | - | |
1880 | | - | |
1881 | | - | |
1882 | | - | |
1883 | | - | |
1884 | | - | |
1885 | | - | |
1886 | | - | |
1887 | | - | |
1888 | | - | |
1889 | | - | |
1890 | | - | |
1891 | | - | |
1892 | | - | |
1893 | | - | |
1894 | | - | |
1895 | | - | |
1896 | | - | |
1897 | | - | |
1898 | | - | |
1899 | | - | |
1900 | | - | |
| 1919 | + | |
| 1920 | + | |
| 1921 | + | |
| 1922 | + | |
| 1923 | + | |
| 1924 | + | |
| 1925 | + | |
| 1926 | + | |
| 1927 | + | |
| 1928 | + | |
1901 | 1929 | | |
1902 | | - | |
1903 | | - | |
1904 | | - | |
1905 | | - | |
| 1930 | + | |
| 1931 | + | |
1906 | 1932 | | |
1907 | 1933 | | |
1908 | 1934 | | |
| |||
1912 | 1938 | | |
1913 | 1939 | | |
1914 | 1940 | | |
1915 | | - | |
| 1941 | + | |
1916 | 1942 | | |
1917 | 1943 | | |
1918 | 1944 | | |
1919 | 1945 | | |
1920 | 1946 | | |
1921 | 1947 | | |
1922 | 1948 | | |
1923 | | - | |
1924 | | - | |
1925 | | - | |
1926 | | - | |
1927 | | - | |
1928 | | - | |
1929 | | - | |
1930 | | - | |
1931 | | - | |
1932 | | - | |
1933 | | - | |
1934 | | - | |
| 1949 | + | |
| 1950 | + | |
| 1951 | + | |
| 1952 | + | |
| 1953 | + | |
| 1954 | + | |
| 1955 | + | |
| 1956 | + | |
| 1957 | + | |
| 1958 | + | |
1935 | 1959 | | |
1936 | 1960 | | |
1937 | | - | |
| 1961 | + | |
1938 | 1962 | | |
1939 | 1963 | | |
1940 | 1964 | | |
| |||
1946 | 1970 | | |
1947 | 1971 | | |
1948 | 1972 | | |
1949 | | - | |
| 1973 | + | |
1950 | 1974 | | |
1951 | 1975 | | |
1952 | 1976 | | |
| |||
1968 | 1992 | | |
1969 | 1993 | | |
1970 | 1994 | | |
1971 | | - | |
| 1995 | + | |
1972 | 1996 | | |
1973 | 1997 | | |
1974 | 1998 | | |
| |||
1981 | 2005 | | |
1982 | 2006 | | |
1983 | 2007 | | |
1984 | | - | |
| 2008 | + | |
1985 | 2009 | | |
1986 | 2010 | | |
1987 | 2011 | | |
1988 | 2012 | | |
1989 | | - | |
| 2013 | + | |
1990 | 2014 | | |
1991 | 2015 | | |
1992 | 2016 | | |
1993 | 2017 | | |
1994 | | - | |
| 2018 | + | |
1995 | 2019 | | |
1996 | 2020 | | |
1997 | 2021 | | |
1998 | 2022 | | |
1999 | | - | |
| 2023 | + | |
2000 | 2024 | | |
2001 | 2025 | | |
2002 | 2026 | | |
2003 | 2027 | | |
2004 | | - | |
| 2028 | + | |
2005 | 2029 | | |
2006 | 2030 | | |
2007 | 2031 | | |
2008 | 2032 | | |
2009 | 2033 | | |
2010 | | - | |
| 2034 | + | |
2011 | 2035 | | |
2012 | 2036 | | |
2013 | 2037 | | |
| |||
2016 | 2040 | | |
2017 | 2041 | | |
2018 | 2042 | | |
2019 | | - | |
| 2043 | + | |
2020 | 2044 | | |
2021 | 2045 | | |
2022 | 2046 | | |
2023 | | - | |
| 2047 | + | |
2024 | 2048 | | |
2025 | 2049 | | |
2026 | 2050 | | |
| |||
2054 | 2078 | | |
2055 | 2079 | | |
2056 | 2080 | | |
2057 | | - | |
| 2081 | + | |
2058 | 2082 | | |
2059 | 2083 | | |
2060 | 2084 | | |
| |||
2294 | 2318 | | |
2295 | 2319 | | |
2296 | 2320 | | |
| 2321 | + | |
2297 | 2322 | | |
2298 | 2323 | | |
2299 | 2324 | | |
| |||
2506 | 2531 | | |
2507 | 2532 | | |
2508 | 2533 | | |
| 2534 | + | |
2509 | 2535 | | |
2510 | 2536 | | |
2511 | 2537 | | |
| |||
2537 | 2563 | | |
2538 | 2564 | | |
2539 | 2565 | | |
2540 | | - | |
| 2566 | + | |
2541 | 2567 | | |
2542 | 2568 | | |
2543 | 2569 | | |
| |||
2860 | 2886 | | |
2861 | 2887 | | |
2862 | 2888 | | |
| 2889 | + | |
| 2890 | + | |
| 2891 | + | |
| 2892 | + | |
| 2893 | + | |
| 2894 | + | |
| 2895 | + | |
| 2896 | + | |
| 2897 | + | |
| 2898 | + | |
| 2899 | + | |
2863 | 2900 | | |
2864 | 2901 | | |
2865 | 2902 | | |
| |||
0 commit comments