@codyn06/cboard 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +68 -2
- package/THIRD_PARTY_NOTICES +29 -0
- package/bin/cboard-context.js +15 -0
- package/bin/cboard.js +5 -0
- package/dist/adapters/bash.d.ts +2 -0
- package/dist/adapters/bash.js +14 -0
- package/dist/adapters/c-family.d.ts +2 -0
- package/dist/adapters/c-family.js +81 -0
- package/dist/adapters/csharp.d.ts +2 -0
- package/dist/adapters/csharp.js +47 -0
- package/dist/adapters/extra-languages.d.ts +3 -0
- package/dist/adapters/extra-languages.js +15 -0
- package/dist/adapters/go.d.ts +2 -0
- package/dist/adapters/go.js +42 -0
- package/dist/adapters/grammar.d.ts +21 -0
- package/dist/adapters/grammar.js +54 -0
- package/dist/adapters/index.d.ts +20 -0
- package/dist/adapters/index.js +62 -0
- package/dist/adapters/java.d.ts +2 -0
- package/dist/adapters/java.js +36 -0
- package/dist/adapters/kotlin.d.ts +2 -0
- package/dist/adapters/kotlin.js +65 -0
- package/dist/adapters/lua-family.d.ts +2 -0
- package/dist/adapters/lua-family.js +186 -0
- package/dist/adapters/php.d.ts +2 -0
- package/dist/adapters/php.js +28 -0
- package/dist/adapters/python.d.ts +2 -0
- package/dist/adapters/python.js +68 -0
- package/dist/adapters/ruby.d.ts +2 -0
- package/dist/adapters/ruby.js +46 -0
- package/dist/adapters/rust.d.ts +2 -0
- package/dist/adapters/rust.js +85 -0
- package/dist/adapters/swift.d.ts +2 -0
- package/dist/adapters/swift.js +38 -0
- package/dist/adapters/types.d.ts +61 -0
- package/dist/adapters/types.js +1 -0
- package/dist/adapters/typescript.d.ts +2 -0
- package/dist/adapters/typescript.js +52 -0
- package/dist/api.d.ts +50 -0
- package/dist/api.js +421 -0
- package/dist/cboard.d.ts +2 -0
- package/dist/cboard.js +97 -0
- package/dist/cli.d.ts +27 -0
- package/dist/cli.js +180 -0
- package/dist/docs-parse.d.ts +64 -0
- package/dist/docs-parse.js +762 -0
- package/dist/extract.d.ts +36 -0
- package/dist/extract.js +458 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.js +5 -0
- package/dist/languages.d.ts +9 -0
- package/dist/languages.js +128 -0
- package/dist/model.d.ts +85 -0
- package/dist/model.js +1 -0
- package/grammars/LICENSES/README.md +3 -0
- package/grammars/LICENSES/c.LICENSE +21 -0
- package/grammars/LICENSES/kotlin.LICENSE +21 -0
- package/grammars/LICENSES/lua.LICENSE +21 -0
- package/grammars/LICENSES/luau.LICENSE +21 -0
- package/grammars/LICENSES/swift.LICENSE +21 -0
- package/grammars/manifest.json +39 -0
- package/grammars/tree-sitter-c.wasm +0 -0
- package/grammars/tree-sitter-kotlin.wasm +0 -0
- package/grammars/tree-sitter-lua.wasm +0 -0
- package/grammars/tree-sitter-luau.wasm +0 -0
- package/grammars/tree-sitter-swift.wasm +0 -0
- package/kit/cboard-connect.js +490 -0
- package/kit/cboard-kit/brief.js +386 -0
- package/kit/cboard-kit/config.js +215 -0
- package/kit/cboard-kit/connect-check.js +52 -0
- package/kit/cboard-kit/headers.js +25 -0
- package/kit/cboard-kit/mcp.js +62 -0
- package/kit/cboard-kit/skill/SKILL.md +154 -0
- package/package.json +53 -4
- package/queries/bash/imports.scm +11 -0
- package/queries/bash/symbols.scm +1 -0
- package/queries/c/imports.scm +2 -0
- package/queries/c/symbols.scm +10 -0
- package/queries/cpp/imports.scm +2 -0
- package/queries/cpp/symbols.scm +11 -0
- package/queries/csharp/imports.scm +2 -0
- package/queries/csharp/symbols.scm +10 -0
- package/queries/go/imports.scm +2 -0
- package/queries/go/symbols.scm +8 -0
- package/queries/java/imports.scm +2 -0
- package/queries/java/symbols.scm +9 -0
- package/queries/javascript/imports.scm +3 -0
- package/queries/javascript/symbols.scm +16 -0
- package/queries/kotlin/imports.scm +2 -0
- package/queries/kotlin/symbols.scm +4 -0
- package/queries/lua/imports.scm +5 -0
- package/queries/lua/symbols.scm +6 -0
- package/queries/luau/imports.scm +5 -0
- package/queries/luau/symbols.scm +9 -0
- package/queries/php/imports.scm +6 -0
- package/queries/php/symbols.scm +7 -0
- package/queries/python/imports.scm +3 -0
- package/queries/python/symbols.scm +8 -0
- package/queries/ruby/imports.scm +2 -0
- package/queries/ruby/symbols.scm +4 -0
- package/queries/rust/imports.scm +5 -0
- package/queries/rust/symbols.scm +11 -0
- package/queries/swift/imports.scm +2 -0
- package/queries/swift/symbols.scm +5 -0
- package/queries/typescript/imports.scm +3 -0
- package/queries/typescript/symbols.scm +20 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 codyn06
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,69 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @codyn06/cboard
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Command-line client for [cboard](https://github.com/codyn06/cboard) project graphs. Run it in any
|
|
4
|
+
repository: it connects the repository to a cboard project, syncs the repository's code layer to the
|
|
5
|
+
graph, and checks that the connection works.
|
|
6
|
+
|
|
7
|
+
Requires Node 20 or newer. No native modules and no install scripts.
|
|
8
|
+
|
|
9
|
+
```sh
|
|
10
|
+
npx @codyn06/cboard connect --project <project-uuid> # once per repository; rerun to update the kit
|
|
11
|
+
npx @codyn06/cboard sync # sync the committed code to the graph
|
|
12
|
+
npx @codyn06/cboard check # is the token accepted, is the project visible?
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
Or install it once and use the `cboard` command directly:
|
|
16
|
+
|
|
17
|
+
```sh
|
|
18
|
+
npm install --global @codyn06/cboard
|
|
19
|
+
cboard connect --project <project-uuid>
|
|
20
|
+
cboard sync
|
|
21
|
+
cboard check
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
`cboard --help` lists the commands, `cboard <command> --help` the flags, `cboard --version` the version.
|
|
25
|
+
|
|
26
|
+
## What `cboard connect` writes
|
|
27
|
+
|
|
28
|
+
`.cboard.json` (server, project, token file), the committed kit in `.cboard/`, the `cboard` entry in
|
|
29
|
+
`.mcp.json`, the SessionStart hooks in `.claude/settings.json`, a marked block in `AGENTS.md`, and the
|
|
30
|
+
`cboard-graph` Claude Code skill. The hooks, the token-header helper and the MCP launcher run the kit in
|
|
31
|
+
`.cboard/` with `node`, never `npx`, so sessions start fast, work offline and run a version-pinned copy.
|
|
32
|
+
The kit comes from this package: `.cboard/package.json` records the package version that wrote it
|
|
33
|
+
(`version`). Rerun `cboard connect` after upgrading the package to update the kit; it changes nothing else when nothing
|
|
34
|
+
changed. `cboard connect` also keeps the generated files on LF with a marked `# cboard:start` ...
|
|
35
|
+
`# cboard:end` block in `.gitattributes`, and prints the Codex MCP snippet only with `--codex`.
|
|
36
|
+
|
|
37
|
+
## The token
|
|
38
|
+
|
|
39
|
+
Create a token in the cboard web app (the "Agent tokens" button in the project header; scopes
|
|
40
|
+
`graph:read`, `graph:contribute`, `graph:context`) and save it to `.cboard/token` in the repository
|
|
41
|
+
(`connect` adds it to `.gitignore`), or export it as `CBOARD_PAT`. The repository's token file takes
|
|
42
|
+
precedence over `CBOARD_PAT`. The token is only ever sent to the default cboard server or to a server you
|
|
43
|
+
list in `CBOARD_TRUSTED_SERVERS`.
|
|
44
|
+
|
|
45
|
+
---
|
|
46
|
+
|
|
47
|
+
# Deterministic code context
|
|
48
|
+
|
|
49
|
+
Run `npx @codyn06/cboard sync` in a connected repository (`cboard-context` remains as an alias of the same command) (project, server and token come from `.cboard.json` and the repo token file, as in the connect kit; the token order is a non-default `tokenEnv` variable, the repo token file, then `CBOARD_PAT`; the server defaults to the hosted one). Flags override: `--repo PATH` (default: current directory), `--project UUID`, `--server URL`, `--token-env NAME`, `--state PATH`.
|
|
50
|
+
|
|
51
|
+
The PAT needs `graph:read` and `graph:context`. Credentials are read from the environment. Scan roots must be clean. The default roots are root package.json, packages/, apps/ and prototypes/mvp/. Only tracked commit blobs are parsed; ignored local files, generated declarations and dependency/output/data directories are excluded. Syntax errors abort before submission.
|
|
52
|
+
|
|
53
|
+
Runtime: web-tree-sitter **0.27.1** (MIT), with @vscode/tree-sitter-wasm **0.3.1** (MIT). The bundled TypeScript and JavaScript grammars are MIT; upstream package metadata was checked during installation. The earlier tree-sitter-wasms 0.1.13 bundle was incompatible and is not a dependency. Real TS/TSX/JS/JSX parse tests pass.
|
|
54
|
+
|
|
55
|
+
Repository, modules/directories, files, symbols, manifest configs and dependencies carry repository/commit/path/line provenance. Symbol bodies contain bounded source excerpts, with explicit truncation metadata; no semantic/model summaries are generated. Relative file imports are extracted. Same-file lexical calls, type references and implements relationships are deterministic name heuristics labelled `inferred`; unresolved calls, package aliases, CommonJS and dynamic imports are omitted.
|
|
56
|
+
|
|
57
|
+
Stable repository-scoped keys reconcile against server state. A same-commit rerun creates no revisions. New commits update source metadata; absent nodes and owned relationships are superseded, never deleted. Overload implementations are traversed. Node and edge pagination must complete before reconciliation. Every batch fits the 50-item/200KB budget (batches are cut at 50 items or 180KB, leaving room for the defaults the server's schema adds) and is atomic; a batch that fails with a server error or statement timeout is retried once as two half-size batches, after the server confirms it never recorded the original; successful earlier batches remain if a later batch conflicts. A rerun resumes reconciliation without trusting local receipt state. Reads use the `/v1/graph` HTTP routes; batches are submitted through the `/mcp` `submit_contribution` tool as base64 (`contribution_b64`), because the CDN firewall in front of the hosted service blocks some code-sync payloads (source excerpts that look like attacks). A server without `contribution_b64` support rejects these calls, so release the server before syncing with a newer client.
|
|
58
|
+
|
|
59
|
+
The local marker in Git metadata (`--state PATH` overrides) records the last successful snapshot without storing a token. The server remains authoritative. Scripts/migrate-cboard.ts reuses the reviewed pure Phase A parser, reading committed documentation blobs and translating acceptance rows and scoped amendments to the approved vocabulary. It never imports prototype application/server code or writes a store directly.
|
|
60
|
+
|
|
61
|
+
## Adding a language
|
|
62
|
+
|
|
63
|
+
Languages are parsed by an adapter (`src/adapters/`), found by the language name from the table in `src/languages.ts`. A language without an adapter still gets file nodes. To add one:
|
|
64
|
+
|
|
65
|
+
1. **Grammar.** Use a wasm from `@vscode/tree-sitter-wasm` (`grammar: { builtin: "name" }`), or vendor one: put the file in `grammars/`, add an entry to `grammars/manifest.json` (`file`, `source`, `version`, `sha256`, `licence`), copy the licence text into `grammars/LICENSES/`, and use `grammar: { vendored: "name" }`. The loader refuses a file whose sha256 differs from the manifest.
|
|
66
|
+
2. **Queries.** Add `queries/<dir>/symbols.scm` (capture `@symbol` on declarations; `@function` marks a function-valued variable) and `queries/<dir>/imports.scm` (capture `@import` on the statement and `@source`, or `@source.string` for a quoted literal). Files are found relative to the module URL, so they work from `src` and from `dist`. Grammars differ: a node type the grammar lacks makes the query fail to compile, so write one query set per grammar family.
|
|
67
|
+
3. **Adapter.** Add a `LanguageAdapter` in `src/adapters/` and register it in `src/adapters/index.ts`. Optional parts: `resolveImport` (turn a specifier into a file of the snapshot, or `undefined`) and `uses` (extra heuristics such as same-file calls; their edges are labelled `inferred`).
|
|
68
|
+
4. **Language table.** Make sure `src/languages.ts` maps the extensions (and shebang interpreters) to the language name the adapter is registered under.
|
|
69
|
+
5. **Fixture test.** Add a small fixture repository and a test that snapshots the extracted nodes and edges (see `tests/unit/code-context-adapters.test.ts`).
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
Third-party notices for @codyn06/cboard
|
|
2
|
+
|
|
3
|
+
@codyn06/cboard is licensed under the MIT licence (see LICENSE). It depends on the packages below,
|
|
4
|
+
which npm installs next to it; each keeps its own licence file in node_modules. Grammars that are
|
|
5
|
+
copied into this package are listed in the last section.
|
|
6
|
+
|
|
7
|
+
web-tree-sitter 0.27.2
|
|
8
|
+
Licence: MIT
|
|
9
|
+
Copyright (c) 2018 Max Brunsfeld
|
|
10
|
+
https://github.com/tree-sitter/tree-sitter
|
|
11
|
+
|
|
12
|
+
@vscode/tree-sitter-wasm 0.3.1
|
|
13
|
+
Licence: MIT
|
|
14
|
+
Copyright (c) Microsoft Corporation
|
|
15
|
+
https://github.com/microsoft/vscode-tree-sitter-wasm
|
|
16
|
+
Its bundled WebAssembly grammars (including the TypeScript, TSX and JavaScript grammars this package
|
|
17
|
+
loads) are built from tree-sitter grammar repositories published under the MIT licence.
|
|
18
|
+
|
|
19
|
+
Vendored grammars (wasm files in grammars/, licence texts in grammars/LICENSES/, hashes in grammars/manifest.json)
|
|
20
|
+
tree-sitter-luau 1.2.0 (npm:tree-sitter-luau)
|
|
21
|
+
Licence: MIT, Copyright (c) 2023 Amaan Qureshi
|
|
22
|
+
tree-sitter-lua 0.4.1 (npm:@tree-sitter-grammars/tree-sitter-lua)
|
|
23
|
+
Licence: MIT, Copyright (c) 2021 Munif Tanjim
|
|
24
|
+
tree-sitter-c 0.24.1 (npm:tree-sitter-c)
|
|
25
|
+
Licence: MIT, Copyright (c) 2014 Max Brunsfeld
|
|
26
|
+
tree-sitter-kotlin 1.1.0 (npm:@tree-sitter-grammars/tree-sitter-kotlin)
|
|
27
|
+
Licence: MIT, Copyright (c) 2024 Amaan Qureshi
|
|
28
|
+
tree-sitter-swift 0.7.3 (https://github.com/alex-pinkus/tree-sitter-swift, release asset)
|
|
29
|
+
Licence: MIT, Copyright (c) 2021 alex-pinkus
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
/* global process, console */
|
|
3
|
+
// Tracked wrapper exists during npm ci, before TypeScript emits dist/cli.js.
|
|
4
|
+
import { sync } from "../dist/cli.js";
|
|
5
|
+
if (process.argv[2] !== "sync") {
|
|
6
|
+
console.error(
|
|
7
|
+
"Usage: cboard-context sync --repo PATH [--project UUID --server URL --token-env CBOARD_PAT]",
|
|
8
|
+
);
|
|
9
|
+
process.exitCode = 1;
|
|
10
|
+
} else {
|
|
11
|
+
await sync(process.argv.slice(3)).catch((error) => {
|
|
12
|
+
console.error(error instanceof Error ? error.message : "Context sync failed");
|
|
13
|
+
process.exitCode = 1;
|
|
14
|
+
});
|
|
15
|
+
}
|
package/bin/cboard.js
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { posix } from "node:path";
|
|
2
|
+
/** `source x` / `. x`: next to the sourcing script, then from the repository root. Variables do not resolve. */
|
|
3
|
+
const resolveImport = (source, fromPath, files) => {
|
|
4
|
+
if (/[$`*?~]/.test(source))
|
|
5
|
+
return undefined;
|
|
6
|
+
return [posix.join(posix.dirname(fromPath), source), posix.normalize(source)].find((candidate) => files.has(candidate));
|
|
7
|
+
};
|
|
8
|
+
// Every shell function is visible to whoever sources the file.
|
|
9
|
+
export const bashAdapter = {
|
|
10
|
+
grammar: { builtin: "bash" },
|
|
11
|
+
queries: "bash",
|
|
12
|
+
isExported: () => true,
|
|
13
|
+
resolveImport,
|
|
14
|
+
};
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import { posix } from "node:path";
|
|
2
|
+
// Parents that make a declaration file-level (or a class member); anything else is a local.
|
|
3
|
+
const CONTAINERS = new Set([
|
|
4
|
+
"translation_unit",
|
|
5
|
+
"declaration_list",
|
|
6
|
+
"field_declaration_list",
|
|
7
|
+
"preproc_if",
|
|
8
|
+
"preproc_ifdef",
|
|
9
|
+
"preproc_else",
|
|
10
|
+
"preproc_elif",
|
|
11
|
+
"template_declaration",
|
|
12
|
+
"linkage_specification",
|
|
13
|
+
]);
|
|
14
|
+
const KINDS = {
|
|
15
|
+
struct_specifier: "struct",
|
|
16
|
+
union_specifier: "union",
|
|
17
|
+
enum_specifier: "enum",
|
|
18
|
+
class_specifier: "class",
|
|
19
|
+
type_definition: "typedef",
|
|
20
|
+
alias_declaration: "typedef",
|
|
21
|
+
namespace_definition: "namespace",
|
|
22
|
+
};
|
|
23
|
+
/** Innermost name of a declarator chain, and whether the chain passes a function declarator. */
|
|
24
|
+
function declarator(node) {
|
|
25
|
+
let current = node;
|
|
26
|
+
let isFunction = false;
|
|
27
|
+
while (current) {
|
|
28
|
+
if (current.type === "function_declarator")
|
|
29
|
+
isFunction = true;
|
|
30
|
+
const next = current.childForFieldName("declarator") ??
|
|
31
|
+
(current.type === "parenthesized_declarator" ? current.firstNamedChild : null);
|
|
32
|
+
if (!next)
|
|
33
|
+
break;
|
|
34
|
+
current = next;
|
|
35
|
+
}
|
|
36
|
+
return { name: current?.text, isFunction };
|
|
37
|
+
}
|
|
38
|
+
/** `Class::method` is named `Class.method`, like the qualified names of the other adapters. */
|
|
39
|
+
const tidy = (name) => name.replace(/\s+/g, "").replace(/^::/, "").replaceAll("::", ".");
|
|
40
|
+
const isStatic = (node) => node.children.some((c) => c.type === "storage_class_specifier" && c.text === "static");
|
|
41
|
+
/**
|
|
42
|
+
* Empty name = not a symbol (a local, a variable, a `struct` that a typedef already names).
|
|
43
|
+
* Exported = not `static` at file scope; methods and types count as exported.
|
|
44
|
+
*/
|
|
45
|
+
function declaration(node) {
|
|
46
|
+
const none = { name: "" };
|
|
47
|
+
const parent = node.parent;
|
|
48
|
+
if (!parent || !CONTAINERS.has(parent.type))
|
|
49
|
+
return none;
|
|
50
|
+
const member = parent.type === "field_declaration_list";
|
|
51
|
+
let name;
|
|
52
|
+
let kind = KINDS[node.type] ?? "function";
|
|
53
|
+
if (node.type === "function_definition" || node.type === "declaration") {
|
|
54
|
+
const found = declarator(node.childForFieldName("declarator"));
|
|
55
|
+
// Prototypes are only kept when they are not class members (the class lists its own).
|
|
56
|
+
if (!found.isFunction || (node.type === "declaration" && member))
|
|
57
|
+
return none;
|
|
58
|
+
name = found.name;
|
|
59
|
+
kind = member ? "method" : "function";
|
|
60
|
+
}
|
|
61
|
+
else if (node.type === "type_definition") {
|
|
62
|
+
name = declarator(node.childForFieldName("declarator")).name;
|
|
63
|
+
}
|
|
64
|
+
else
|
|
65
|
+
name = node.childForFieldName("name")?.text;
|
|
66
|
+
if (!name)
|
|
67
|
+
return none;
|
|
68
|
+
return { name: tidy(name), kind, exported: member || !isStatic(node) };
|
|
69
|
+
}
|
|
70
|
+
/** `#include "x.h"`: next to the including file, then from the repository root, then `include/`. */
|
|
71
|
+
const resolveImport = (source, fromPath, files) => [
|
|
72
|
+
posix.join(posix.dirname(fromPath), source),
|
|
73
|
+
posix.normalize(source),
|
|
74
|
+
posix.join("include", source),
|
|
75
|
+
].find((candidate) => files.has(candidate));
|
|
76
|
+
// shortcut: `.h` always maps to C (see languages.ts); a C++ header is parsed with the C grammar,
|
|
77
|
+
// so one with classes is flagged parseError. Upgrade by sniffing `class`/`namespace`/`template`.
|
|
78
|
+
export const cFamilyAdapters = {
|
|
79
|
+
c: { grammar: { vendored: "c" }, queries: "c", declaration, resolveImport },
|
|
80
|
+
cpp: { grammar: { builtin: "cpp" }, queries: "cpp", declaration, resolveImport },
|
|
81
|
+
};
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
/** Exported means `public`; interface members are public without saying so. */
|
|
2
|
+
function declaration(node) {
|
|
3
|
+
const name = node.childForFieldName("name")?.text;
|
|
4
|
+
if (!name)
|
|
5
|
+
return undefined;
|
|
6
|
+
const exported = node.children.some((c) => c.type === "modifier" && c.text === "public") ||
|
|
7
|
+
node.parent?.parent?.type === "interface_declaration";
|
|
8
|
+
return { name, exported };
|
|
9
|
+
}
|
|
10
|
+
// Namespace name to the files declaring it, built once per snapshot (the `files` set identifies it).
|
|
11
|
+
const indexes = new WeakMap();
|
|
12
|
+
function namespaces(files, read) {
|
|
13
|
+
let index = indexes.get(files);
|
|
14
|
+
if (!index) {
|
|
15
|
+
index = new Map();
|
|
16
|
+
for (const path of [...files].sort())
|
|
17
|
+
if (path.endsWith(".cs"))
|
|
18
|
+
for (const [, name = ""] of (read(path) ?? "").matchAll(/^\s*namespace\s+([\w.]+)/gm))
|
|
19
|
+
index.set(name, [...(index.get(name) ?? []), path]);
|
|
20
|
+
indexes.set(files, index);
|
|
21
|
+
}
|
|
22
|
+
return index;
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* `using A.B;` names a namespace, not a file: the edge goes to every `.cs` file that declares
|
|
26
|
+
* `namespace A.B` (block or file-scoped form), excluding the importing file. For `using static
|
|
27
|
+
* A.B.T;` and `using X = A.B.T;` the target is a type, so the namespace is tried as written and
|
|
28
|
+
* then without its last segment. Namespaces declared in no snapshot file (System, packages) get no edge.
|
|
29
|
+
*/
|
|
30
|
+
function resolveImport(source, fromPath, files, read = () => undefined) {
|
|
31
|
+
const parsed = /^(?:global\s+)?using\s+(static\s+)?(?:\w+\s*=\s*)?(?:global::)?([\w.]+)\s*;/.exec(source.trim());
|
|
32
|
+
if (!parsed)
|
|
33
|
+
return undefined;
|
|
34
|
+
const name = parsed[2] ?? "";
|
|
35
|
+
const index = namespaces(files, read);
|
|
36
|
+
const outer = name.includes(".") ? name.slice(0, name.lastIndexOf(".")) : undefined;
|
|
37
|
+
const candidates = parsed[1] || /using\s+\w+\s*=/.test(source) ? [name, outer] : [name];
|
|
38
|
+
const found = candidates.flatMap((ns) => (ns ? (index.get(ns) ?? []) : []));
|
|
39
|
+
const targets = [...new Set(found)].filter((p) => p !== fromPath);
|
|
40
|
+
return targets.length ? targets : undefined;
|
|
41
|
+
}
|
|
42
|
+
export const csharpAdapter = {
|
|
43
|
+
grammar: { builtin: "c-sharp" },
|
|
44
|
+
queries: "csharp",
|
|
45
|
+
declaration,
|
|
46
|
+
resolveImport,
|
|
47
|
+
};
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { bashAdapter } from "./bash.js";
|
|
2
|
+
import { cFamilyAdapters } from "./c-family.js";
|
|
3
|
+
import { kotlinAdapter } from "./kotlin.js";
|
|
4
|
+
import { phpAdapter } from "./php.js";
|
|
5
|
+
import { rubyAdapter } from "./ruby.js";
|
|
6
|
+
import { swiftAdapter } from "./swift.js";
|
|
7
|
+
/** C, C++, Ruby, PHP, shell (language name "shell"), Kotlin and Swift. */
|
|
8
|
+
export const extraAdapters = {
|
|
9
|
+
...cFamilyAdapters,
|
|
10
|
+
ruby: rubyAdapter,
|
|
11
|
+
php: phpAdapter,
|
|
12
|
+
shell: bashAdapter,
|
|
13
|
+
kotlin: kotlinAdapter,
|
|
14
|
+
swift: swiftAdapter,
|
|
15
|
+
};
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { posix } from "node:path";
|
|
2
|
+
/** Methods are named `Type.Method`; exported means the declared name starts with an upper-case letter. */
|
|
3
|
+
function declaration(node) {
|
|
4
|
+
const name = node.childForFieldName("name")?.text;
|
|
5
|
+
if (!name)
|
|
6
|
+
return undefined;
|
|
7
|
+
const exported = /^\p{Lu}/u.test(name);
|
|
8
|
+
if (node.type !== "method_declaration")
|
|
9
|
+
return { name, exported };
|
|
10
|
+
const receiver = node.childForFieldName("receiver")?.descendantsOfType("type_identifier")[0];
|
|
11
|
+
return { name: receiver ? `${receiver.text}.${name}` : name, exported };
|
|
12
|
+
}
|
|
13
|
+
/** Module path declared by the nearest `go.mod` above the file, with the folder holding it. */
|
|
14
|
+
function nearestModule(fromPath, read) {
|
|
15
|
+
for (let dir = posix.dirname(fromPath);; dir = posix.dirname(dir)) {
|
|
16
|
+
const text = read(posix.join(dir, "go.mod"));
|
|
17
|
+
const module = text && /^\s*module\s+"?([^\s"]+)"?/m.exec(text)?.[1];
|
|
18
|
+
if (module)
|
|
19
|
+
return { module, dir };
|
|
20
|
+
if (dir === ".")
|
|
21
|
+
return undefined;
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* An import path under the module path of the nearest `go.mod` names a package folder. The edge
|
|
26
|
+
* goes to every `.go` file of that folder except `_test.go` files (a package is its files, so no
|
|
27
|
+
* single representative is chosen). Standard-library and external paths get no edge.
|
|
28
|
+
*/
|
|
29
|
+
function resolveImport(source, fromPath, files, read = () => undefined) {
|
|
30
|
+
const found = nearestModule(fromPath, read);
|
|
31
|
+
if (!found || (source !== found.module && !source.startsWith(`${found.module}/`)))
|
|
32
|
+
return undefined;
|
|
33
|
+
const folder = posix.join(found.dir, source.slice(found.module.length));
|
|
34
|
+
const inFolder = [...files].filter((p) => posix.dirname(p) === folder && p.endsWith(".go") && !p.endsWith("_test.go"));
|
|
35
|
+
return inFolder.length ? inFolder.sort() : undefined;
|
|
36
|
+
}
|
|
37
|
+
export const goAdapter = {
|
|
38
|
+
grammar: { builtin: "go" },
|
|
39
|
+
queries: "go",
|
|
40
|
+
declaration,
|
|
41
|
+
resolveImport,
|
|
42
|
+
};
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { Language, Query } from "web-tree-sitter";
|
|
2
|
+
import type { GrammarSource } from "./types.js";
|
|
3
|
+
/** One `grammars/manifest.json` entry: a wasm file shipped in the package, pinned by hash. */
|
|
4
|
+
export interface VendoredGrammar {
|
|
5
|
+
file: string;
|
|
6
|
+
source: string;
|
|
7
|
+
version: string;
|
|
8
|
+
sha256: string;
|
|
9
|
+
licence: string;
|
|
10
|
+
}
|
|
11
|
+
export interface GrammarManifest {
|
|
12
|
+
grammars: Record<string, VendoredGrammar>;
|
|
13
|
+
}
|
|
14
|
+
/** Bytes of a vendored grammar after checking the manifest's sha256; throws on a mismatch. */
|
|
15
|
+
export declare function readVendoredGrammar(language: string, dir?: string, manifest?: GrammarManifest): Buffer;
|
|
16
|
+
/** The loaded grammar, cached per source; the runtime is initialised on first use. */
|
|
17
|
+
export declare function loadGrammar(source: GrammarSource): Promise<Language>;
|
|
18
|
+
/** Text of `queries/<dir>/<file>`. */
|
|
19
|
+
export declare const readQuery: (dir: string, file: string) => string;
|
|
20
|
+
/** Compiled query, cached per language object and query file. */
|
|
21
|
+
export declare function compileQuery(language: Language, dir: string, file: string): Query;
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { createHash } from "node:crypto";
|
|
2
|
+
import { readFileSync } from "node:fs";
|
|
3
|
+
import { createRequire } from "node:module";
|
|
4
|
+
import { fileURLToPath } from "node:url";
|
|
5
|
+
import { Language, Parser, Query } from "web-tree-sitter";
|
|
6
|
+
const require = createRequire(import.meta.url);
|
|
7
|
+
// Resolved from the module URL so it works from `src` and from the published `dist` layout alike.
|
|
8
|
+
const packageFile = (relative) => fileURLToPath(new URL(`../../${relative}`, import.meta.url));
|
|
9
|
+
/** Bytes of a vendored grammar after checking the manifest's sha256; throws on a mismatch. */
|
|
10
|
+
export function readVendoredGrammar(language, dir = packageFile("grammars"), manifest = JSON.parse(readFileSync(`${dir}/manifest.json`, "utf8"))) {
|
|
11
|
+
const entry = manifest.grammars[language];
|
|
12
|
+
if (!entry)
|
|
13
|
+
throw new Error(`No vendored grammar for ${language} in grammars/manifest.json`);
|
|
14
|
+
const bytes = readFileSync(`${dir}/${entry.file}`);
|
|
15
|
+
const actual = createHash("sha256").update(bytes).digest("hex");
|
|
16
|
+
if (actual !== entry.sha256)
|
|
17
|
+
throw new Error(`Vendored grammar ${entry.file} does not match its manifest sha256 (expected ${entry.sha256}, got ${actual})`);
|
|
18
|
+
return bytes;
|
|
19
|
+
}
|
|
20
|
+
let initialized;
|
|
21
|
+
const languages = new Map();
|
|
22
|
+
/** The loaded grammar, cached per source; the runtime is initialised on first use. */
|
|
23
|
+
export async function loadGrammar(source) {
|
|
24
|
+
await (initialized ??= Parser.init());
|
|
25
|
+
const name = "builtin" in source ? source.builtin : source.vendored;
|
|
26
|
+
const id = `${"builtin" in source ? "builtin" : "vendored"}:${name}`;
|
|
27
|
+
let language = languages.get(id);
|
|
28
|
+
if (!language) {
|
|
29
|
+
language = ("builtin" in source
|
|
30
|
+
? Language.load(require.resolve(`@vscode/tree-sitter-wasm/wasm/tree-sitter-${name}.wasm`))
|
|
31
|
+
: Language.load(readVendoredGrammar(name))).catch((cause) => {
|
|
32
|
+
languages.delete(id);
|
|
33
|
+
throw new Error(`Cannot load ${name} WASM grammar; installed grammar/runtime ABI must be compatible`, { cause });
|
|
34
|
+
});
|
|
35
|
+
languages.set(id, language);
|
|
36
|
+
}
|
|
37
|
+
return language;
|
|
38
|
+
}
|
|
39
|
+
/** Text of `queries/<dir>/<file>`. */
|
|
40
|
+
export const readQuery = (dir, file) => readFileSync(packageFile(`queries/${dir}/${file}`), "utf8");
|
|
41
|
+
// A compiled query is bound to its grammar, so tsx and typescript each get their own.
|
|
42
|
+
const queries = new WeakMap();
|
|
43
|
+
/** Compiled query, cached per language object and query file. */
|
|
44
|
+
export function compileQuery(language, dir, file) {
|
|
45
|
+
const forLanguage = queries.get(language) ?? new Map();
|
|
46
|
+
queries.set(language, forLanguage);
|
|
47
|
+
const id = `${dir}/${file}`;
|
|
48
|
+
const cached = forLanguage.get(id);
|
|
49
|
+
if (cached)
|
|
50
|
+
return cached;
|
|
51
|
+
const query = new Query(language, readQuery(dir, file));
|
|
52
|
+
forLanguage.set(id, query);
|
|
53
|
+
return query;
|
|
54
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { Parser, type Node } from "web-tree-sitter";
|
|
2
|
+
import type { LanguageAdapter } from "./types.js";
|
|
3
|
+
export type { GrammarSource, LanguageAdapter, Use, WalkContext } from "./types.js";
|
|
4
|
+
export declare const adapterFor: (language: string) => LanguageAdapter | undefined;
|
|
5
|
+
export declare const parsedLanguages: () => string[];
|
|
6
|
+
/** An import statement found by the adapter's import query. */
|
|
7
|
+
export interface ImportMatch {
|
|
8
|
+
/** Module specifier, quotes stripped. */
|
|
9
|
+
source: string;
|
|
10
|
+
/** Start line (1-based) of the whole statement. */
|
|
11
|
+
line: number;
|
|
12
|
+
}
|
|
13
|
+
/** A parser plus the adapter's queries, ready to run on one file. The caller must `delete()` it. */
|
|
14
|
+
export declare function openAdapter(adapter: LanguageAdapter): Promise<{
|
|
15
|
+
parser: Parser;
|
|
16
|
+
/** Declaration nodes by node id: kind is "function" for `@function` captures, else the node type. */
|
|
17
|
+
symbolNodes: (root: Node) => Map<number, string | undefined>;
|
|
18
|
+
/** Import statements by node id. */
|
|
19
|
+
importNodes: (root: Node) => Map<number, ImportMatch>;
|
|
20
|
+
}>;
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import { Parser } from "web-tree-sitter";
|
|
2
|
+
import { compileQuery, loadGrammar } from "./grammar.js";
|
|
3
|
+
import { csharpAdapter } from "./csharp.js";
|
|
4
|
+
import { goAdapter } from "./go.js";
|
|
5
|
+
import { javaAdapter } from "./java.js";
|
|
6
|
+
import { extraAdapters } from "./extra-languages.js";
|
|
7
|
+
import { pythonAdapter } from "./python.js";
|
|
8
|
+
import { luaAdapters } from "./lua-family.js";
|
|
9
|
+
import { rustAdapter } from "./rust.js";
|
|
10
|
+
import { typescriptAdapters } from "./typescript.js";
|
|
11
|
+
/** Adapters by language name (the values of the table in languages.ts). One entry per parsed language. */
|
|
12
|
+
const adapters = {
|
|
13
|
+
...typescriptAdapters,
|
|
14
|
+
python: pythonAdapter,
|
|
15
|
+
...luaAdapters,
|
|
16
|
+
go: goAdapter,
|
|
17
|
+
rust: rustAdapter,
|
|
18
|
+
java: javaAdapter,
|
|
19
|
+
csharp: csharpAdapter,
|
|
20
|
+
...extraAdapters,
|
|
21
|
+
};
|
|
22
|
+
export const adapterFor = (language) => adapters[language];
|
|
23
|
+
export const parsedLanguages = () => Object.keys(adapters);
|
|
24
|
+
/** A parser plus the adapter's queries, ready to run on one file. The caller must `delete()` it. */
|
|
25
|
+
export async function openAdapter(adapter) {
|
|
26
|
+
const language = await loadGrammar(adapter.grammar);
|
|
27
|
+
const parser = new Parser();
|
|
28
|
+
parser.setLanguage(language);
|
|
29
|
+
const symbols = compileQuery(language, adapter.queries, "symbols.scm");
|
|
30
|
+
const imports = compileQuery(language, adapter.queries, "imports.scm");
|
|
31
|
+
return {
|
|
32
|
+
parser,
|
|
33
|
+
/** Declaration nodes by node id: kind is "function" for `@function` captures, else the node type. */
|
|
34
|
+
symbolNodes: (root) => {
|
|
35
|
+
const found = new Map();
|
|
36
|
+
for (const match of symbols.matches(root))
|
|
37
|
+
for (const { name, node } of match.captures)
|
|
38
|
+
if (name === "symbol")
|
|
39
|
+
found.set(node.id, undefined);
|
|
40
|
+
else if (name === "function" || name === "type")
|
|
41
|
+
found.set(node.id, name);
|
|
42
|
+
return found;
|
|
43
|
+
},
|
|
44
|
+
/** Import statements by node id. */
|
|
45
|
+
importNodes: (root) => {
|
|
46
|
+
const found = new Map();
|
|
47
|
+
for (const match of imports.matches(root)) {
|
|
48
|
+
const statement = match.captures.find((c) => c.name === "import")?.node;
|
|
49
|
+
const captured = match.captures.find((c) => c.name.startsWith("source"));
|
|
50
|
+
if (!statement || !captured)
|
|
51
|
+
continue;
|
|
52
|
+
found.set(statement.id, {
|
|
53
|
+
source: captured.name === "source.string"
|
|
54
|
+
? captured.node.text.slice(1, -1)
|
|
55
|
+
: captured.node.text,
|
|
56
|
+
line: statement.startPosition.row + 1,
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
return found;
|
|
60
|
+
},
|
|
61
|
+
};
|
|
62
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
/** Exported means `public`; interface members are public without saying so. */
|
|
2
|
+
function declaration(node) {
|
|
3
|
+
const name = node.childForFieldName("name")?.text;
|
|
4
|
+
if (!name)
|
|
5
|
+
return undefined;
|
|
6
|
+
const modifiers = node.children.find((c) => c.type === "modifiers");
|
|
7
|
+
const exported = !!modifiers?.children.some((c) => c.type === "public") ||
|
|
8
|
+
node.parent?.type === "interface_body";
|
|
9
|
+
return { name, exported };
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* `import a.b.C;` (or `import static a.b.C.m;`, or a nested type `a.b.C.D`) resolves to the one
|
|
13
|
+
* file whose path ends with `a/b/C.java`, under any source root (`src/main/java/`, ...). Wildcard,
|
|
14
|
+
* ambiguous and external imports get no edge.
|
|
15
|
+
*/
|
|
16
|
+
function resolveImport(source, _fromPath, files) {
|
|
17
|
+
const name = /^import\s+(?:static\s+)?([\w.]+)\s*;/.exec(source.trim())?.[1];
|
|
18
|
+
if (!name)
|
|
19
|
+
return undefined;
|
|
20
|
+
const parts = name.split(".");
|
|
21
|
+
for (let n = parts.length; n >= 1; n--) {
|
|
22
|
+
const suffix = `${parts.slice(0, n).join("/")}.java`;
|
|
23
|
+
const matches = [...files].filter((p) => p === suffix || p.endsWith(`/${suffix}`));
|
|
24
|
+
if (matches.length === 1)
|
|
25
|
+
return matches[0];
|
|
26
|
+
if (matches.length > 1)
|
|
27
|
+
return undefined;
|
|
28
|
+
}
|
|
29
|
+
return undefined;
|
|
30
|
+
}
|
|
31
|
+
export const javaAdapter = {
|
|
32
|
+
grammar: { builtin: "java" },
|
|
33
|
+
queries: "java",
|
|
34
|
+
declaration,
|
|
35
|
+
resolveImport,
|
|
36
|
+
};
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
const visibility = (node) => node.children
|
|
2
|
+
.find((c) => c.type === "modifiers")
|
|
3
|
+
?.children.find((c) => c.type === "visibility_modifier")?.text;
|
|
4
|
+
function declaration(node) {
|
|
5
|
+
const name = node.childForFieldName("name")?.text;
|
|
6
|
+
if (!name)
|
|
7
|
+
return undefined;
|
|
8
|
+
const modifiers = node.children.find((c) => c.type === "modifiers");
|
|
9
|
+
let kind = "function";
|
|
10
|
+
if (node.type === "object_declaration")
|
|
11
|
+
kind = "object";
|
|
12
|
+
else if (node.type === "class_declaration")
|
|
13
|
+
kind = node.children.some((c) => c.type === "interface")
|
|
14
|
+
? "interface"
|
|
15
|
+
: modifiers?.text.split(/\s+/).includes("enum")
|
|
16
|
+
? "enum"
|
|
17
|
+
: "class";
|
|
18
|
+
else if (node.parent?.type === "class_body" || node.parent?.type === "enum_class_body")
|
|
19
|
+
kind = "method";
|
|
20
|
+
const access = visibility(node);
|
|
21
|
+
return { name, kind, exported: access !== "private" && access !== "internal" };
|
|
22
|
+
}
|
|
23
|
+
// Kotlin sources are indexed by package directory: `a.b.C` is a file ending in `a/b/C.kt`.
|
|
24
|
+
const suffixIndexes = new WeakMap();
|
|
25
|
+
function byBaseName(files) {
|
|
26
|
+
let index = suffixIndexes.get(files);
|
|
27
|
+
if (!index) {
|
|
28
|
+
index = new Map();
|
|
29
|
+
for (const file of files)
|
|
30
|
+
if (file.endsWith(".kt")) {
|
|
31
|
+
const base = file.slice(file.lastIndexOf("/") + 1);
|
|
32
|
+
index.set(base, [...(index.get(base) ?? []), file]);
|
|
33
|
+
}
|
|
34
|
+
suffixIndexes.set(files, index);
|
|
35
|
+
}
|
|
36
|
+
return index;
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* `source` is the whole import. `import a.b.C` resolves to the one file ending in `a/b/C.kt`
|
|
40
|
+
* (also `a.b.C.member` through `C.kt`). Star imports, aliases' targets that match no file and
|
|
41
|
+
* ambiguous matches add no edge.
|
|
42
|
+
*/
|
|
43
|
+
const resolveImport = (source, _from, files) => {
|
|
44
|
+
const match = /^import\s+([\w.`]+?)(\.\*)?\s*(?:as\s+\w+)?\s*;?$/.exec(source.trim());
|
|
45
|
+
if (!match || match[2])
|
|
46
|
+
return undefined;
|
|
47
|
+
const parts = (match[1] ?? "").replaceAll("`", "").split(".");
|
|
48
|
+
const index = byBaseName(files);
|
|
49
|
+
for (let end = parts.length; end >= 2; end--) {
|
|
50
|
+
const suffix = `${parts.slice(0, end).join("/")}.kt`;
|
|
51
|
+
const base = `${parts[end - 1] ?? ""}.kt`;
|
|
52
|
+
const hits = (index.get(base) ?? []).filter((f) => f === suffix || f.endsWith(`/${suffix}`));
|
|
53
|
+
if (hits.length === 1)
|
|
54
|
+
return hits[0];
|
|
55
|
+
if (hits.length > 1)
|
|
56
|
+
return undefined;
|
|
57
|
+
}
|
|
58
|
+
return undefined;
|
|
59
|
+
};
|
|
60
|
+
export const kotlinAdapter = {
|
|
61
|
+
grammar: { vendored: "kotlin" },
|
|
62
|
+
queries: "kotlin",
|
|
63
|
+
declaration,
|
|
64
|
+
resolveImport,
|
|
65
|
+
};
|