Skip to content

Commit

Permalink
Export default module interface types directly (#979)
Browse files Browse the repository at this point in the history
We were using an obscure CommonJS syntax for what was essentially a type
alias for the default modules and then exporting them in a large
object-like export. Instead, just `export type` directly in the
aliasing and explicitly reexport nested namespaces.
  • Loading branch information
scotttrinh authored Apr 25, 2024
1 parent 07bff6f commit d1b1878
Showing 1 changed file with 17 additions and 19 deletions.
36 changes: 17 additions & 19 deletions packages/generate/src/edgeql-js/generateInterfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -197,28 +197,26 @@ export const generateInterfaces = (params: GenerateInterfacesParams) => {
plainTypesCode.addExport(module.internalName, { modes: ["js"] });

if (module.isRoot && module.name === "default") {
const typeRefs = [
...module.types.values(),
...[...module.nestedModules.values()].map(
(nestedMod) => nestedMod.fullInternalName
),
];
for (const typeRef of typeRefs) {
const sliceTo = module.internalName.length + 1;
for (const typeRef of module.types.values()) {
const aliased = typeRef.slice(sliceTo);
plainTypesCode.writeln([`export type ${aliased} = ${typeRef};\n`]);
}

for (const nestedMod of module.nestedModules.values()) {
plainTypesCode.writeln([
`import ${typeRef.slice(
module.internalName.length + 1
)} = ${typeRef};`,
`export namespace ${nestedMod.internalName} {`,
]);
plainTypesCode.increaseIndent();
for (const typeRef of nestedMod.types.values()) {
const aliased = typeRef.slice(
sliceTo + nestedMod.internalName.length + 1
);
plainTypesCode.writeln([`export type ${aliased} = ${typeRef};`]);
}
plainTypesCode.decreaseIndent();
plainTypesCode.writeln([`}`]);
}
plainTypesCode.writeln([t`export type {`]);
plainTypesCode.increaseIndent();
plainTypesCode.writeln([
typeRefs
.map((typeRef) => typeRef.slice(module.internalName.length + 1))
.join(",\n"),
]);
plainTypesCode.decreaseIndent();
plainTypesCode.writeln([t`};`]);
}
};

Expand Down

0 comments on commit d1b1878

Please sign in to comment.