esbuild icon indicating copy to clipboard operation
esbuild copied to clipboard

DCE doesn't remove unreferenced `new Regexp`.

Open JeanMeche opened this issue 9 months ago • 1 comments

While inspecting my bundle output I just saw that an unreferenced regex wasn't removed by DCE. It looks like it only affects regex created by new RegExp(...).

Is there a reason for that ?

const regex1 = new RegExp('foo'); // KO
const regex2 = /foo/ // OK

Demo

JeanMeche avatar May 06 '24 01:05 JeanMeche

This is because esbuild considers throwing an exception to be a side effect, and new RegExp can potentially throw an exception (such as new RegExp('[')). The string literal is not checked for whether it's a valid regular expression or not.

evanw avatar May 06 '24 02:05 evanw