Skip to content

Commit

Permalink
Fix collision in QB with nested object & module names (#900)
Browse files Browse the repository at this point in the history
  • Loading branch information
CarsonF committed Mar 12, 2024
1 parent 45fb70a commit 5d4ee2b
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 7 deletions.
13 changes: 13 additions & 0 deletions integration-tests/lts/dbschema/default.esdl
Expand Up @@ -220,4 +220,17 @@ module User {
scalar type Status extending enum<"Active", "Disabled">;

type User extending default::User;

type Profile {
link address -> User::Profile::MailingAddress;
}

module Profile {
type MailingAddress {
property street -> str;
property city -> str;
property state -> str;
property zip -> str;
}
}
}
14 changes: 14 additions & 0 deletions integration-tests/lts/dbschema/migrations/00025.edgeql
@@ -0,0 +1,14 @@
CREATE MIGRATION m1rjlewu5fimvn4lf4xguullcbvlttuxmtxyl4yz4dmsbyw5shva7a
ONTO m13x34vijy2dlwl3x5jewnjcxb6tysyo7pr2zbbljadjdi2y5w3cja
{
CREATE MODULE User::Profile IF NOT EXISTS;
CREATE TYPE User::Profile::MailingAddress {
CREATE PROPERTY city: std::str;
CREATE PROPERTY state: std::str;
CREATE PROPERTY street: std::str;
CREATE PROPERTY zip: std::str;
};
CREATE TYPE User::Profile {
CREATE LINK address: User::Profile::MailingAddress;
};
};
45 changes: 38 additions & 7 deletions packages/generate/src/builders.ts
Expand Up @@ -488,21 +488,40 @@ class BuilderImportsExports {
exports.push(`export type { ${exportTypes.join(", ")} };\n`);
}
if (refsDefault.length || forceDefaultExport) {
const refsByExportAs = [...groupToMapBy(refsDefault, (x) => x.as)].map(
([as, group]) => ({
as,
refs: group.map(({ ref }) => ref).reverse(),
})
);
if (mode === "ts" || mode === "dts") {
exports.push(
`${
mode === "dts" ? "declare " : ""
}type __defaultExports = {\n${refsDefault
.map(({ ref, as }) => ` ${genutil.quote(as)}: typeof ${ref}`)
}type __defaultExports = {\n${refsByExportAs
.map(({ refs, as }) => {
const key = genutil.quote(as);
const value = refs.map((ref) => `typeof ${ref}`).join(" & ");
return ` ${key}: ${value}`;
})
.join(";\n")}\n};`
);
}
if (mode === "ts" || mode === "js") {
exports.push(
`const __defaultExports${
mode === "ts" ? ": __defaultExports" : ""
} = {\n${refsDefault
.map(({ ref, as }) => ` ${genutil.quote(as)}: ${ref}`)
} = {\n${refsByExportAs
.map(({ refs, as }) => {
const key = genutil.quote(as);
const value =
refs.length === 1
? refs
: `Object.freeze({ ${refs
.map((ref) => `...${ref}`)
.join(", ")} })`;
return ` ${key}: ${value}`;
})
.join(",\n")}\n};`
);
}
Expand Down Expand Up @@ -603,15 +622,16 @@ export class CodeBuilder {

let prefix = "";
if (ref.dir !== this.dir) {
const mod = path.posix.basename(ref.dir, path.posix.extname(ref.dir));
prefix = `_${mod}`;
const namespaceParts = identRef.name.split("::").slice(0, -1);
prefix = `_${namespaceParts.join("")}`;
const moduleFileName = namespaceParts.at(-1)!;

let importPath = path.posix.join(
path.posix.relative(
path.posix.dirname(this.dir),
path.posix.dirname(ref.dir)
),
mod
moduleFileName
);

if (!importPath.startsWith("../")) {
Expand Down Expand Up @@ -846,3 +866,14 @@ export class DirBuilder {
}
}
}

const groupToMapBy = <K, V>(
items: Iterable<V>,
by: (item: V) => K
): ReadonlyMap<K, readonly V[]> =>
[...items].reduce((map, item) => {
const groupKey = by(item);
const prev = map.get(groupKey) ?? [];
map.set(groupKey, [...prev, item]);
return map;
}, new Map<K, V[]>());

0 comments on commit 5d4ee2b

Please sign in to comment.