Skip to content

Commit

Permalink
fix: ts exported vars are shadowed by declare (#14946)
Browse files Browse the repository at this point in the history
* fix

* review
  • Loading branch information
liuxingbaoyu committed Sep 21, 2022
1 parent c602be9 commit bedfc72
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 6 deletions.
28 changes: 22 additions & 6 deletions packages/babel-plugin-transform-typescript/src/index.ts
Expand Up @@ -36,7 +36,7 @@ function isInType(path: NodePath) {
const GLOBAL_TYPES = new WeakMap<Scope, Set<string>>();
// Track programs which contain imports/exports of values, so that we can include
// empty exports for programs that do not, but were parsed as modules. This allows
// tools to infer unamibiguously that results are ESM.
// tools to infer unambiguously that results are ESM.
const NEEDS_EXPLICIT_ESM = new WeakMap();
const PARSED_PARAMS = new WeakSet();

Expand All @@ -60,6 +60,21 @@ function isGlobalType({ scope }: NodePath, name: string) {
function registerGlobalType(programScope: Scope, name: string) {
GLOBAL_TYPES.get(programScope).add(name);
}

// A hack to avoid removing the impl Binding when we remove the declare NodePath
function safeRemove(path: NodePath) {
const ids = path.getBindingIdentifiers();
for (const name of Object.keys(ids)) {
const binding = path.scope.getBinding(name);
if (binding && binding.identifier === ids[name]) {
binding.scope.removeBinding(name);
}
}
path.opts.noScope = true;
path.remove();
path.opts.noScope = false;
}

export interface Options extends SyntaxOptions {
/** @default true */
allowNamespaces?: boolean;
Expand All @@ -71,13 +86,15 @@ export interface Options extends SyntaxOptions {
optimizeConstEnums?: boolean;
allowDeclareFields?: boolean;
}

type ExtraNodeProps = {
declare?: unknown;
accessibility?: unknown;
abstract?: unknown;
optional?: unknown;
override?: unknown;
};

export default declare((api, opts: Options) => {
api.assertVersion(7);

Expand Down Expand Up @@ -451,16 +468,16 @@ export default declare((api, opts: Options) => {
},

TSDeclareFunction(path) {
path.remove();
safeRemove(path);
},

TSDeclareMethod(path) {
path.remove();
safeRemove(path);
},

VariableDeclaration(path) {
if (path.node.declare) {
path.remove();
safeRemove(path);
}
},

Expand All @@ -475,8 +492,7 @@ export default declare((api, opts: Options) => {
ClassDeclaration(path) {
const { node } = path;
if (node.declare) {
path.remove();
return;
safeRemove(path);
}
},

Expand Down
@@ -0,0 +1,19 @@
declare class Signal<T = any> {
value: T
}

function Signal(this: Signal, value?: unknown) {
this.value = value
}

export { Signal };


function Signal2(this: Signal2, value?: unknown) {
this.value = value
}
declare class Signal2<T = any> {
value: T
}

export { Signal2 };
@@ -0,0 +1,11 @@
function Signal(value) {
this.value = value;
}

export { Signal };

function Signal2(value) {
this.value = value;
}

export { Signal2 };

0 comments on commit bedfc72

Please sign in to comment.