Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Babel's TypeScript support doesn't match the compiler w.r.t. inline declarations of in-scope identifiers #10353

Open
ELLIOTTCABLE opened this issue Aug 17, 2019 · 3 comments

Comments

@ELLIOTTCABLE
Copy link

commented Aug 17, 2019

Bug Report

Current Behavior
When encountering a declare statement for an identifier that's already in scope, Babel throws up. The upstream TypeScript compiler understands this syntax.

{ SyntaxError: /Users/ec/Sync/Code/excmd/src/interface.ts: Identifier 'Something'
    has already been declared (3:14)

   1 | import Something from './somewhere.js'
   2 | 
>  3 | declare class Something {
     |               ^
   4 |    // ...
   5 | }
    at Object.raise (/Users/ec/Sync/Code/excmd/node_modules/@babel/parser/lib/index.js:6325:17)
    at TypeScriptScopeHandler.checkRedeclarationInScope (/Users/ec/Sync/Code/excmd/node_modules/@babel/parser/lib/index.js:3759:12)
    at TypeScriptScopeHandler.declareName (/Users/ec/Sync/Code/excmd/node_modules/@babel/parser/lib/index.js:3725:12)
    at TypeScriptScopeHandler.declareName (/Users/ec/Sync/Code/excmd/node_modules/@babel/parser/lib/index.js:3835:11)
    at Object.checkLVal (/Users/ec/Sync/Code/excmd/node_modules/@babel/parser/lib/index.js:8021:22)
    at Object.checkLVal (/Users/ec/Sync/Code/excmd/node_modules/@babel/parser/lib/index.js:5727:15)
    at Object.parseClassId (/Users/ec/Sync/Code/excmd/node_modules/@babel/parser/lib/index.js:10855:14)
    at Object.parseClassId (/Users/ec/Sync/Code/excmd/node_modules/@babel/parser/lib/index.js:5508:11)
    at Object.parseClass (/Users/ec/Sync/Code/excmd/node_modules/@babel/parser/lib/index.js:10572:10)
    at Object.tsTryParseDeclare (/Users/ec/Sync/Code/excmd/node_modules/@babel/parser/lib/index.js:4984:21)
  pos: 1216,
  loc: Position { line: 3, column: 14 },
  code: 'BABEL_PARSE_ERROR' }

Input Code

import Something from './somewhere.js'

declare class Something {
   // ...
}

Expected behavior/code
As with tsc, this should have no effect on the produced code; these declarations are essentially "inline .d.ts" for non-TypeScript imports. To satisfy the current Babel, I guess I'll need to extract these declarations into additional .d.ts files, and check those into my source-control; but as I only have a single TypeScript file in my codebase, I'd really prefer to keep the typings for other modules centralized to that single TypeScript file!

Babel Configuration (.babelrc, package.json, cli command)

// .babelrc
{
   "presets": ["@babel/env", "@babel/preset-typescript"]
}

// CLI invocation
babel --verbose src --extensions '.ts,.js' --ignore '**/*.bs.js' --ignore '**/*.d.ts' --copy-files --out-dir lib

Environment

  • Babel version(s): 7.5.5 (@babel/core 7.5.5)
  • Node/npm version: v10.12.0
  • OS: macOS 10.14.5
  • How you are using Babel: cli

Additional context/Screenshots
The above example is somewhat massaged / simplified from my actual codebase I can't even get the web-playground (with "TypeScript" enabled!) to parse a simple declare class Blah, but I would have included a working example if I could.

@ELLIOTTCABLE

This comment has been minimized.

Copy link
Author

commented Aug 17, 2019

  • Vaguely relevant: #7943
  • Link to TypeScript playground for above simplified example-code (note that it's properly parsed, and the type-error is then understood);
  • Link to Babel playground, with my attempt to replicate the above. Unfortunately, I'm getting completely unrelated syntax-errors that I don't see locally, not sure why, instead of the actual redeclaration-error I'm reporting here.
@babel-bot

This comment has been minimized.

Copy link
Collaborator

commented Aug 17, 2019

Hey @ELLIOTTCABLE! 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.

@nicolo-ribaudo

This comment has been minimized.

Copy link
Member

commented Aug 18, 2019

Probably fixed by #10352

nicolo-ribaudo added a commit to nicolo-ribaudo/babel that referenced this issue Aug 19, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
3 participants
You can’t perform that action at this time.