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

fix: ts exported vars are shadowed by declare #14946

Merged
merged 2 commits into from Sep 21, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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,9 @@
declare class Signal<T = any> {
value: T
}

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

export { Signal };
Comment on lines +1 to +9
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can also add a test with the declarations swapped?

declare class Signal<T = any> {
	value: T
}

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

export { Signal };

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ts seems to show that it cannot be merged.

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

export { Signal };