guybedford / es-module-lexer

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

Dynamic import with extra parenthesis not captured #156

Closed privatenumber closed 10 months ago

privatenumber commented 11 months ago

When the dynamic import argument is wrapped in a parenthesis, the statement range is not correctly captured:

import { init, parse } from 'es-module-lexer';

await init;

const source = 'import(("asdf"))';

const [imports] = parse(source, 'optional-sourcename');
const [imp] = imports;
const dynamicImportStatement = source.slice(imp.ss, imp.se);
// import(("asdf")
// Expected to be: import(("asdf"))

if (dynamicImportStatement !== source) {
  console.log('Full statement was not captured:', dynamicImportStatement);
}

Reproduction: https://stackblitz.com/edit/stackblitz-starters-zzxwkp?file=index.mjs

guybedford commented 11 months ago

Hey @privatenumber, yes this is by design - we don't parse arbitrary expressions, so from the perspective of the lexer, ('string') is an expression, while 'string' is a direct static string.

In the dynamic expression cases, you always do just need to apply a custom parser, but at least there is an expression range you can apply it to.

privatenumber commented 11 months ago

The problem is that the expression range is incorrect, which results in invalid JS.

e.g. a parser cannot correctly parse import(("asdf") because it's missing the closing parenthesis ).

guybedford commented 11 months ago

Okay, right, if it’s the wrong offset for a slice function then that’s a buf we should fix.

On Sat, Sep 9, 2023 at 19:04 Hiroki Osame @.***> wrote:

The problem is that the expression range is incorrect, which results in invalid JS.

e.g. a parser cannot correctly parse import(("asdf") because it's missing the closing parenthesis ).

— Reply to this email directly, view it on GitHub https://github.com/guybedford/es-module-lexer/issues/156#issuecomment-1712685926, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAESFSQRNMFG6OW7RIO4FBDXZUN3LANCNFSM6AAAAAA4A2CIP4 . You are receiving this because you commented.Message ID: @.***>

guybedford commented 10 months ago

Released in 1.3.1.

privatenumber commented 10 months ago

Thank you @guybedford !