jco
jco copied to clipboard
We should avoid generating names in the global file scope for `d.ts` to avoid name shadowing
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 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.
Related to #338 ?