tc39 / proposal-source-phase-imports

Proposal to enable importing modules at the source phase
https://tc39.es/proposal-source-phase-imports/
MIT License
129 stars 9 forks source link

Consider cost of `import source from` ambiguity #42

Closed lucacasonato closed 1 year ago

lucacasonato commented 1 year ago

@waldemarhorwat pointed out and is concerned about the ambiguity of import source from ....

Here are all possible productions:

// regular import, default binding name is `source`
import source from "";
// source phase import, binding name is `from`
import source from from "";

And when taking into account import declarations with local imports:

// with import declarations, regular import, default binding name = source, import declaration = from
import source from from;

Regarding other ambiguity with import declarations local imports, phase imports, and ASI, also see #41.

Due to the disambiguation complexity (even though I think it is bounded to a three token lookahead), I suggest we ban import source from from in the context of source phase imports. This means that: import source from ... is unambiguously a regular import with a binding name of source. With import declarations this could be followed by either a identifier (for local imports), or a specifier string literal.

This reduces the parsing complexity back to a 1-token lookahead.

Thanks @littledan for the suggestion.

@waldemarhorwat, what do you think?