guybedford / es-module-lexer

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

Parse dynamic imports with backticks #118

Closed bluwy closed 10 months ago

bluwy commented 2 years ago

When parsing a dynamic import with backticks, the s and e result can be used to get the raw string. However the e result seems to always be one index less of the closing parenthesis/comma, instead of the last backtick.

Reproduction

Stackblitz

Run node index.js in the terminal to log the first parsed import

Expected result

The log should be "`bla.js`"

Actual result

It gives "`bla.js`\n /* bar */\n" (trailing comments aren't stripped)

guybedford commented 2 years ago

@bluwy I agree it would be nice to add this trailing whitespace / comment adjustment to the output. This isn't so much a bug though since arbitrary dynamic import expressions just are considered as arbitrary code expressions and the handling of whitespace and comments is thus somewhat of an implementation detail. I will leave this open to track any improvement here as a feature.