Skip to content

Commit

Permalink
feat!: make topDeclDeps trivially serializable (#29)
Browse files Browse the repository at this point in the history
  • Loading branch information
scarf005 committed Feb 26, 2024
1 parent 6f94b65 commit 226a311
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 32 deletions.
20 changes: 10 additions & 10 deletions graph/__snapshots__/top_decl_deps_test.ts.snap
Expand Up @@ -2,18 +2,18 @@ export const snapshot = {};

snapshot[`getTopDeclDeps() converts graph into valid TopDeclDeps 1`] = `
{
"AliasedImport (/d.tsx:8:14)": [
"a (/a.ts:1:14)",
"vscode://file/b.tsx:5:14?kind=VariableDeclaration&name=CompAAA": [
"vscode://file/a.ts:3:14?kind=VariableDeclaration&name=aaa",
"vscode://file/a.ts:1:14?kind=VariableDeclaration&name=a",
],
"CompAAA (/b.tsx:5:14)": [
"aaa (/a.ts:3:14)",
"a (/a.ts:1:14)",
"vscode://file/d.tsx:4:14?kind=VariableDeclaration&name=InnerImport": [
"vscode://file/b.tsx:4:14?kind=VariableDeclaration&name=Comp",
"vscode://file/a.ts:1:14?kind=VariableDeclaration&name=a",
"vscode://file/c.tsx:7:14?kind=VariableDeclaration&name=Page",
"vscode://file/b.tsx:8:14?kind=VariableDeclaration&name=Unrelated",
],
"InnerImport (/d.tsx:4:14)": [
"Comp (/b.tsx:4:14)",
"a (/a.ts:1:14)",
"Page (/c.tsx:7:14)",
"Unrelated (/b.tsx:8:14)",
"vscode://file/d.tsx:8:14?kind=VariableDeclaration&name=AliasedImport": [
"vscode://file/a.ts:1:14?kind=VariableDeclaration&name=a",
],
}
`;
12 changes: 0 additions & 12 deletions graph/_format.ts
Expand Up @@ -2,7 +2,6 @@ import { Reducer, Stream } from "https://deno.land/x/rimbu@1.2.0/stream/mod.ts"
import { encodeVSCodeURI, prettyPrintURI } from "./vscode_uri.ts"

import type { Declaration, DeclDeps } from "./decl_deps.ts"
import type { TopDeclDeps } from "./top_decl_deps.ts"

export const serializeNoColor = (x: unknown) =>
Deno.inspect(x, {
Expand All @@ -28,14 +27,3 @@ export const declDepsSerializer = (declDeps: DeclDeps) =>
serializeNoColor(
asRecord((x) => prettyPrintURI(encodeVSCodeURI(x)))(declDeps),
)

export const topDeclDepsSerializer = (topDeclDeps: TopDeclDeps): string =>
serializeNoColor(Object.fromEntries(
Array.from(topDeclDeps.entries())
.map(([decl, deps]) =>
[
prettyPrintURI(decl),
Array.from(deps).map(prettyPrintURI),
] as const
),
))
9 changes: 6 additions & 3 deletions graph/graph_descendants.ts
Expand Up @@ -44,7 +44,10 @@ export const graphDescendantsRaw = <T>(
* TOP2 <- B, a, c
* ```
*/
export const graphDescendants = <T>(graph: ArrowGraph<T>): Map<T, Set<T>> =>
// deno-lint-ignore no-explicit-any
export const graphDescendants = <T extends keyof any>(
graph: ArrowGraph<T>,
): Record<T, T[]> =>
graphDescendantsRaw(graph).stream()
.map(([k, v]) => [k, v.stream().reduce(Reducer.toJSSet())] as const)
.reduce(Reducer.toJSMap())
.map(([k, v]) => [k, [...v.stream().reduce(Reducer.toJSSet())]] as [T, T[]])
.reduce(Reducer.toJSObject())
8 changes: 4 additions & 4 deletions graph/graph_descendants_test.ts
Expand Up @@ -15,10 +15,10 @@ Deno.test("graphDescendants() gets all valid path of directed graph", () => {

const result = graphDescendants(graph)

const expected = new Map([
["TOP1", new Set(["A", "a", "b"])],
["TOP2", new Set(["B", "a", "c"])],
])
const expected = {
TOP1: ["b", "A", "a"],
TOP2: ["a", "B", "c"],
}

assertEquals(result, expected)
})
2 changes: 1 addition & 1 deletion graph/top_decl_deps.ts
Expand Up @@ -3,7 +3,7 @@ import type { Graph } from "./graph.ts"
import { VSCodeURI } from "./vscode_uri.ts"
import { graphDescendants } from "./graph_descendants.ts"

export type TopDeclDeps = Map<VSCodeURI, Set<VSCodeURI>>
export type TopDeclDeps = Record<VSCodeURI, VSCodeURI[]>

/**
* Flattens graph of references into top-level links.
Expand Down
3 changes: 1 addition & 2 deletions graph/top_decl_deps_test.ts
Expand Up @@ -7,7 +7,6 @@ import { getAllDecls } from "./decls.ts"
import { declDepsToGraph } from "./graph.ts"
import { getTopDeclDeps } from "./top_decl_deps.ts"
import { snapshotTest } from "./_snapshot.ts"
import { topDeclDepsSerializer } from "./_format.ts"

snapshotTest(
"getTopDeclDeps() converts graph into valid TopDeclDeps",
Expand All @@ -20,6 +19,6 @@ snapshotTest(

const topDeclDeps = getTopDeclDeps(graph)

await assertSnapshot(t, topDeclDeps, { serializer: topDeclDepsSerializer })
await assertSnapshot(t, topDeclDeps)
},
)

0 comments on commit 226a311

Please sign in to comment.