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

Closed
ELLIOTTCABLE opened this issue Aug 17, 2019 · 8 comments
Labels
area: typescript Has PR i: bug outdated A closed issue/PR that is archived due to age. Recommended to make a new issue

Comments

@ELLIOTTCABLE
Copy link

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
Copy link
Author

ELLIOTTCABLE commented Aug 17, 2019

@babel-bot
Copy link
Collaborator

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
Copy link
Member

Probably fixed by #10352

nicolo-ribaudo added a commit to nicolo-ribaudo/babel that referenced this issue Aug 19, 2019
@ELLIOTTCABLE
Copy link
Author

I had high hopes, @nicolo-ribaudo, but unfortunately, it doesn't look like #10352 resolved this particular problem! )=

With babel --version #=> 7.8.3 (@babel/core 7.8.3),

import * as $_AST from 'bs-excmd/src/aST.bs'

declare class $_AST {
   static copy_expression($expr: $Expressiont): $Expressiont
   static get_literal_exn($sub: $or_subexpr): $string
   static get_sub_exn($sub: $or_subexpr): $Expressiont
   // ...

I'm still getting:

$ babel --root-mode upward --verbose src --extensions '.ts,.js' --ignore '**/*.d.ts' --copy-files --out-dir lib
TypeError: Duplicate declaration "$_AST"
  66 | type $element = Nominal<object, 'MenhirInterpreter.element'>
  67 | 
> 68 | declare class $_AST {
     |               ^^^^^
  69 |    static copy_expression($expr: $Expressiont): $Expressiont
  70 |    // FIXME: this is a lieeeee
  71 |    static get_literal_exn($sub: $or_subexpr): $string
    at File.buildCodeFrameError (/Users/ec/Sync/Code/excmd/node_modules/@babel/core/lib/transformation/file/file.js:267:12)
    at Scope.checkBlockScopedCollisions (/Users/ec/Sync/Code/excmd/node_modules/@babel/core/node_modules/@babel/traverse/lib/scope/index.js:306:22)
    at Scope.registerBinding (/Users/ec/Sync/Code/excmd/node_modules/@babel/core/node_modules/@babel/traverse/lib/scope/index.js:459:16)
    at Scope.registerDeclaration (/Users/ec/Sync/Code/excmd/node_modules/@babel/core/node_modules/@babel/traverse/lib/scope/index.js:406:12)
    at Object.BlockScoped (/Users/ec/Sync/Code/excmd/node_modules/@babel/core/node_modules/@babel/traverse/lib/scope/index.js:149:28)
    at Object.newFn (/Users/ec/Sync/Code/excmd/node_modules/@babel/core/node_modules/@babel/traverse/lib/visitors.js:220:17)
    at NodePath._call (/Users/ec/Sync/Code/excmd/node_modules/@babel/core/node_modules/@babel/traverse/lib/path/context.js:55:20)
    at NodePath.call (/Users/ec/Sync/Code/excmd/node_modules/@babel/core/node_modules/@babel/traverse/lib/path/context.js:38:14)
    at NodePath.visit (/Users/ec/Sync/Code/excmd/node_modules/@babel/core/node_modules/@babel/traverse/lib/path/context.js:90:31)
    at TraversalContext.visitQueue (/Users/ec/Sync/Code/excmd/node_modules/@babel/core/node_modules/@babel/traverse/lib/context.js:112:16)

@nicolo-ribaudo
Copy link
Member

nicolo-ribaudo commented Jan 23, 2020

It fixed it for the parser but not in @babel/traverse

@ELLIOTTCABLE
Copy link
Author

Ahhhh, I see. Just to be sure: the non-registration fix should affect this combination of an ES6 module-star-import and a class-type-declaration?

Should I open a separate Issue somewhere else? 😣 I took a look at your changes, and dug around checkBlockScopedCollisions in hopes that perhaps I could correspondingly update @babel/traverse to match, but it doesn't look like that module handles TypeScript specially at all, so I'm a little afraid to dive in and try to add the first TypeScript-specific hack in there! 🤣

@nicolo-ribaudo
Copy link
Member

Well, when we introduce a big new feature like TS it will inevitably sneak across all the core modules 🤷‍♂️ 😛

@liuxingbaoyu
Copy link
Member

Can't reproduce in latest version, seems to have been fixed.

@github-actions github-actions bot added the outdated A closed issue/PR that is archived due to age. Recommended to make a new issue label Dec 31, 2022
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Dec 31, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area: typescript Has PR i: bug outdated A closed issue/PR that is archived due to age. Recommended to make a new issue
Projects
None yet
Development

No branches or pull requests

4 participants