@ctxo/lang-go 0.7.0-alpha.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/dist/index.d.ts +43 -0
- package/dist/index.js +258 -0
- package/dist/index.js.map +1 -0
- package/package.json +52 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Alper Hankendi
|
|
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/dist/index.d.ts
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { ILanguageAdapter, SymbolKind, SymbolNode, GraphEdge, ComplexityMetrics, CtxoLanguagePlugin } from '@ctxo/plugin-api';
|
|
2
|
+
import Parser, { Tree, SyntaxNode } from 'tree-sitter';
|
|
3
|
+
|
|
4
|
+
type Language = Parameters<InstanceType<typeof Parser>['setLanguage']>[0];
|
|
5
|
+
|
|
6
|
+
declare abstract class TreeSitterAdapter implements ILanguageAdapter {
|
|
7
|
+
abstract readonly extensions: readonly string[];
|
|
8
|
+
readonly tier: "syntax";
|
|
9
|
+
protected parser: Parser;
|
|
10
|
+
protected symbolRegistry: Map<string, "function" | "class" | "interface" | "method" | "variable" | "type">;
|
|
11
|
+
constructor(language: Language);
|
|
12
|
+
isSupported(filePath: string): boolean;
|
|
13
|
+
setSymbolRegistry(registry: Map<string, SymbolKind>): void;
|
|
14
|
+
protected parse(source: string): Tree;
|
|
15
|
+
protected buildSymbolId(filePath: string, name: string, kind: SymbolKind): string;
|
|
16
|
+
protected nodeToLineRange(node: SyntaxNode): {
|
|
17
|
+
startLine: number;
|
|
18
|
+
endLine: number;
|
|
19
|
+
startOffset: number;
|
|
20
|
+
endOffset: number;
|
|
21
|
+
};
|
|
22
|
+
protected countCyclomaticComplexity(node: SyntaxNode, branchTypes: string[]): number;
|
|
23
|
+
abstract extractSymbols(filePath: string, source: string): Promise<SymbolNode[]>;
|
|
24
|
+
abstract extractEdges(filePath: string, source: string): Promise<GraphEdge[]>;
|
|
25
|
+
abstract extractComplexity(filePath: string, source: string): Promise<ComplexityMetrics[]>;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
declare class GoAdapter extends TreeSitterAdapter {
|
|
29
|
+
readonly extensions: readonly [".go"];
|
|
30
|
+
constructor();
|
|
31
|
+
extractSymbols(filePath: string, source: string): Promise<SymbolNode[]>;
|
|
32
|
+
extractEdges(filePath: string, source: string): Promise<GraphEdge[]>;
|
|
33
|
+
extractComplexity(filePath: string, source: string): Promise<ComplexityMetrics[]>;
|
|
34
|
+
private isExported;
|
|
35
|
+
private extractReceiverType;
|
|
36
|
+
private extractTypeSymbols;
|
|
37
|
+
private extractImportEdges;
|
|
38
|
+
private findFirstExportedSymbolId;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
declare const plugin: CtxoLanguagePlugin;
|
|
42
|
+
|
|
43
|
+
export { GoAdapter, TreeSitterAdapter, plugin as default, plugin };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,258 @@
|
|
|
1
|
+
// src/go-adapter.ts
|
|
2
|
+
import GoLanguage from "tree-sitter-go";
|
|
3
|
+
|
|
4
|
+
// src/tree-sitter-adapter.ts
|
|
5
|
+
import Parser from "tree-sitter";
|
|
6
|
+
import { extname } from "path";
|
|
7
|
+
var TreeSitterAdapter = class {
|
|
8
|
+
tier = "syntax";
|
|
9
|
+
parser;
|
|
10
|
+
symbolRegistry = /* @__PURE__ */ new Map();
|
|
11
|
+
constructor(language) {
|
|
12
|
+
this.parser = new Parser();
|
|
13
|
+
this.parser.setLanguage(language);
|
|
14
|
+
}
|
|
15
|
+
isSupported(filePath) {
|
|
16
|
+
const ext = extname(filePath).toLowerCase();
|
|
17
|
+
return this.extensions.includes(ext);
|
|
18
|
+
}
|
|
19
|
+
setSymbolRegistry(registry) {
|
|
20
|
+
this.symbolRegistry = registry;
|
|
21
|
+
}
|
|
22
|
+
parse(source) {
|
|
23
|
+
return this.parser.parse(source);
|
|
24
|
+
}
|
|
25
|
+
buildSymbolId(filePath, name, kind) {
|
|
26
|
+
return `${filePath}::${name}::${kind}`;
|
|
27
|
+
}
|
|
28
|
+
nodeToLineRange(node) {
|
|
29
|
+
return {
|
|
30
|
+
startLine: node.startPosition.row,
|
|
31
|
+
endLine: node.endPosition.row,
|
|
32
|
+
startOffset: node.startIndex,
|
|
33
|
+
endOffset: node.endIndex
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
countCyclomaticComplexity(node, branchTypes) {
|
|
37
|
+
let complexity = 1;
|
|
38
|
+
const visit = (n) => {
|
|
39
|
+
if (branchTypes.includes(n.type)) {
|
|
40
|
+
complexity++;
|
|
41
|
+
}
|
|
42
|
+
for (let i = 0; i < n.childCount; i++) {
|
|
43
|
+
visit(n.child(i));
|
|
44
|
+
}
|
|
45
|
+
};
|
|
46
|
+
visit(node);
|
|
47
|
+
return complexity;
|
|
48
|
+
}
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
// src/go-adapter.ts
|
|
52
|
+
var GO_BRANCH_TYPES = [
|
|
53
|
+
"if_statement",
|
|
54
|
+
"for_statement",
|
|
55
|
+
"expression_switch_statement",
|
|
56
|
+
"type_switch_statement",
|
|
57
|
+
"expression_case",
|
|
58
|
+
"type_case",
|
|
59
|
+
"select_statement",
|
|
60
|
+
"communication_case"
|
|
61
|
+
];
|
|
62
|
+
var GoAdapter = class extends TreeSitterAdapter {
|
|
63
|
+
extensions = [".go"];
|
|
64
|
+
constructor() {
|
|
65
|
+
super(GoLanguage);
|
|
66
|
+
}
|
|
67
|
+
async extractSymbols(filePath, source) {
|
|
68
|
+
try {
|
|
69
|
+
const tree = this.parse(source);
|
|
70
|
+
const symbols = [];
|
|
71
|
+
for (let i = 0; i < tree.rootNode.childCount; i++) {
|
|
72
|
+
const node = tree.rootNode.child(i);
|
|
73
|
+
if (node.type === "function_declaration") {
|
|
74
|
+
const name = node.childForFieldName("name")?.text;
|
|
75
|
+
if (!name || !this.isExported(name)) continue;
|
|
76
|
+
const range = this.nodeToLineRange(node);
|
|
77
|
+
symbols.push({
|
|
78
|
+
symbolId: this.buildSymbolId(filePath, name, "function"),
|
|
79
|
+
name,
|
|
80
|
+
kind: "function",
|
|
81
|
+
...range
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
if (node.type === "method_declaration") {
|
|
85
|
+
const methodName = node.childForFieldName("name")?.text;
|
|
86
|
+
if (!methodName || !this.isExported(methodName)) continue;
|
|
87
|
+
const receiverType = this.extractReceiverType(node);
|
|
88
|
+
const qualifiedName = receiverType ? `${receiverType}.${methodName}` : methodName;
|
|
89
|
+
const range = this.nodeToLineRange(node);
|
|
90
|
+
symbols.push({
|
|
91
|
+
symbolId: this.buildSymbolId(filePath, qualifiedName, "method"),
|
|
92
|
+
name: qualifiedName,
|
|
93
|
+
kind: "method",
|
|
94
|
+
...range
|
|
95
|
+
});
|
|
96
|
+
}
|
|
97
|
+
if (node.type === "type_declaration") {
|
|
98
|
+
this.extractTypeSymbols(node, filePath, symbols);
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
return symbols;
|
|
102
|
+
} catch (err) {
|
|
103
|
+
console.error(`[ctxo:go] Symbol extraction failed for ${filePath}: ${err.message}`);
|
|
104
|
+
return [];
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
async extractEdges(filePath, source) {
|
|
108
|
+
try {
|
|
109
|
+
const tree = this.parse(source);
|
|
110
|
+
const edges = [];
|
|
111
|
+
const firstExportedSymbol = this.findFirstExportedSymbolId(tree.rootNode, filePath);
|
|
112
|
+
if (!firstExportedSymbol) return edges;
|
|
113
|
+
for (let i = 0; i < tree.rootNode.childCount; i++) {
|
|
114
|
+
const node = tree.rootNode.child(i);
|
|
115
|
+
if (node.type === "import_declaration") {
|
|
116
|
+
this.extractImportEdges(node, filePath, firstExportedSymbol, edges);
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
return edges;
|
|
120
|
+
} catch (err) {
|
|
121
|
+
console.error(`[ctxo:go] Edge extraction failed for ${filePath}: ${err.message}`);
|
|
122
|
+
return [];
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
async extractComplexity(filePath, source) {
|
|
126
|
+
try {
|
|
127
|
+
const tree = this.parse(source);
|
|
128
|
+
const metrics = [];
|
|
129
|
+
for (let i = 0; i < tree.rootNode.childCount; i++) {
|
|
130
|
+
const node = tree.rootNode.child(i);
|
|
131
|
+
if (node.type === "function_declaration") {
|
|
132
|
+
const name = node.childForFieldName("name")?.text;
|
|
133
|
+
if (!name || !this.isExported(name)) continue;
|
|
134
|
+
metrics.push({
|
|
135
|
+
symbolId: this.buildSymbolId(filePath, name, "function"),
|
|
136
|
+
cyclomatic: this.countCyclomaticComplexity(node, GO_BRANCH_TYPES)
|
|
137
|
+
});
|
|
138
|
+
}
|
|
139
|
+
if (node.type === "method_declaration") {
|
|
140
|
+
const methodName = node.childForFieldName("name")?.text;
|
|
141
|
+
if (!methodName || !this.isExported(methodName)) continue;
|
|
142
|
+
const receiverType = this.extractReceiverType(node);
|
|
143
|
+
const qualifiedName = receiverType ? `${receiverType}.${methodName}` : methodName;
|
|
144
|
+
metrics.push({
|
|
145
|
+
symbolId: this.buildSymbolId(filePath, qualifiedName, "method"),
|
|
146
|
+
cyclomatic: this.countCyclomaticComplexity(node, GO_BRANCH_TYPES)
|
|
147
|
+
});
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
return metrics;
|
|
151
|
+
} catch (err) {
|
|
152
|
+
console.error(`[ctxo:go] Complexity extraction failed for ${filePath}: ${err.message}`);
|
|
153
|
+
return [];
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
// ── Private helpers ─────────────────────────────────────────
|
|
157
|
+
isExported(name) {
|
|
158
|
+
return name.length > 0 && name[0] === name[0].toUpperCase() && name[0] !== name[0].toLowerCase();
|
|
159
|
+
}
|
|
160
|
+
extractReceiverType(methodNode) {
|
|
161
|
+
const params = methodNode.child(1);
|
|
162
|
+
if (params?.type !== "parameter_list") return void 0;
|
|
163
|
+
for (let i = 0; i < params.childCount; i++) {
|
|
164
|
+
const param = params.child(i);
|
|
165
|
+
if (param.type === "parameter_declaration") {
|
|
166
|
+
const typeNode = param.childForFieldName("type");
|
|
167
|
+
if (typeNode) {
|
|
168
|
+
const text = typeNode.text;
|
|
169
|
+
return text.replace(/^\*/, "");
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
return void 0;
|
|
174
|
+
}
|
|
175
|
+
extractTypeSymbols(typeDecl, filePath, symbols) {
|
|
176
|
+
for (let i = 0; i < typeDecl.childCount; i++) {
|
|
177
|
+
const spec = typeDecl.child(i);
|
|
178
|
+
if (spec.type !== "type_spec") continue;
|
|
179
|
+
const name = spec.childForFieldName("name")?.text;
|
|
180
|
+
if (!name || !this.isExported(name)) continue;
|
|
181
|
+
const typeBody = spec.childForFieldName("type");
|
|
182
|
+
let kind = "type";
|
|
183
|
+
if (typeBody?.type === "struct_type") kind = "class";
|
|
184
|
+
else if (typeBody?.type === "interface_type") kind = "interface";
|
|
185
|
+
const range = this.nodeToLineRange(spec);
|
|
186
|
+
symbols.push({
|
|
187
|
+
symbolId: this.buildSymbolId(filePath, name, kind),
|
|
188
|
+
name,
|
|
189
|
+
kind,
|
|
190
|
+
...range
|
|
191
|
+
});
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
extractImportEdges(importDecl, _filePath, fromSymbol, edges) {
|
|
195
|
+
const visit = (node) => {
|
|
196
|
+
if (node.type === "import_spec") {
|
|
197
|
+
const pathNode = node.childForFieldName("path") ?? node.child(0);
|
|
198
|
+
if (pathNode) {
|
|
199
|
+
const importPath = pathNode.text.replace(/"/g, "");
|
|
200
|
+
edges.push({
|
|
201
|
+
from: fromSymbol,
|
|
202
|
+
to: `${importPath}::${importPath.split("/").pop()}::variable`,
|
|
203
|
+
kind: "imports"
|
|
204
|
+
});
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
for (let i = 0; i < node.childCount; i++) {
|
|
208
|
+
visit(node.child(i));
|
|
209
|
+
}
|
|
210
|
+
};
|
|
211
|
+
visit(importDecl);
|
|
212
|
+
}
|
|
213
|
+
findFirstExportedSymbolId(rootNode, filePath) {
|
|
214
|
+
for (let i = 0; i < rootNode.childCount; i++) {
|
|
215
|
+
const node = rootNode.child(i);
|
|
216
|
+
if (node.type === "function_declaration") {
|
|
217
|
+
const name = node.childForFieldName("name")?.text;
|
|
218
|
+
if (name && this.isExported(name)) return this.buildSymbolId(filePath, name, "function");
|
|
219
|
+
}
|
|
220
|
+
if (node.type === "type_declaration") {
|
|
221
|
+
for (let j = 0; j < node.childCount; j++) {
|
|
222
|
+
const spec = node.child(j);
|
|
223
|
+
if (spec.type === "type_spec") {
|
|
224
|
+
const name = spec.childForFieldName("name")?.text;
|
|
225
|
+
if (name && this.isExported(name)) {
|
|
226
|
+
const typeBody = spec.childForFieldName("type");
|
|
227
|
+
const kind = typeBody?.type === "struct_type" ? "class" : typeBody?.type === "interface_type" ? "interface" : "type";
|
|
228
|
+
return this.buildSymbolId(filePath, name, kind);
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
return void 0;
|
|
235
|
+
}
|
|
236
|
+
};
|
|
237
|
+
|
|
238
|
+
// src/index.ts
|
|
239
|
+
var VERSION = "0.7.0-alpha.0";
|
|
240
|
+
var plugin = {
|
|
241
|
+
apiVersion: "1",
|
|
242
|
+
id: "go",
|
|
243
|
+
name: "Go (tree-sitter)",
|
|
244
|
+
version: VERSION,
|
|
245
|
+
extensions: [".go"],
|
|
246
|
+
tier: "syntax",
|
|
247
|
+
createAdapter(_ctx) {
|
|
248
|
+
return new GoAdapter();
|
|
249
|
+
}
|
|
250
|
+
};
|
|
251
|
+
var index_default = plugin;
|
|
252
|
+
export {
|
|
253
|
+
GoAdapter,
|
|
254
|
+
TreeSitterAdapter,
|
|
255
|
+
index_default as default,
|
|
256
|
+
plugin
|
|
257
|
+
};
|
|
258
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/go-adapter.ts","../src/tree-sitter-adapter.ts","../src/index.ts"],"sourcesContent":["import GoLanguage from 'tree-sitter-go';\nimport type { SyntaxNode } from 'tree-sitter';\nimport { TreeSitterAdapter } from './tree-sitter-adapter.js';\nimport type { SymbolNode, GraphEdge, ComplexityMetrics } from '@ctxo/plugin-api';\n\nconst GO_BRANCH_TYPES = [\n 'if_statement', 'for_statement',\n 'expression_switch_statement', 'type_switch_statement',\n 'expression_case', 'type_case',\n 'select_statement', 'communication_case',\n];\n\nexport class GoAdapter extends TreeSitterAdapter {\n readonly extensions = ['.go'] as const;\n\n constructor() {\n super(GoLanguage);\n }\n\n async extractSymbols(filePath: string, source: string): Promise<SymbolNode[]> {\n try {\n const tree = this.parse(source);\n const symbols: SymbolNode[] = [];\n\n for (let i = 0; i < tree.rootNode.childCount; i++) {\n const node = tree.rootNode.child(i)!;\n\n if (node.type === 'function_declaration') {\n const name = node.childForFieldName('name')?.text;\n if (!name || !this.isExported(name)) continue;\n const range = this.nodeToLineRange(node);\n symbols.push({\n symbolId: this.buildSymbolId(filePath, name, 'function'),\n name,\n kind: 'function',\n ...range,\n });\n }\n\n if (node.type === 'method_declaration') {\n const methodName = node.childForFieldName('name')?.text;\n if (!methodName || !this.isExported(methodName)) continue;\n const receiverType = this.extractReceiverType(node);\n const qualifiedName = receiverType ? `${receiverType}.${methodName}` : methodName;\n const range = this.nodeToLineRange(node);\n symbols.push({\n symbolId: this.buildSymbolId(filePath, qualifiedName, 'method'),\n name: qualifiedName,\n kind: 'method',\n ...range,\n });\n }\n\n if (node.type === 'type_declaration') {\n this.extractTypeSymbols(node, filePath, symbols);\n }\n }\n\n return symbols;\n } catch (err) {\n console.error(`[ctxo:go] Symbol extraction failed for ${filePath}: ${(err as Error).message}`);\n return [];\n }\n }\n\n async extractEdges(filePath: string, source: string): Promise<GraphEdge[]> {\n try {\n const tree = this.parse(source);\n const edges: GraphEdge[] = [];\n const firstExportedSymbol = this.findFirstExportedSymbolId(tree.rootNode, filePath);\n if (!firstExportedSymbol) return edges;\n\n for (let i = 0; i < tree.rootNode.childCount; i++) {\n const node = tree.rootNode.child(i)!;\n\n if (node.type === 'import_declaration') {\n this.extractImportEdges(node, filePath, firstExportedSymbol, edges);\n }\n }\n\n return edges;\n } catch (err) {\n console.error(`[ctxo:go] Edge extraction failed for ${filePath}: ${(err as Error).message}`);\n return [];\n }\n }\n\n async extractComplexity(filePath: string, source: string): Promise<ComplexityMetrics[]> {\n try {\n const tree = this.parse(source);\n const metrics: ComplexityMetrics[] = [];\n\n for (let i = 0; i < tree.rootNode.childCount; i++) {\n const node = tree.rootNode.child(i)!;\n\n if (node.type === 'function_declaration') {\n const name = node.childForFieldName('name')?.text;\n if (!name || !this.isExported(name)) continue;\n metrics.push({\n symbolId: this.buildSymbolId(filePath, name, 'function'),\n cyclomatic: this.countCyclomaticComplexity(node, GO_BRANCH_TYPES),\n });\n }\n\n if (node.type === 'method_declaration') {\n const methodName = node.childForFieldName('name')?.text;\n if (!methodName || !this.isExported(methodName)) continue;\n const receiverType = this.extractReceiverType(node);\n const qualifiedName = receiverType ? `${receiverType}.${methodName}` : methodName;\n metrics.push({\n symbolId: this.buildSymbolId(filePath, qualifiedName, 'method'),\n cyclomatic: this.countCyclomaticComplexity(node, GO_BRANCH_TYPES),\n });\n }\n }\n\n return metrics;\n } catch (err) {\n console.error(`[ctxo:go] Complexity extraction failed for ${filePath}: ${(err as Error).message}`);\n return [];\n }\n }\n\n // ── Private helpers ─────────────────────────────────────────\n\n private isExported(name: string): boolean {\n return name.length > 0 && name[0]! === name[0]!.toUpperCase() && name[0]! !== name[0]!.toLowerCase();\n }\n\n private extractReceiverType(methodNode: SyntaxNode): string | undefined {\n // method_declaration has parameter_list as first child (receiver)\n const params = methodNode.child(1);\n if (params?.type !== 'parameter_list') return undefined;\n\n for (let i = 0; i < params.childCount; i++) {\n const param = params.child(i)!;\n if (param.type === 'parameter_declaration') {\n // Find type identifier — may be pointer (*Type) or plain (Type)\n const typeNode = param.childForFieldName('type');\n if (typeNode) {\n const text = typeNode.text;\n return text.replace(/^\\*/, '');\n }\n }\n }\n return undefined;\n }\n\n private extractTypeSymbols(typeDecl: SyntaxNode, filePath: string, symbols: SymbolNode[]): void {\n for (let i = 0; i < typeDecl.childCount; i++) {\n const spec = typeDecl.child(i)!;\n if (spec.type !== 'type_spec') continue;\n\n const name = spec.childForFieldName('name')?.text;\n if (!name || !this.isExported(name)) continue;\n\n // Determine kind from the type body\n const typeBody = spec.childForFieldName('type');\n let kind: 'class' | 'interface' | 'type' = 'type';\n if (typeBody?.type === 'struct_type') kind = 'class';\n else if (typeBody?.type === 'interface_type') kind = 'interface';\n\n const range = this.nodeToLineRange(spec);\n symbols.push({\n symbolId: this.buildSymbolId(filePath, name, kind),\n name,\n kind,\n ...range,\n });\n }\n }\n\n private extractImportEdges(\n importDecl: SyntaxNode,\n _filePath: string,\n fromSymbol: string,\n edges: GraphEdge[],\n ): void {\n const visit = (node: SyntaxNode) => {\n if (node.type === 'import_spec') {\n const pathNode = node.childForFieldName('path') ?? node.child(0);\n if (pathNode) {\n const importPath = pathNode.text.replace(/\"/g, '');\n edges.push({\n from: fromSymbol,\n to: `${importPath}::${importPath.split('/').pop()}::variable`,\n kind: 'imports',\n });\n }\n }\n for (let i = 0; i < node.childCount; i++) {\n visit(node.child(i)!);\n }\n };\n visit(importDecl);\n }\n\n private findFirstExportedSymbolId(rootNode: SyntaxNode, filePath: string): string | undefined {\n for (let i = 0; i < rootNode.childCount; i++) {\n const node = rootNode.child(i)!;\n\n if (node.type === 'function_declaration') {\n const name = node.childForFieldName('name')?.text;\n if (name && this.isExported(name)) return this.buildSymbolId(filePath, name, 'function');\n }\n if (node.type === 'type_declaration') {\n for (let j = 0; j < node.childCount; j++) {\n const spec = node.child(j)!;\n if (spec.type === 'type_spec') {\n const name = spec.childForFieldName('name')?.text;\n if (name && this.isExported(name)) {\n const typeBody = spec.childForFieldName('type');\n const kind = typeBody?.type === 'struct_type' ? 'class' : typeBody?.type === 'interface_type' ? 'interface' : 'type';\n return this.buildSymbolId(filePath, name, kind);\n }\n }\n }\n }\n }\n return undefined;\n }\n}\n","import Parser from 'tree-sitter';\nimport type { Tree, SyntaxNode } from 'tree-sitter';\ntype Language = Parameters<InstanceType<typeof Parser>['setLanguage']>[0];\nimport { extname } from 'node:path';\nimport type { SymbolNode, GraphEdge, ComplexityMetrics, SymbolKind, ILanguageAdapter } from '@ctxo/plugin-api';\n\nexport abstract class TreeSitterAdapter implements ILanguageAdapter {\n abstract readonly extensions: readonly string[];\n readonly tier = 'syntax' as const;\n\n protected parser: Parser;\n protected symbolRegistry = new Map<string, SymbolKind>();\n\n constructor(language: Language) {\n this.parser = new Parser();\n this.parser.setLanguage(language);\n }\n\n isSupported(filePath: string): boolean {\n const ext = extname(filePath).toLowerCase();\n return (this.extensions as readonly string[]).includes(ext);\n }\n\n setSymbolRegistry(registry: Map<string, SymbolKind>): void {\n this.symbolRegistry = registry;\n }\n\n protected parse(source: string): Tree {\n return this.parser.parse(source);\n }\n\n protected buildSymbolId(filePath: string, name: string, kind: SymbolKind): string {\n return `${filePath}::${name}::${kind}`;\n }\n\n protected nodeToLineRange(node: SyntaxNode): {\n startLine: number;\n endLine: number;\n startOffset: number;\n endOffset: number;\n } {\n return {\n startLine: node.startPosition.row,\n endLine: node.endPosition.row,\n startOffset: node.startIndex,\n endOffset: node.endIndex,\n };\n }\n\n protected countCyclomaticComplexity(node: SyntaxNode, branchTypes: string[]): number {\n let complexity = 1;\n const visit = (n: SyntaxNode) => {\n if (branchTypes.includes(n.type)) {\n complexity++;\n }\n for (let i = 0; i < n.childCount; i++) {\n visit(n.child(i)!);\n }\n };\n visit(node);\n return complexity;\n }\n\n abstract extractSymbols(filePath: string, source: string): Promise<SymbolNode[]>;\n abstract extractEdges(filePath: string, source: string): Promise<GraphEdge[]>;\n abstract extractComplexity(filePath: string, source: string): Promise<ComplexityMetrics[]>;\n}\n","import type { CtxoLanguagePlugin, PluginContext, ILanguageAdapter } from '@ctxo/plugin-api';\nimport { GoAdapter } from './go-adapter.js';\n\nexport { GoAdapter } from './go-adapter.js';\nexport { TreeSitterAdapter } from './tree-sitter-adapter.js';\n\nconst VERSION = '0.7.0-alpha.0';\n\nexport const plugin: CtxoLanguagePlugin = {\n apiVersion: '1',\n id: 'go',\n name: 'Go (tree-sitter)',\n version: VERSION,\n extensions: ['.go'],\n tier: 'syntax',\n createAdapter(_ctx: PluginContext): ILanguageAdapter {\n return new GoAdapter();\n },\n};\n\nexport default plugin;\n"],"mapp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AS,aAAa;AAC7B,kBAAM,OAAO,KAAK,kBAAkB,MAAM,GAAG;AAC7C,gBAAI,QAAQ,KAAK,WAAW,IAAI,GAAG;AACjC,oBAAM,WAAW,KAAK,kBAAkB,MAAM;AAC9C,oBAAM,OAAO,UAAU,SAAS,gBAAgB,UAAU,UAAU,SAAS,mBAAmB,cAAc;AAC9G,qBAAO,KAAK,cAAc,UAAU,MAAM,IAAI;AAAA,YAChD;AAAA,UACF;AAAA,QACF;AAAA,MACF;AAAA,IACF;AACA,WAAO;AAAA,EACT;AACF;;;AEvNA,IAAM,UAAU;AAET,IAAM,SAA6B;AAAA,EACxC,YAAY;AAAA,EACZ,IAAI;AAAA,EACJ,MAAM;AAAA,EACN,SAAS;AAAA,EACT,YAAY,CAAC,KAAK;AAAA,EAClB,MAAM;AAAA,EACN,cAAc,MAAuC;AACnD,WAAO,IAAI,UAAU;AAAA,EACvB;AACF;AAEA,IAAO,gBAAQ;","names":[]}
|
package/package.json
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@ctxo/lang-go",
|
|
3
|
+
"version": "0.7.0-alpha.0",
|
|
4
|
+
"description": "Ctxo Go language plugin (tree-sitter, syntax tier)",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"engines": {
|
|
7
|
+
"node": ">=20"
|
|
8
|
+
},
|
|
9
|
+
"main": "dist/index.js",
|
|
10
|
+
"types": "dist/index.d.ts",
|
|
11
|
+
"exports": {
|
|
12
|
+
".": {
|
|
13
|
+
"types": "./dist/index.d.ts",
|
|
14
|
+
"import": "./dist/index.js"
|
|
15
|
+
},
|
|
16
|
+
"./package.json": "./package.json"
|
|
17
|
+
},
|
|
18
|
+
"files": [
|
|
19
|
+
"dist/",
|
|
20
|
+
"README.md"
|
|
21
|
+
],
|
|
22
|
+
"keywords": [
|
|
23
|
+
"ctxo",
|
|
24
|
+
"ctxo-plugin",
|
|
25
|
+
"language-plugin",
|
|
26
|
+
"go",
|
|
27
|
+
"golang",
|
|
28
|
+
"tree-sitter"
|
|
29
|
+
],
|
|
30
|
+
"author": "Alper Hankendi",
|
|
31
|
+
"license": "MIT",
|
|
32
|
+
"dependencies": {
|
|
33
|
+
"tree-sitter": "^0.21.1",
|
|
34
|
+
"tree-sitter-go": "^0.23.4"
|
|
35
|
+
},
|
|
36
|
+
"peerDependencies": {
|
|
37
|
+
"@ctxo/plugin-api": "^0.7.0-alpha.0"
|
|
38
|
+
},
|
|
39
|
+
"devDependencies": {
|
|
40
|
+
"@types/node": "^22.15.3",
|
|
41
|
+
"tsup": "^8.4.0",
|
|
42
|
+
"typescript": "^5.8.3",
|
|
43
|
+
"vitest": "^3.1.3",
|
|
44
|
+
"@ctxo/plugin-api": "0.7.0-alpha.0"
|
|
45
|
+
},
|
|
46
|
+
"scripts": {
|
|
47
|
+
"build": "tsup",
|
|
48
|
+
"typecheck": "tsc --noEmit",
|
|
49
|
+
"test": "vitest run",
|
|
50
|
+
"test:unit": "vitest run"
|
|
51
|
+
}
|
|
52
|
+
}
|