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

We should avoid generating names in the global file scope for d.ts to avoid name shadowing #164

Open
dbaeumer opened this issue Sep 20, 2023 · 2 comments
Labels
bug Something isn't working TypeScript TypeScript output generation

Comments

@dbaeumer
Copy link

dbaeumer commented Sep 20, 2023

Currently the d.ts file do generate names in the global file scope. If such a name is redefined inside an interface it will shadow the name in the file scope and might lead to incorrect d.ts files. We should instead use * imports with a $ prefixed name and define types inside the corresponding namespace. For example wasi-clocks-timezone.d.ts could look like this:

import * as $1 from './wasi-clocks-wall-clock';

export namespace Timezone {
	export type Datetime = $1.WallClock.Datetime;

	export type Timezone = number;

	export declare function display(this_: Timezone, when: Datetime): TimezoneDisplay;

	export declare function utcOffset(this_: Timezone, when: Datetime): number;

	export declare function dropTimezone(this_: Timezone): void;

	export interface TimezoneDisplay {
		utcOffset: number;
		name: string;
		inDaylightSavingTime: boolean;
	}
}
export type Timezone = Pick<typeof Timezone, 'display' | 'utcOffset' | 'dropTimezone'>;
@dbaeumer dbaeumer changed the title Don't generate names in the global file scope for d.ts to avoid name shadowing We should avoid generating names in the global file scope for d.ts to avoid name shadowing Sep 20, 2023
@guybedford guybedford added the TypeScript TypeScript output generation label Sep 20, 2023
@guybedford
Copy link
Collaborator

@dbaeumer we do have local name generation utility that handles deduping in https://github.com/bytecodealliance/jco/blob/main/crates/js-component-bindgen/src/names.rs. In the TS generation, we run all name generation through this deduping to get the local name in https://github.com/bytecodealliance/jco/blob/main/crates/js-component-bindgen/src/ts_bindgen.rs#L380.

I wouldn't be surprised if there are bugs, and I'm open to completely changing the way we handle this as well.

If you did notice any specific bug cases with duplicate names not deduping it would be great to check those.

@yoshuawuyts yoshuawuyts added the bug Something isn't working label Oct 19, 2023
@jcbhmr
Copy link

jcbhmr commented Jan 16, 2024

Related to #338 ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working TypeScript TypeScript output generation
Projects
None yet
Development

No branches or pull requests

4 participants