oxc icon indicating copy to clipboard operation
oxc copied to clipboard

semantics: references created by direct eval in non-strict mode is not respected

Open sapphi-red opened this issue 11 months ago • 3 comments

Currently semantic does not track the existence of direct eval. is_global_reference may return true even if that reference refers to a variable declared by direct eval (the declaration is only leaked outside in non-strict mode). https://github.com/oxc-project/oxc/blob/e4d66e46360dbdec289e40319f8b2a335b25c099/crates/oxc_semantic/src/symbol.rs#L342-L346

This leads to minification errors for codes like:

function foo() {
  eval('var Object = class { constructor() { console.log("Constructor") } }')
  console.log(new Object())
}

foo()

playground

I'd say just never use direct eval 😅 But leaving a note for reference. (This is not something I met with real world code)

sapphi-red avatar Jan 09 '25 04:01 sapphi-red

All logic related to eval has not been implemented yet. We need to either implement them or warn about it. Note: esbuild bails minification and mangling for all parent scopes containing eval.

Boshen avatar Jan 09 '25 04:01 Boshen

Direct eval disables the mangler.

https://github.com/oxc-project/oxc/blob/0b08159ee76120159694b39a159d9eb6272279aa/crates/oxc_mangler/src/lib.rs#L195-L198

Boshen avatar Feb 20 '25 05:02 Boshen

It's not only the mangler. We need to disable all the compression that uses is_global_reference or relies on any information related to variables / functions. The reproduction in the OP is still broken.

sapphi-red avatar Feb 20 '25 05:02 sapphi-red