Skip to content

Commit

Permalink
feat: GithubURI (#30)
Browse files Browse the repository at this point in the history
* feat: add hash to `VSCodeURI`

* feat: `GithubURI`
  • Loading branch information
scarf005 committed Feb 26, 2024
1 parent 226a311 commit 5422e89
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 14 deletions.
4 changes: 2 additions & 2 deletions graph/__snapshots__/graph_test.ts.snap
Expand Up @@ -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)",
Expand Down
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`] = `
{
"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",
],
}
`;
18 changes: 18 additions & 0 deletions 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}`
}
16 changes: 16 additions & 0 deletions 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)
})
3 changes: 2 additions & 1 deletion graph/vscode_uri.ts
Expand Up @@ -23,14 +23,15 @@ 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,
// column,
// kind: node.getKindName(),
// })

return `vscode://file/${path}:${line}:${column}?${searchParams}`
return `vscode://file/${path}:${line}:${column}?${searchParams}#${hash}`
}

/**
Expand Down
2 changes: 1 addition & 1 deletion graph/vscode_uri_test.ts
Expand Up @@ -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)
Expand Down

0 comments on commit 5422e89

Please sign in to comment.