From 5422e897d828ae413a7bfd2f4be60826365e36b6 Mon Sep 17 00:00:00 2001 From: scarf Date: Mon, 26 Feb 2024 12:43:26 +0900 Subject: [PATCH] feat: `GithubURI` (#30) * feat: add hash to `VSCodeURI` * feat: `GithubURI` --- graph/__snapshots__/graph_test.ts.snap | 4 ++-- .../__snapshots__/top_decl_deps_test.ts.snap | 20 +++++++++---------- graph/github_uri.ts | 18 +++++++++++++++++ graph/github_uri_test.ts | 16 +++++++++++++++ graph/vscode_uri.ts | 3 ++- graph/vscode_uri_test.ts | 2 +- 6 files changed, 49 insertions(+), 14 deletions(-) create mode 100644 graph/github_uri.ts create mode 100644 graph/github_uri_test.ts diff --git a/graph/__snapshots__/graph_test.ts.snap b/graph/__snapshots__/graph_test.ts.snap index 505c516..05d058a 100644 --- a/graph/__snapshots__/graph_test.ts.snap +++ b/graph/__snapshots__/graph_test.ts.snap @@ -4,9 +4,9 @@ snapshot[`declDepsToGraph() converts declDeps into valid graph 1`] = ` { "/a.ts": { "a (/a.ts:1:14)": [ - "AliasedImport (/d.tsx:8:14)", - "aaa (/a.ts:3:14)", "Comp (/b.tsx:4:14)", + "aaa (/a.ts:3:14)", + "AliasedImport (/d.tsx:8:14)", ], "aaa (/a.ts:3:14)": [ "CompAAA (/b.tsx:5:14)", diff --git a/graph/__snapshots__/top_decl_deps_test.ts.snap b/graph/__snapshots__/top_decl_deps_test.ts.snap index 9a65ec1..6289aea 100644 --- a/graph/__snapshots__/top_decl_deps_test.ts.snap +++ b/graph/__snapshots__/top_decl_deps_test.ts.snap @@ -2,18 +2,18 @@ export const snapshot = {}; snapshot[`getTopDeclDeps() converts graph into valid TopDeclDeps 1`] = ` { - "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", + "vscode://file/b.tsx:5:14?kind=VariableDeclaration&name=CompAAA#L5-L5": [ + "vscode://file/a.ts:3:14?kind=VariableDeclaration&name=aaa#L3-L3", + "vscode://file/a.ts:1:14?kind=VariableDeclaration&name=a#L1-L1", ], - "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", + "vscode://file/d.tsx:4:14?kind=VariableDeclaration&name=InnerImport#L4-L7": [ + "vscode://file/c.tsx:7:14?kind=VariableDeclaration&name=Page#L7-L7", + "vscode://file/b.tsx:4:14?kind=VariableDeclaration&name=Comp#L4-L4", + "vscode://file/a.ts:1:14?kind=VariableDeclaration&name=a#L1-L1", + "vscode://file/b.tsx:8:14?kind=VariableDeclaration&name=Unrelated#L8-L8", ], - "vscode://file/d.tsx:8:14?kind=VariableDeclaration&name=AliasedImport": [ - "vscode://file/a.ts:1:14?kind=VariableDeclaration&name=a", + "vscode://file/d.tsx:8:14?kind=VariableDeclaration&name=AliasedImport#L8-L8": [ + "vscode://file/a.ts:1:14?kind=VariableDeclaration&name=a#L1-L1", ], } `; diff --git a/graph/github_uri.ts b/graph/github_uri.ts new file mode 100644 index 0000000..3094c3a --- /dev/null +++ b/graph/github_uri.ts @@ -0,0 +1,18 @@ +import { parseVSCodeURI, VSCodeURI } from "./vscode_uri.ts" + +export type GithubURI = + `https://github.com/${string}/${string}/blob/${string}/${string}#L${string}-L${string}` + +type GithubURIOption = { + owner: string + repo: string + commit: string +} + +export const mkToGithubURI = + ({ owner, repo, commit }: GithubURIOption) => (uri: VSCodeURI): GithubURI => { + const { url, path } = parseVSCodeURI(uri) + const hash = url.hash as `#L${string}-L${string}` + + return `https://github.com/${owner}/${repo}/blob/${commit}${path as `/${string}`}${hash}` + } diff --git a/graph/github_uri_test.ts b/graph/github_uri_test.ts new file mode 100644 index 0000000..060be9c --- /dev/null +++ b/graph/github_uri_test.ts @@ -0,0 +1,16 @@ +import { assertEquals } from "../test_deps.ts" +import { mkToGithubURI } from "./github_uri.ts" + +Deno.test("mkToGithubURI() converts VSCodeURI to GithubURI", () => { + const toGithubURI = mkToGithubURI({ + owner: "owner", + repo: "repo", + commit: "main", + }) + const uri = + "vscode://file/foo/bar/baz.ts:1:12?kind=VariableDeclaration&name=baz#L1-L2" + const expected = + "https://github.com/owner/repo/blob/main/foo/bar/baz.ts#L1-L2" + + assertEquals(toGithubURI(uri), expected) +}) diff --git a/graph/vscode_uri.ts b/graph/vscode_uri.ts index 72feb95..38129e4 100644 --- a/graph/vscode_uri.ts +++ b/graph/vscode_uri.ts @@ -23,6 +23,7 @@ export const encodeVSCodeURI = (node: Node): VSCodeURI => { const searchParams = new URLSearchParams({ kind: node.getKindName() }) if (Node.hasName(node)) searchParams.set("name", node.getName()) + const hash = `L${node.getStartLineNumber()}-L${node.getEndLineNumber()}` // console.log(node.getStart(), node.getText(), { // line, @@ -30,7 +31,7 @@ export const encodeVSCodeURI = (node: Node): VSCodeURI => { // kind: node.getKindName(), // }) - return `vscode://file/${path}:${line}:${column}?${searchParams}` + return `vscode://file/${path}:${line}:${column}?${searchParams}#${hash}` } /** diff --git a/graph/vscode_uri_test.ts b/graph/vscode_uri_test.ts index 6fd189b..a95dbea 100644 --- a/graph/vscode_uri_test.ts +++ b/graph/vscode_uri_test.ts @@ -27,7 +27,7 @@ const aURI = encodeVSCodeURI(aDecl) Deno.test("encodeVSCodeURI() encodes to valid URL", () => { const expected = new URL( - "vscode://file/a.ts:1:14?kind=VariableDeclaration&name=a", + "vscode://file/a.ts:1:14?kind=VariableDeclaration&name=a#L1-L1", ) assertEquals(new URL(aURI), expected)