es-module-lexer icon indicating copy to clipboard operation
es-module-lexer copied to clipboard

Parse error in `for` statement when identifier `of` is divided

Open quiqueg opened this issue 7 months ago • 1 comments

Similar to https://github.com/guybedford/es-module-lexer/issues/173 and https://github.com/guybedford/es-module-lexer/issues/177.

Steps to reproduce

  1. for statement which contains:
    • of as an identifier on the left side of a binary expression, whose operator is /

Minimal source code

for (i = of / 2;;) break
for (; of / 2;) break
for (;; of / 2) break

Also affects for..of and for..in statements

However, these are both pathological examples: while they are valid JavaScript, of / 2 would have to produce an iterable for these to execute without error.

for (i of of / 2) break
for (i in of / 2) break

Full repro

import { init, parse } from 'es-module-lexer';
(async () => {
  await init;
  parse('for (i = of / 2;;) {}');
})();
Error: Parse error @:1:22
    at parse (file:///app/node_modules/.pnpm/[email protected]/node_modules/es-module-lexer/dist/lexer.js:2:682)
    at file:///app/index.mjs:4:3 {
  idx: 21
}

Affected versions

Reproducible in versions 1.5.1 through 1.7.0.


Like https://github.com/guybedford/es-module-lexer/issues/177, we unfortunately have this code in our minified Vite output and so our CI/CD broke. I somehow managed to find it in our 8MB minified JS file 🫠

quiqueg avatar Jun 01 '25 09:06 quiqueg

Thanks for clearing the clear reproduction on this one. This edge case gets to the heart of the projects assumptions, and fixing it is a heuristic exercise unfortunately. The question is then just how well we can patch these edge cases.

I've put together a fix for this in https://github.com/guybedford/es-module-lexer/pull/195, but before releasing that I'd really value some more eyes on it to determine if there are more edge cases I'm missing here.

guybedford avatar Dec 07 '25 08:12 guybedford