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

feat!: make topDeclDeps trivially serializable #29

Merged
merged 1 commit into from
Feb 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions graph/__snapshots__/top_decl_deps_test.ts.snap
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { Reducer, Stream } from "https://deno.land/x/[email protected]/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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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)
},
)