guybedford / es-module-lexer

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

Nested dynamic import tracking #98

Closed mafintosh closed 2 years ago

mafintosh commented 2 years ago

Found this edge case:

await import(await import('foo'))

Basically if you parse that, all the start offsets are correct but the end ones for the first import is a big negative number.

guybedford commented 2 years ago

Thanks @mafintosh yes nested dynamic imports are not supported! We only analyze based on cur_dynamic_import in https://github.com/guybedford/es-module-lexer/blob/main/src/lexer.c#L107, when this should really be a stack to support arbitrary nesting.

Did you have real code that this was affecting?

mafintosh commented 2 years ago

Only for a test case that our transforms work for any js code, but I was able to work around it by only knowing the where the import statement was, which does work atm, so not a big issue.

Only using the import start index massively simplified my transforms so it was a good challenge 😄

guybedford commented 2 years ago

Okay, I'm going to leave this open to track a real-world need for it since it's a bit of work for an edge case, but let me know if it is affecting you in any material way too!

mafintosh commented 2 years ago

For sure, as long as the start index is correct, I'd much prefer MOAR PERF vs fixing this.

guybedford commented 2 years ago

Btw the big negative number for the end index is actually uninitialized memory... the great thing about Wasm is not having to worry about this stuff :P

guybedford commented 2 years ago

This feature is now supported as of https://github.com/guybedford/es-module-lexer/pull/102 using a dynamic import stack for tracking.

mafintosh commented 2 years ago

Wow, amazing, thanks!