MCPcopy
hub / github.com/date-fns/date-fns / readRefsFromJSON

Function readRefsFromJSON

pkgs/docs/src/json.ts:18–85  ·  view source on GitHub ↗
(
  config: DateFnsDocs.Config,
  configDir: string,
)

Source from the content-addressed store, hash-verified

16 * @returns parsed function reflections
17 */
18export async function readRefsFromJSON(
19 config: DateFnsDocs.Config,
20 configDir: string,
21): Promise<DateFnsDocs.Reflection[]> {
22 const jsonPath = path.resolve(configDir, config.json);
23 const docs = await readDocsJSON(jsonPath);
24 const map = typesMap(docs);
25
26 return (
27 docs.children?.reduce<DateFnsDocs.Reflection[]>((acc, reflection) => {
28 // Use set to avoid duplicates
29 const recoveredRefs = new Set<DeclarationReflection>();
30 const refMap = typesMap(reflection);
31
32 function recoverRefs(refl: DeclarationReflection) {
33 const refs = extractRefs(refl);
34
35 const missingRefs = new Set<number>();
36 refs.forEach((id) => {
37 if (refMap[id]) return;
38 missingRefs.add(id);
39 });
40
41 missingRefs.forEach((id) => {
42 const missingRef = map[id];
43 if (!missingRef) return;
44
45 recoveredRefs.add(missingRef);
46
47 // Update map, to avoid adding types included with the missing ref
48 refMap[id] = missingRef;
49 Object.assign(refMap, typesMap(missingRef));
50
51 // Recursively recover missing refs
52 recoverRefs(missingRef);
53 });
54 }
55
56 recoverRefs(reflection);
57
58 const completedRef = {
59 ...reflection,
60 children: [...(reflection.children || []), ...recoveredRefs],
61 } as DeclarationReflection;
62
63 const fileName = completedRef.sources?.[0].fileName;
64 const refOverrides = (fileName && config.kindsMap[fileName]) || undefined;
65
66 if (refOverrides?.kind === "constants") {
67 return acc.concat({
68 kind: "constants",
69 ref: completedRef,
70 category: refOverrides?.category,
71 });
72 } else {
73 const fn = findFn(reflection);
74 if (!fn) return acc;
75

Callers 1

getFnPagesFunction · 0.90

Calls 4

readDocsJSONFunction · 0.85
typesMapFunction · 0.85
recoverRefsFunction · 0.85
findFnFunction · 0.85

Tested by

no test coverage detected