babel / babylon

PSA: moved into babel/babel as @babel/parser -->

Home Page:https://github.com/babel/babel/tree/master/packages/babel-parser

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Typescript: ExportNamedDeclaration raises SyntaxError when exportExtensions plugin enabled

traviskaufman opened this issue · comments

When parsing typescript code with the exportExtensions plugin enabled, code that is otherwise parseable fails to parse. My suspicion given the parsing logic behind export statements is due to the fact that this is because the exportExtensions plugin expects a specific token pattern, which is not the case when parsing an ExportNamedDeclaration statement.

Input Code

// input.ts
export declare interface Foo {
  a: number;
}

ASTExplorer link

Babylon/Babel Configuration

// Options given to babylon.parse
{
  sourceType: 'module',
  plugins: [
    'typescript',
    'classProperties',
    'objectRestSpread',
    'decorators',
    'exportExtensions',
    'asyncGenerators',
  ],
}

Expected Behavior

The code should parse successfully, as this is valid typescript code, and the typescript plugin has been enabled.

Current Behavior

The code fails to parse, failing with a SyntaxError: Unexpected Token at the loc directly after the declare token.

Additional Notes

Taking a look at the stack trace, it seems like the code that causes the parse failure occurs at src/parser/statement.js:1296

Note that this code is executed iff the exportExtensions plugin is enabled. My naive hunch is it seems like what the code is saying is:

"First I expect an export token, followed by an export specifier, followed immediately by a from token"

Perhaps it's getting confused when it encounters declare as it expects a symbolic identifier?

Or perhaps there may be a parsing issue within the isDefaultExport() logic(?)

Note that the code parses successfully when exportExtensions is not enabled.

Your Environment

software version
Babylon 7.0.0-beta.27
node v8.1.3
npm 5.3.0
Operating System Linux (I unfortunately am not at liberty to give away any more information than this)
commented

Hey @traviskaufman! We really appreciate you taking the time to report an issue. The collaborators
on this project attempt to help as many people as possible, but we're a limited number of volunteers,
so it's possible this won't be addressed swiftly.

If you need any help, or just have general Babel or JavaScript questions, we have a vibrant Slack
community that typically always has someone willing to help. You can sign-up here
for an invite.