Skip to content

Commit

Permalink
refactor!: rename API terminology
Browse files Browse the repository at this point in the history
  • Loading branch information
scarf005 committed Feb 20, 2024
1 parent ad47fb8 commit cf0b21e
Show file tree
Hide file tree
Showing 8 changed files with 42 additions and 49 deletions.
34 changes: 0 additions & 34 deletions graph/create.ts

This file was deleted.

7 changes: 4 additions & 3 deletions graph/deps_map.ts → graph/decl_deps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ export type Declaration =
| ClassDeclaration
| FunctionDeclaration

export type DepsMap = Map<Declaration, Declaration[]>
export type DeclDeps = Map<Declaration, Declaration[]>

/**
* @privateRemarks
*
Expand Down Expand Up @@ -58,8 +59,8 @@ const getReferencedVarDecls = (node: Declaration): Declaration[] => {
}

/** Recursively query variable references into key-value map */
export const fromLinks = (links: Declaration[]): DepsMap => {
const graph: DepsMap = new Map()
export const getDeclDeps = (links: Declaration[]): DeclDeps => {
const graph: DeclDeps = new Map()

let current = links
while (current.length > 0) {
Expand Down
2 changes: 1 addition & 1 deletion graph/links.ts → graph/decls.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {
StreamSource,
} from "https://deno.land/x/[email protected]/stream/mod.ts"
import { SourceFile } from "../deps/ts_morph.ts"
import { Declaration } from "./deps_map.ts"
import { Declaration } from "./decl_deps.ts"

/**
* Find initial declarations to begin analyze with.
Expand Down
4 changes: 2 additions & 2 deletions graph/links_bench.ts → graph/decls_bench.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { SourceFile } from "../deps/ts_morph.ts"
import type { Declaration } from "./deps_map.ts"
import { getDecls as getDeclsStream } from "./links.ts"
import type { Declaration } from "./decl_deps.ts"
import { getDecls as getDeclsStream } from "./decls.ts"
import { getDeclExampleFile } from "./_example_project.ts"

/**
Expand Down
4 changes: 2 additions & 2 deletions graph/links_test.ts → graph/decls_test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { Declaration } from "../graph/deps_map.ts"
import { getDecls } from "../graph/links.ts"
import type { Declaration } from "./decl_deps.ts"
import { getDecls } from "./decls.ts"
import { assertEquals } from "https://deno.land/[email protected]/assert/assert_equals.ts"
import { declExampleText } from "./_example_project.ts"
import { inMemoryProject } from "./_project.ts"
Expand Down
24 changes: 24 additions & 0 deletions graph/graph.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import type { GraphElement } from "https://deno.land/x/[email protected]/graph/custom/common/link.ts"
import { ArrowGraphHashed } from "https://deno.land/x/[email protected]/graph/mod.ts"
import { Stream } from "https://deno.land/x/[email protected]/stream/mod.ts"
import { DeclDeps } from "./decl_deps.ts"

export type Id = string

/** directed graph of references, using `string` as unique key */
export type Graph = ArrowGraphHashed<Id>

/**
* ## Warning
*
* uses `Node.getName` as unique key which is prone to collisions
* if there are multiple variables with the same name
*
* FIXME: assumes class declaration has names (hence `!`)
*/
export const declDepsToGraph = (declDeps: DeclDeps): Graph =>
Stream.from(declDeps).flatMap(([source, targets]) =>
Stream.from(targets)
.map((to): GraphElement<Id> => [source.getName()!, to.getName()!])
)
.reduce(ArrowGraphHashed.reducer())
10 changes: 6 additions & 4 deletions graph/mod.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
export * from "./create.ts"
export * from "./flatten.ts"
export * from "./links.ts"
export * from "./deps_map.ts"
export * from "./decls.ts"

export * from "./decl_deps.ts"
export * from "./graph.ts"
export * from "./top_decl_deps.ts"

export * from "./fs.ts"
6 changes: 3 additions & 3 deletions graph/flatten.ts → graph/top_decl_deps.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { HashMap, HashSet } from "https://deno.land/x/[email protected]/hashed/mod.ts"
import { LinkDepsGraph } from "./create.ts"
import type { Graph, Id } from "./graph.ts"

export type FlatDepsMap = HashMap<string, HashSet<string>>
export type TopDeclDeps = HashMap<Id, HashSet<Id>>

/**
* Flattens graph of references into top-level links
Expand All @@ -20,7 +20,7 @@ export type FlatDepsMap = HashMap<string, HashSet<string>>
* TOP2 <- B, a, c
* ```
*/
export const fromGraph = (graph: LinkDepsGraph): FlatDepsMap => {
export const graphToTopDeclDeps = (graph: Graph): TopDeclDeps => {
const topLevelNodes = graph.streamNodes()
.filter((node) => graph.getConnectionsFrom(node).size === 0)

Expand Down

0 comments on commit cf0b21e

Please sign in to comment.