You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Browse filesBrowse the repository at this point in the historyBrowse files
authored
fix: don't crash on statically-evaluated expressions that throw (BigInt mixing) (#210)
## Problem
The static evaluator (`src/utils/static-eval.js`) applies JS operators
directly to known values, but some of those operations can throw at
evaluation time:
- **BigInt mixing**: `null + 1n` → `TypeError: Cannot mix BigInt and
other types, use explicit conversions`
- **Unary `+` on a BigInt**: `+1n` → `TypeError: Cannot convert a BigInt
value to a number`
The loader statically evaluates the RHS of every top-level-bound
assignment (to track known bindings), so real-world code hits this.
dd-trace 5's
[`appsec/downstream_requests.js`](https://github.com/DataDog/dd-trace-js/blob/master/packages/dd-trace/src/appsec/downstream_requests.js)
does:
```js
let globalRequestCounter
function enable (_config) {
globalRequestCounter = 0n
}
function disable () {
globalRequestCounter = null // <- registers null as the known binding value
}
function shouldSampleBody (req, outgoingUrl) {
// evaluator computes `null + 1n` here and throws, crashing the build
globalRequestCounter = (globalRequestCounter + 1n) & UINT64_MAX
...
}
```
The uncaught TypeError kills the whole compilation — e.g. `ncc build` of
anything depending on `dd-trace@5` dies with:
```
TypeError: Cannot mix BigInt and other types, use explicit conversions
at Object.BinaryExpression (.../relocate-loader.js.cache.js:...)
at walk (...)
at computePureStaticValue (...)
```
This blocks bumping dd-trace 4 → 5 in any ncc-bundled project (every ncc
version is affected since they all bundle this loader).
## Fix
Wrap the operator application in `BinaryExpression` and
`UnaryExpression` in try/catch, so a throwing expression is treated as
**not statically computable** (the evaluator's existing "unknown value"
semantics). `CallExpression` and `NewExpression` already handle
evaluation errors exactly this way, so this follows established
convention in the file. The diff is best viewed with whitespace ignored.
## Test
Added `test/unit/static-eval-bigint` reproducing the dd-trace pattern
(known-`null` binding + `1n`, BigInt consts, and unary `+` on a known
BigInt). Without the fix it crashes the build with the errors above;
with the fix the module passes through untouched.
`npx jest test/index.test.js`: 85/85 pass. (`test/project.test.js` fails
identically on unmodified `main` in my environment — pre-existing,
unrelated.)
🤖 Generated with [Claude Code](https://claude.com/claude-code)
## Related
- Related to vercel/ncc#1307
- Related to
#208
Co-authored-by: vercel[bot] <35613825+vercel[bot]@users.noreply.github.com>
Co-authored-by: Claude <noreply@anthropic.com>
0 commit comments