Skip to content

Commit 9f0fe1c

Browse files
committed
fix ancestor logic
1 parent ce4ed57 commit 9f0fe1c

File tree

3 files changed

+34
-19
lines changed

3 files changed

+34
-19
lines changed

Diff for: packages/base/links-to-editor.gts

+13-1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import {
2424
RealmURLContextName,
2525
type ResolvedCodeRef,
2626
getNarrowestType,
27+
Loader,
2728
} from '@cardstack/runtime-common';
2829
import { AddButton, IconButton } from '@cardstack/boxel-ui/components';
2930
import { IconMinusCircle } from '@cardstack/boxel-ui/icons';
@@ -146,7 +147,7 @@ export class LinksToEditor extends GlimmerComponent<Signature> {
146147

147148
private chooseCard = restartableTask(async () => {
148149
let type = identifyCard(this.args.field.card) ?? baseCardRef;
149-
type = await getNarrowestType(this.args.subclassType, type);
150+
type = await getNarrowestType(this.args.subclassType, type, myLoader());
150151
let chosenCard: CardDef | undefined = await chooseCard(
151152
{ filter: { type } },
152153
{
@@ -164,3 +165,14 @@ export class LinksToEditor extends GlimmerComponent<Signature> {
164165
}
165166
});
166167
}
168+
169+
function myLoader(): Loader {
170+
// we know this code is always loaded by an instance of our Loader, which sets
171+
// import.meta.loader.
172+
173+
// When type-checking realm-server, tsc sees this file and thinks
174+
// it will be transpiled to CommonJS and so it complains about this line. But
175+
// this file is always loaded through our loader and always has access to import.meta.
176+
// @ts-ignore
177+
return (import.meta as any).loader;
178+
}

Diff for: packages/base/links-to-many-component.gts

+13-1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import {
3131
RealmURLContextName,
3232
type ResolvedCodeRef,
3333
getNarrowestType,
34+
Loader,
3435
} from '@cardstack/runtime-common';
3536
import { IconMinusCircle, IconX, FourLines } from '@cardstack/boxel-ui/icons';
3637
import { eq } from '@cardstack/boxel-ui/helpers';
@@ -98,7 +99,7 @@ class LinksToManyEditor extends GlimmerComponent<Signature> {
9899
selectedCards?.map((card: any) => ({ not: { eq: { id: card.id } } })) ??
99100
[];
100101
let type = identifyCard(this.args.field.card) ?? baseCardRef;
101-
type = await getNarrowestType(this.args.subclassType, type);
102+
type = await getNarrowestType(this.args.subclassType, type, myLoader());
102103
let filter = {
103104
every: [{ type }, ...selectedCardsQuery],
104105
};
@@ -488,3 +489,14 @@ export function getLinksToManyComponent({
488489
},
489490
});
490491
}
492+
493+
function myLoader(): Loader {
494+
// we know this code is always loaded by an instance of our Loader, which sets
495+
// import.meta.loader.
496+
497+
// When type-checking realm-server, tsc sees this file and thinks
498+
// it will be transpiled to CommonJS and so it complains about this line. But
499+
// this file is always loaded through our loader and always has access to import.meta.
500+
// @ts-ignore
501+
return (import.meta as any).loader;
502+
}

Diff for: packages/runtime-common/code-ref.ts

+8-17
Original file line numberDiff line numberDiff line change
@@ -233,22 +233,11 @@ function refEquals(ref1: CodeRef, ref2: CodeRef): boolean {
233233
if (!isResolvedCodeRef(ref1) || !isResolvedCodeRef(ref2)) {
234234
return false;
235235
}
236-
return ref1.name === ref1.module && ref1.module === ref2.module;
236+
return ref1.name === ref2.name && ref1.module === ref2.module;
237237
}
238238

239-
function myLoader(): Loader {
240-
// we know this code is always loaded by an instance of our Loader, which sets
241-
// import.meta.loader.
242-
243-
// When type-checking realm-server, tsc sees this file and thinks
244-
// it will be transpiled to CommonJS and so it complains about this line. But
245-
// this file is always loaded through our loader and always has access to import.meta.
246-
// @ts-ignore
247-
return (import.meta as any).loader;
248-
}
249-
250-
async function getAncestorRef(codeRef: CodeRef) {
251-
let card = await loadCard(codeRef, { loader: myLoader() });
239+
async function getAncestorRef(codeRef: CodeRef, loader: Loader) {
240+
let card = await loadCard(codeRef, { loader: loader });
252241
let ancestor = getAncestor(card);
253242
return identifyCard(ancestor);
254243
}
@@ -258,13 +247,14 @@ async function getAncestorRef(codeRef: CodeRef) {
258247
async function isInsideAncestorChain(
259248
codeRef: CodeRef,
260249
codeRefAncestor: CodeRef,
250+
loader: Loader,
261251
): Promise<boolean | undefined> {
262252
if (refEquals(codeRef, codeRefAncestor)) {
263253
return true;
264254
} else {
265-
let newAncestorRef = await getAncestorRef(codeRef);
255+
let newAncestorRef = await getAncestorRef(codeRef, loader);
266256
if (newAncestorRef) {
267-
return isInsideAncestorChain(newAncestorRef, codeRefAncestor);
257+
return isInsideAncestorChain(newAncestorRef, codeRefAncestor, loader);
268258
} else {
269259
return undefined;
270260
}
@@ -275,11 +265,12 @@ async function isInsideAncestorChain(
275265
export async function getNarrowestType(
276266
subclassType: CodeRef | undefined,
277267
type: CodeRef,
268+
loader: Loader,
278269
) {
279270
let narrowTypeExists: boolean = false;
280271
if (subclassType) {
281272
narrowTypeExists =
282-
(await isInsideAncestorChain(subclassType, type)) ?? false;
273+
(await isInsideAncestorChain(subclassType, type, loader)) ?? false;
283274
}
284275
let narrowestType = narrowTypeExists && subclassType ? subclassType : type;
285276
return narrowestType;

0 commit comments

Comments
 (0)