guybedford / es-module-lexer

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

Expose string start/end for dynamic imports #56

Closed LarsDenBakker closed 2 years ago

LarsDenBakker commented 4 years ago

Dynamic imports may contain bare imports. Using the lexer we get the start and end of the entire dynamic import body, but it would be useful to know where the string actually starts and ends. It's easy to handle whitespace by users, but especially comments are tricky to parse and I think the lexer already does this parsing I think.

For example:

import(
  'my-package'
);
import(
  /* foo */
  'my-package'
);
guybedford commented 4 years ago

We could certainly use the s and e values for this, and then have the ss and se be what the current start and end are.

I'd be good with making that as a breaking change. It just takes one call to commentWhitespace() to get the starting position. The only issue is that because dynamic imports can be arbitrary expressions, we don't actually know the end of the expression, but we could possible trace the expression end for strings only, then have se as -1 in the "dynamic" cases.

If you'd be interested in making a PR I'm all for it.

LarsDenBakker commented 4 years ago

I'm interested in making a PR, but I haven't been able to get the C environment working on my mac 😅

For expressions, we do try to find the end of the first string in the case of bare imports:

import('my-package' + subPath);
import`'my-package${subPath}`);

and then we just resolve the first part.

guybedford commented 4 years ago

I'm interested in making a PR, but I haven't been able to get the C environment working on my mac 😅

Let me know if I can help with this at all.

Best-effort base string parsing sounds pretty sweet actually, nice suggestion!