guybedford / es-module-lexer

Low-overhead lexer dedicated to ES module parsing for fast analysis
MIT License
917 stars 48 forks source link

Incorrect regular expression detection regarding `break` and `continue` #123

Closed evanw closed 2 years ago

evanw commented 2 years ago

I was curious how regular expressions were handled and I noticed some cases with break and continue that aren't handled. Here's an example of one:

x: while (true) {
  if (foo) break
  /import("a")/.test(bar) || baz()
  if (foo) continue
  /import("b")/.test(bar) || baz()
  if (foo) break x
  /import("c")/.test(bar) || baz()
  if (foo) continue x
  /import("d")/.test(bar) || baz()
}

This library thinks that these are all imports.

guybedford commented 2 years ago

Thanks for posting. The lexer isn't perfect and there are some academic cases that can break it, in particular https://github.com/nodejs/cjs-module-lexer/issues/22 and https://github.com/nodejs/cjs-module-lexer/issues/20. We effectively do have a "real code" eventual cutoff which may not be the best choice for a perfect parser, although we do aim to support all real code. This is very much not an academic case though, fixed in https://github.com/guybedford/es-module-lexer/pull/124.