Skip to content

Commit bedfc72

Browse files
authored
fix: ts exported vars are shadowed by declare (#14946)
* fix * review
1 parent c602be9 commit bedfc72

3 files changed

Lines changed: 52 additions & 6 deletions

File tree

packages/babel-plugin-transform-typescript/src/index.ts

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ function isInType(path: NodePath) {
3636
const 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.
4040
const NEEDS_EXPLICIT_ESM = new WeakMap();
4141
const PARSED_PARAMS = new WeakSet();
4242

@@ -60,6 +60,21 @@ function isGlobalType({ scope }: NodePath, name: string) {
6060
function 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+
6378
export 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+
7490
type ExtraNodeProps = {
7591
declare?: unknown;
7692
accessibility?: unknown;
7793
abstract?: unknown;
7894
optional?: unknown;
7995
override?: unknown;
8096
};
97+
8198
export 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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
declare class Signal<T = any> {
2+
value: T
3+
}
4+
5+
function Signal(this: Signal, value?: unknown) {
6+
this.value = value
7+
}
8+
9+
export { Signal };
10+
11+
12+
function Signal2(this: Signal2, value?: unknown) {
13+
this.value = value
14+
}
15+
declare class Signal2<T = any> {
16+
value: T
17+
}
18+
19+
export { Signal2 };
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
function Signal(value) {
2+
this.value = value;
3+
}
4+
5+
export { Signal };
6+
7+
function Signal2(value) {
8+
this.value = value;
9+
}
10+
11+
export { Signal2 };

0 commit comments

Comments
 (0)