@@ -36,7 +36,7 @@ function isInType(path: NodePath) {
3636const GLOBAL_TYPES = new WeakMap < Scope , Set < string > > ( ) ;
3737// Track programs which contain imports/exports of values, so that we can include
3838// empty exports for programs that do not, but were parsed as modules. This allows
39- // tools to infer unamibiguously that results are ESM.
39+ // tools to infer unambiguously that results are ESM.
4040const NEEDS_EXPLICIT_ESM = new WeakMap ( ) ;
4141const PARSED_PARAMS = new WeakSet ( ) ;
4242
@@ -60,6 +60,21 @@ function isGlobalType({ scope }: NodePath, name: string) {
6060function registerGlobalType ( programScope : Scope , name : string ) {
6161 GLOBAL_TYPES . get ( programScope ) . add ( name ) ;
6262}
63+
64+ // A hack to avoid removing the impl Binding when we remove the declare NodePath
65+ function safeRemove ( path : NodePath ) {
66+ const ids = path . getBindingIdentifiers ( ) ;
67+ for ( const name of Object . keys ( ids ) ) {
68+ const binding = path . scope . getBinding ( name ) ;
69+ if ( binding && binding . identifier === ids [ name ] ) {
70+ binding . scope . removeBinding ( name ) ;
71+ }
72+ }
73+ path . opts . noScope = true ;
74+ path . remove ( ) ;
75+ path . opts . noScope = false ;
76+ }
77+
6378export interface Options extends SyntaxOptions {
6479 /** @default true */
6580 allowNamespaces ?: boolean ;
@@ -71,13 +86,15 @@ export interface Options extends SyntaxOptions {
7186 optimizeConstEnums ?: boolean ;
7287 allowDeclareFields ?: boolean ;
7388}
89+
7490type ExtraNodeProps = {
7591 declare ?: unknown ;
7692 accessibility ?: unknown ;
7793 abstract ?: unknown;
7894 optional ?: unknown ;
7995 override ?: unknown ;
8096} ;
97+
8198export default declare ( ( api , opts : Options ) => {
8299 api . assertVersion ( 7 ) ;
83100
@@ -451,16 +468,16 @@ export default declare((api, opts: Options) => {
451468 } ,
452469
453470 TSDeclareFunction ( path ) {
454- path . remove ( ) ;
471+ safeRemove ( path ) ;
455472 } ,
456473
457474 TSDeclareMethod ( path ) {
458- path . remove ( ) ;
475+ safeRemove ( path ) ;
459476 } ,
460477
461478 VariableDeclaration ( path ) {
462479 if ( path . node . declare ) {
463- path . remove ( ) ;
480+ safeRemove ( path ) ;
464481 }
465482 } ,
466483
@@ -475,8 +492,7 @@ export default declare((api, opts: Options) => {
475492 ClassDeclaration ( path ) {
476493 const { node } = path ;
477494 if ( node . declare ) {
478- path . remove ( ) ;
479- return ;
495+ safeRemove ( path ) ;
480496 }
481497 } ,
482498
0 commit comments