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

Wrap types in default module in $default namespace and re-export them at top level #888

Merged
merged 2 commits into from Mar 5, 2024
Merged
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
34 changes: 17 additions & 17 deletions packages/generate/src/edgeql-js/generateInterfaces.ts
@@ -1,4 +1,4 @@
import { CodeBuffer, js, t } from "../builders";
import { CodeBuffer, t } from "../builders";
import type { GeneratorParams } from "../genutil";
import { $ } from "../genutil";
import { makePlainIdent, quote, splitName, toTSScalarType } from "../genutil";
Expand Down Expand Up @@ -32,8 +32,7 @@ export const generateInterfaces = (params: GenerateInterfacesParams) => {
const modName = modParts[modParts.length - 1];
const parentModName = modParts.slice(0, -1).join("::");
const parent = parentModName ? getModule(parentModName) : null;
const internalName =
modName === "default" && !parentModName ? "" : makePlainIdent(modName);
const internalName = makePlainIdent(modName);

module = {
name: modName,
Expand Down Expand Up @@ -73,9 +72,8 @@ export const generateInterfaces = (params: GenerateInterfacesParams) => {
(typeName: string, withModule: boolean = false): string => {
const { tMod, tName, module } = getPlainTypeModule(typeName);
return (
((mod !== tMod || withModule) && tMod !== "default"
? `${module.fullInternalName}.`
: "") + `${makePlainIdent(tName)}`
(mod !== tMod || withModule ? `${module.fullInternalName}.` : "") +
`${makePlainIdent(tName)}`
);
};

Expand Down Expand Up @@ -178,12 +176,8 @@ export const generateInterfaces = (params: GenerateInterfacesParams) => {
const plainTypesExportBuf = new CodeBuffer();

const writeModuleExports = (module: ModuleData) => {
const wrapInNamespace = !(module.isRoot && module.name === "default");
if (wrapInNamespace) {
plainTypesCode.writeln([t`export namespace ${module.internalName} {`]);
plainTypesCode.writeln([js`const ${module.internalName} = {`]);
plainTypesCode.increaseIndent();
scotttrinh marked this conversation as resolved.
Show resolved Hide resolved
}
plainTypesCode.writeln([t`export namespace ${module.internalName} {`]);
plainTypesCode.increaseIndent();

plainTypesCode.writeBuf(module.buf);

Expand All @@ -200,11 +194,17 @@ export const generateInterfaces = (params: GenerateInterfacesParams) => {
plainTypesExportBuf.decreaseIndent();
plainTypesExportBuf.writeln([t`};`]);

if (wrapInNamespace) {
plainTypesCode.decreaseIndent();
plainTypesCode.writeln([t`}`]);
plainTypesCode.writeln([js`}`]);
plainTypesCode.addExport(module.internalName, { modes: ["js"] });
plainTypesCode.decreaseIndent();
plainTypesCode.writeln([t`}`]);
plainTypesCode.addExport(module.internalName, { modes: ["js"] });

if (module.isRoot && module.name === "default") {
for (const [typeName, typeRef] of module.types) {
plainTypesCode.writeln([`import ${typeName} = ${typeRef};`]);
}
plainTypesCode.writeln([
`export {${[...module.types.keys()].join(", ")}};`,
]);
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

Seems like this is already covered by the existing tests in interfaces.test.ts, but do you think it'd be easy to write a quick check for the pathological case we're fixing here?

Copy link
Member Author

Choose a reason for hiding this comment

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

Added some tests and fixes some bugs.

};

Expand Down