guybedford / es-module-lexer

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

Incorrect Documentation for `ImportType` #170

Closed joshunrau closed 3 months ago

joshunrau commented 3 months ago

In the README it says the following:

// Import type is provided by `t` value
// (1 for static, 2, for dynamic)
// Returns true
imports[2].t == 2;

This is also stated in lexer.d.ts:

export declare enum ImportType {
    /**
     * A normal static using any syntax variations
     *   import .. from 'module'
     */
    Static = 1,
    /**
     * A dynamic import expression `import(specifier)`
     * or `import(specifier, opts)`
     */
    Dynamic = 2,
    /**
     * An import.meta expression
     */
    ImportMeta = 3,
    /**
     * A source phase import
     *   import source x from 'module'
     */
    StaticSourcePhase = 4,
    /**
     * A dynamic source phase import
     *   import.source('module')
     */
    DynamicSourcePhase = 5
}

However, at runtime, I observe that Static and Dynamic are in fact reversed:


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

await init;

const input = `
  // dynamic
  const { a } = await import('a');
  // static
  import b from 'b';
  import { c } from 'c';
`;

const [imports] = parse(input);

console.log(imports);
// [
//   { n: 'a', t: 1, s: 43, e: 46, ss: 36, se: 47, d: 42, a: -1 },
//   { n: 'b', t: 2, s: 78, e: 79, ss: 63, se: 80, d: -1, a: -1 },
//   { n: 'c', t: 2, s: 103, e: 104, ss: 84, se: 105, d: -1, a: -1 }
// ];
guybedford commented 3 months ago

Great catch thank you, this is actually a bug.... added https://github.com/guybedford/es-module-lexer/pull/172.