JamesHenry / typescript-estree

:sparkles: A parser that converts TypeScript source code into an ESTree-compatible form
https://jameshenry.blog
Other
84 stars 13 forks source link

sourceType is wrong if `import`/`export` exists in module blocks #31

Closed mysticatea closed 5 years ago

mysticatea commented 6 years ago

What version of TypeScript are you using?

What version of typescript-estree are you using?

What code were you trying to parse?

declare module "*" {
    const a = 1
    export { a }
}

What did you expect to happen?

The sourceType property of Program node is "module" because it contains ES2015 module syntax as an ambient module declaration.

What actually happened?

The sourceType property of Program node was "script".

{
  "type": "Program",
  "body": [
    {
      "type": "TSModuleDeclaration",
      "id": {
        "type": "Literal",
        "raw": "\"*\"",
        "value": "*"
      },
      "body": {
        "type": "TSModuleBlock",
        "body": [
          {
            "type": "VariableDeclaration",
            "declarations": [
              {
                "type": "VariableDeclarator",
                "id": {
                  "type": "Identifier",
                  "name": "a"
                },
                "init": {
                  "type": "Literal",
                  "value": 1,
                  "raw": "1"
                }
              }
            ],
            "kind": "const"
          },
          {
            "type": "ExportNamedDeclaration",
            "source": null,
            "specifiers": [
              {
                "type": "ExportSpecifier",
                "local": {
                  "type": "Identifier",
                  "name": "a"
                },
                "exported": {
                  "type": "Identifier",
                  "name": "a"
                }
              }
            ],
            "declaration": null
          }
        ]
      },
      "declare": true
    }
  ],
  "sourceType": "script"
}
Jessidhia commented 6 years ago

That is actually correct. This module is a global script. It is only the module it declares that is a module.

If this module was a module (had a top level import/export), it would actually change the meaning of declare module to be a module augmentation instead of declaration. Module augmentations can only add types to existing modules instead of declaring new modules.

JamesHenry commented 5 years ago

@mysticatea Hopefully @Kovensky's response is clear, please feel free to reopen if there is more to discuss