@abhinav2203/codeflow-core 0.1.1 → 1.0.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/dist/analyzer/index.d.ts +2 -0
- package/dist/analyzer/index.js +1 -0
- package/dist/analyzer/repo-multi.d.ts +6 -0
- package/dist/analyzer/repo-multi.js +246 -0
- package/dist/analyzer/repo-multi.test.d.ts +1 -0
- package/dist/analyzer/repo-multi.test.js +208 -0
- package/dist/analyzer/test-hook-check.d.ts +1 -0
- package/dist/analyzer/test-hook-check.js +2 -0
- package/dist/analyzer/tree-sitter-analyzer.d.ts +34 -0
- package/dist/analyzer/tree-sitter-analyzer.js +617 -0
- package/dist/analyzer/tree-sitter-loader.d.ts +8 -0
- package/dist/analyzer/tree-sitter-loader.js +97 -0
- package/dist/analyzer/tree-sitter-queries.d.ts +10 -0
- package/dist/analyzer/tree-sitter-queries.js +285 -0
- package/package.json +13 -2
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
import { createRequire } from "node:module";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import { fileURLToPath } from "node:url";
|
|
4
|
+
import { Language, Parser } from "web-tree-sitter";
|
|
5
|
+
const LANGUAGE_EXTENSIONS = {
|
|
6
|
+
go: [".go"],
|
|
7
|
+
python: [".py"],
|
|
8
|
+
c: [".c", ".h"],
|
|
9
|
+
cpp: [".cpp", ".cc", ".cxx", ".hpp"],
|
|
10
|
+
rust: [".rs"],
|
|
11
|
+
typescript: [".ts", ".tsx"],
|
|
12
|
+
javascript: [".js", ".jsx"]
|
|
13
|
+
};
|
|
14
|
+
const EXTENSION_TO_LANGUAGE = new Map();
|
|
15
|
+
for (const [lang, exts] of Object.entries(LANGUAGE_EXTENSIONS)) {
|
|
16
|
+
for (const ext of exts) {
|
|
17
|
+
EXTENSION_TO_LANGUAGE.set(ext, lang);
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
const GRAMMAR_PACKAGE_NAMES = {
|
|
21
|
+
go: "tree-sitter-go",
|
|
22
|
+
python: "tree-sitter-python",
|
|
23
|
+
c: "tree-sitter-c",
|
|
24
|
+
cpp: "tree-sitter-cpp",
|
|
25
|
+
rust: "tree-sitter-rust",
|
|
26
|
+
typescript: "tree-sitter-typescript",
|
|
27
|
+
javascript: "tree-sitter-javascript"
|
|
28
|
+
};
|
|
29
|
+
const WASM_FILE_NAMES = {
|
|
30
|
+
go: "tree-sitter-go.wasm",
|
|
31
|
+
python: "tree-sitter-python.wasm",
|
|
32
|
+
c: "tree-sitter-c.wasm",
|
|
33
|
+
cpp: "tree-sitter-cpp.wasm",
|
|
34
|
+
rust: "tree-sitter-rust.wasm",
|
|
35
|
+
typescript: "tree-sitter-typescript.wasm",
|
|
36
|
+
javascript: "tree-sitter-javascript.wasm"
|
|
37
|
+
};
|
|
38
|
+
let parserInitialized = false;
|
|
39
|
+
const initParser = async () => {
|
|
40
|
+
if (parserInitialized) {
|
|
41
|
+
return;
|
|
42
|
+
}
|
|
43
|
+
await Parser.init();
|
|
44
|
+
parserInitialized = true;
|
|
45
|
+
};
|
|
46
|
+
const loadedGrammars = new Map();
|
|
47
|
+
const resolveWasmPath = (lang) => {
|
|
48
|
+
const packageName = GRAMMAR_PACKAGE_NAMES[lang];
|
|
49
|
+
const wasmFileName = WASM_FILE_NAMES[lang];
|
|
50
|
+
try {
|
|
51
|
+
// Use createRequire for ESM compatibility
|
|
52
|
+
const require = createRequire(import.meta.url);
|
|
53
|
+
const packagePath = require.resolve(`${packageName}/package.json`);
|
|
54
|
+
const packageDir = path.dirname(packagePath);
|
|
55
|
+
return path.join(packageDir, wasmFileName);
|
|
56
|
+
}
|
|
57
|
+
catch {
|
|
58
|
+
// Fallback to import.meta.resolve if available (Node 20.6+)
|
|
59
|
+
try {
|
|
60
|
+
const packageUrl = import.meta.resolve(`${packageName}/package.json`);
|
|
61
|
+
const packagePath = fileURLToPath(packageUrl);
|
|
62
|
+
const packageDir = path.dirname(packagePath);
|
|
63
|
+
return path.join(packageDir, wasmFileName);
|
|
64
|
+
}
|
|
65
|
+
catch {
|
|
66
|
+
throw new Error(`Cannot resolve WASM file for language "${lang}". ` +
|
|
67
|
+
`Ensure "${packageName}" is installed.`);
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
};
|
|
71
|
+
export const loadLanguage = async (lang) => {
|
|
72
|
+
if (loadedGrammars.has(lang)) {
|
|
73
|
+
return loadedGrammars.get(lang);
|
|
74
|
+
}
|
|
75
|
+
await initParser();
|
|
76
|
+
const wasmPath = resolveWasmPath(lang);
|
|
77
|
+
const language = await Language.load(wasmPath);
|
|
78
|
+
loadedGrammars.set(lang, language);
|
|
79
|
+
return language;
|
|
80
|
+
};
|
|
81
|
+
export const createParser = async (lang) => {
|
|
82
|
+
await initParser();
|
|
83
|
+
const parser = new Parser();
|
|
84
|
+
const language = await loadLanguage(lang);
|
|
85
|
+
parser.setLanguage(language);
|
|
86
|
+
return parser;
|
|
87
|
+
};
|
|
88
|
+
export const extensionToLanguage = (ext) => EXTENSION_TO_LANGUAGE.get(ext) ?? null;
|
|
89
|
+
export const SUPPORTED_EXTENSIONS = new Set(EXTENSION_TO_LANGUAGE.keys());
|
|
90
|
+
export const getLanguageFromPath = (filePath) => {
|
|
91
|
+
const ext = path.extname(filePath);
|
|
92
|
+
return extensionToLanguage(ext);
|
|
93
|
+
};
|
|
94
|
+
export const resetLoader = () => {
|
|
95
|
+
parserInitialized = false;
|
|
96
|
+
loadedGrammars.clear();
|
|
97
|
+
};
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { SupportedLanguage } from "./tree-sitter-loader.js";
|
|
2
|
+
export interface LanguageQueries {
|
|
3
|
+
functions: string;
|
|
4
|
+
classes: string;
|
|
5
|
+
methods: string;
|
|
6
|
+
imports: string;
|
|
7
|
+
calls: string;
|
|
8
|
+
inherits: string;
|
|
9
|
+
}
|
|
10
|
+
export declare const QUERIES_BY_LANGUAGE: Record<SupportedLanguage, LanguageQueries>;
|
|
@@ -0,0 +1,285 @@
|
|
|
1
|
+
const TS_JS_QUERIES = {
|
|
2
|
+
functions: `
|
|
3
|
+
(function_declaration
|
|
4
|
+
name: (identifier) @name
|
|
5
|
+
parameters: (formal_parameters) @params
|
|
6
|
+
return_type: (type_annotation)? @returnType
|
|
7
|
+
body: (statement_block) @body) @func
|
|
8
|
+
(generator_function_declaration
|
|
9
|
+
name: (identifier) @name
|
|
10
|
+
parameters: (formal_parameters) @params
|
|
11
|
+
return_type: (type_annotation)? @returnType
|
|
12
|
+
body: (statement_block) @body) @func
|
|
13
|
+
`,
|
|
14
|
+
classes: `
|
|
15
|
+
(class_declaration
|
|
16
|
+
name: (type_identifier) @name
|
|
17
|
+
heritage: (class_heritage
|
|
18
|
+
(identifier) @parent)?) @class
|
|
19
|
+
(class_declaration
|
|
20
|
+
name: (type_identifier) @name) @class
|
|
21
|
+
`,
|
|
22
|
+
methods: `
|
|
23
|
+
(method_definition
|
|
24
|
+
name: (property_identifier) @name
|
|
25
|
+
parameters: (formal_parameters) @params
|
|
26
|
+
return_type: (type_annotation)? @returnType
|
|
27
|
+
body: (statement_block) @body) @method
|
|
28
|
+
`,
|
|
29
|
+
imports: `
|
|
30
|
+
(import_statement
|
|
31
|
+
source: (string) @source) @import
|
|
32
|
+
(import_require_clause
|
|
33
|
+
source: (string) @source) @import
|
|
34
|
+
`,
|
|
35
|
+
calls: `
|
|
36
|
+
(call_expression
|
|
37
|
+
function: (identifier) @callee) @call
|
|
38
|
+
(call_expression
|
|
39
|
+
function: (member_expression
|
|
40
|
+
property: (property_identifier) @callee)) @call
|
|
41
|
+
`,
|
|
42
|
+
inherits: `
|
|
43
|
+
(class_heritage
|
|
44
|
+
(identifier) @parent) @inherits
|
|
45
|
+
`
|
|
46
|
+
};
|
|
47
|
+
const GO_QUERIES = {
|
|
48
|
+
functions: `
|
|
49
|
+
(function_declaration
|
|
50
|
+
name: (identifier) @name
|
|
51
|
+
parameters: (parameter_list) @params
|
|
52
|
+
result: (_) @returnType?
|
|
53
|
+
body: (block) @body) @func
|
|
54
|
+
(method_declaration
|
|
55
|
+
receiver: (parameter_list) @receiver
|
|
56
|
+
name: (field_identifier) @name
|
|
57
|
+
parameters: (parameter_list) @params
|
|
58
|
+
result: (_) @returnType?
|
|
59
|
+
body: (block) @body) @func
|
|
60
|
+
`,
|
|
61
|
+
classes: `
|
|
62
|
+
(type_declaration
|
|
63
|
+
declarator: (type_identifier) @name
|
|
64
|
+
type: (struct_type)) @class
|
|
65
|
+
`,
|
|
66
|
+
methods: `
|
|
67
|
+
(method_declaration
|
|
68
|
+
receiver: (parameter_list
|
|
69
|
+
(parameter_declaration
|
|
70
|
+
name: (identifier)? @receiverName
|
|
71
|
+
type: (_) @receiverType))
|
|
72
|
+
name: (field_identifier) @name
|
|
73
|
+
parameters: (parameter_list) @params
|
|
74
|
+
result: (_) @returnType?
|
|
75
|
+
body: (block) @body) @method
|
|
76
|
+
`,
|
|
77
|
+
imports: `
|
|
78
|
+
(import_declaration
|
|
79
|
+
(import_spec_list
|
|
80
|
+
(import_spec
|
|
81
|
+
path: (interpreted_string_literal) @source))) @import
|
|
82
|
+
(import_declaration
|
|
83
|
+
(import_spec
|
|
84
|
+
path: (interpreted_string_literal) @source)) @import
|
|
85
|
+
`,
|
|
86
|
+
calls: `
|
|
87
|
+
(call_expression
|
|
88
|
+
function: (identifier) @callee) @call
|
|
89
|
+
(call_expression
|
|
90
|
+
function: (selector_expression
|
|
91
|
+
field: (field_identifier) @callee)) @call
|
|
92
|
+
(call_expression
|
|
93
|
+
function: (qualified_identifier
|
|
94
|
+
name: (field_identifier) @callee)) @call
|
|
95
|
+
`,
|
|
96
|
+
inherits: `
|
|
97
|
+
(type_spec
|
|
98
|
+
type: (struct_type
|
|
99
|
+
(field_declaration
|
|
100
|
+
name: (field_identifier) @parent
|
|
101
|
+
type: (type_identifier)))) @inherits
|
|
102
|
+
`
|
|
103
|
+
};
|
|
104
|
+
const PYTHON_QUERIES = {
|
|
105
|
+
functions: `
|
|
106
|
+
(function_definition
|
|
107
|
+
name: (identifier) @name
|
|
108
|
+
parameters: (parameters) @params
|
|
109
|
+
return_type: (type)? @returnType
|
|
110
|
+
body: (block) @body) @func
|
|
111
|
+
`,
|
|
112
|
+
classes: `
|
|
113
|
+
(class_definition
|
|
114
|
+
name: (identifier) @name
|
|
115
|
+
superclasses: (argument_list
|
|
116
|
+
(identifier) @parent)?) @class
|
|
117
|
+
(class_definition
|
|
118
|
+
name: (identifier) @name) @class
|
|
119
|
+
`,
|
|
120
|
+
methods: `
|
|
121
|
+
(function_definition
|
|
122
|
+
name: (identifier) @name
|
|
123
|
+
parameters: (parameters) @params
|
|
124
|
+
return_type: (type)? @returnType
|
|
125
|
+
body: (block) @body) @method
|
|
126
|
+
`,
|
|
127
|
+
imports: `
|
|
128
|
+
(import_statement
|
|
129
|
+
name: (dotted_name) @source) @import
|
|
130
|
+
(import_from_statement
|
|
131
|
+
module_name: (dotted_name) @source) @import
|
|
132
|
+
`,
|
|
133
|
+
calls: `
|
|
134
|
+
(call
|
|
135
|
+
function: (identifier) @callee) @call
|
|
136
|
+
(call
|
|
137
|
+
function: (attribute
|
|
138
|
+
attribute: (identifier) @callee)) @call
|
|
139
|
+
`,
|
|
140
|
+
inherits: `
|
|
141
|
+
(argument_list
|
|
142
|
+
(identifier) @parent) @inherits
|
|
143
|
+
`
|
|
144
|
+
};
|
|
145
|
+
const C_QUERIES = {
|
|
146
|
+
functions: `
|
|
147
|
+
(function_definition
|
|
148
|
+
declarator: (function_declarator
|
|
149
|
+
declarator: (identifier) @name
|
|
150
|
+
parameters: (parameter_list) @params)
|
|
151
|
+
body: (compound_statement) @body) @func
|
|
152
|
+
`,
|
|
153
|
+
classes: ``,
|
|
154
|
+
methods: ``,
|
|
155
|
+
imports: `
|
|
156
|
+
(preproc_include
|
|
157
|
+
path: (system_lib_string) @source) @import
|
|
158
|
+
(preproc_include
|
|
159
|
+
path: (string_literal) @source) @import
|
|
160
|
+
`,
|
|
161
|
+
calls: `
|
|
162
|
+
(call_expression
|
|
163
|
+
function: (identifier) @callee) @call
|
|
164
|
+
`,
|
|
165
|
+
inherits: ``
|
|
166
|
+
};
|
|
167
|
+
const CPP_QUERIES = {
|
|
168
|
+
functions: `
|
|
169
|
+
(function_definition
|
|
170
|
+
declarator: (function_declarator
|
|
171
|
+
declarator: (identifier) @name
|
|
172
|
+
parameters: (parameter_list) @params)
|
|
173
|
+
body: (compound_statement) @body) @func
|
|
174
|
+
(function_definition
|
|
175
|
+
declarator: (function_declarator
|
|
176
|
+
declarator: (field_identifier) @name
|
|
177
|
+
parameters: (parameter_list) @params)
|
|
178
|
+
body: (compound_statement) @body) @func
|
|
179
|
+
`,
|
|
180
|
+
classes: `
|
|
181
|
+
(class_specifier
|
|
182
|
+
name: (type_identifier) @name
|
|
183
|
+
(base_class_clause
|
|
184
|
+
type: (type_identifier) @parent)?) @class
|
|
185
|
+
(class_specifier
|
|
186
|
+
name: (type_identifier) @name) @class
|
|
187
|
+
(struct_specifier
|
|
188
|
+
name: (type_identifier) @name
|
|
189
|
+
(base_class_clause
|
|
190
|
+
type: (type_identifier) @parent)?) @class
|
|
191
|
+
(struct_specifier
|
|
192
|
+
name: (type_identifier) @name) @class
|
|
193
|
+
`,
|
|
194
|
+
methods: `
|
|
195
|
+
(function_definition
|
|
196
|
+
declarator: (function_declarator
|
|
197
|
+
declarator: (field_identifier) @name
|
|
198
|
+
parameters: (parameter_list) @params)
|
|
199
|
+
body: (compound_statement) @body) @method
|
|
200
|
+
`,
|
|
201
|
+
imports: `
|
|
202
|
+
(preproc_include
|
|
203
|
+
path: (system_lib_string) @source) @import
|
|
204
|
+
(preproc_include
|
|
205
|
+
path: (string_literal) @source) @import
|
|
206
|
+
(import_declaration
|
|
207
|
+
(import_module) @source) @import
|
|
208
|
+
`,
|
|
209
|
+
calls: `
|
|
210
|
+
(call_expression
|
|
211
|
+
function: (identifier) @callee) @call
|
|
212
|
+
(call_expression
|
|
213
|
+
function: (field_expression
|
|
214
|
+
field: (field_identifier) @callee)) @call
|
|
215
|
+
`,
|
|
216
|
+
inherits: `
|
|
217
|
+
(base_class_clause
|
|
218
|
+
type: (type_identifier) @parent) @inherits
|
|
219
|
+
`
|
|
220
|
+
};
|
|
221
|
+
const RUST_QUERIES = {
|
|
222
|
+
functions: `
|
|
223
|
+
(function_item
|
|
224
|
+
name: (identifier) @name
|
|
225
|
+
parameters: (parameters) @params
|
|
226
|
+
return_type: (type_identifier)? @returnType
|
|
227
|
+
body: (block) @body) @func
|
|
228
|
+
`,
|
|
229
|
+
classes: `
|
|
230
|
+
(struct_item
|
|
231
|
+
name: (type_identifier) @name) @class
|
|
232
|
+
(enum_item
|
|
233
|
+
name: (type_identifier) @name) @class
|
|
234
|
+
(trait_item
|
|
235
|
+
name: (type_identifier) @name) @class
|
|
236
|
+
(impl_item
|
|
237
|
+
type: (type_identifier) @name
|
|
238
|
+
trait: (type_identifier)? @trait) @class
|
|
239
|
+
`,
|
|
240
|
+
methods: `
|
|
241
|
+
(function_item
|
|
242
|
+
name: (identifier) @name
|
|
243
|
+
parameters: (parameters
|
|
244
|
+
(self_parameter))? @self
|
|
245
|
+
return_type: (type_identifier)? @returnType
|
|
246
|
+
body: (block) @body) @method
|
|
247
|
+
`,
|
|
248
|
+
imports: `
|
|
249
|
+
(use_declaration
|
|
250
|
+
argument: (scoped_identifier
|
|
251
|
+
path: (identifier) @source)) @import
|
|
252
|
+
(use_declaration
|
|
253
|
+
argument: (scoped_identifier
|
|
254
|
+
(scoped_identifier
|
|
255
|
+
path: (identifier) @source))) @import
|
|
256
|
+
(use_declaration
|
|
257
|
+
argument: (identifier) @source) @import
|
|
258
|
+
(use_declaration
|
|
259
|
+
argument: (use_as_clause
|
|
260
|
+
path: (_) @source)) @import
|
|
261
|
+
`,
|
|
262
|
+
calls: `
|
|
263
|
+
(call_expression
|
|
264
|
+
function: (identifier) @callee) @call
|
|
265
|
+
(call_expression
|
|
266
|
+
function: (scoped_identifier
|
|
267
|
+
name: (identifier) @callee)) @call
|
|
268
|
+
(call_expression
|
|
269
|
+
function: (field_expression
|
|
270
|
+
field: (field_identifier) @callee)) @call
|
|
271
|
+
`,
|
|
272
|
+
inherits: `
|
|
273
|
+
(trait_bounds
|
|
274
|
+
(type_identifier) @parent) @inherits
|
|
275
|
+
`
|
|
276
|
+
};
|
|
277
|
+
export const QUERIES_BY_LANGUAGE = {
|
|
278
|
+
typescript: TS_JS_QUERIES,
|
|
279
|
+
javascript: TS_JS_QUERIES,
|
|
280
|
+
go: GO_QUERIES,
|
|
281
|
+
python: PYTHON_QUERIES,
|
|
282
|
+
c: C_QUERIES,
|
|
283
|
+
cpp: CPP_QUERIES,
|
|
284
|
+
rust: RUST_QUERIES
|
|
285
|
+
};
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@abhinav2203/codeflow-core",
|
|
3
3
|
"description": "Framework-agnostic CodeFlow analysis core for blueprint generation, repository analysis, exports, and conflict detection.",
|
|
4
|
-
"version": "0.
|
|
4
|
+
"version": "1.0.0",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/index.js",
|
|
7
7
|
"types": "dist/index.d.ts",
|
|
@@ -39,14 +39,25 @@
|
|
|
39
39
|
],
|
|
40
40
|
"scripts": {
|
|
41
41
|
"build": "tsc -p tsconfig.json",
|
|
42
|
+
"test": "vitest",
|
|
43
|
+
"test:watch": "vitest --watch",
|
|
42
44
|
"prepare": "npm run build"
|
|
43
45
|
},
|
|
44
46
|
"dependencies": {
|
|
47
|
+
"tree-sitter-c": "^0.24.0",
|
|
48
|
+
"tree-sitter-cpp": "^0.23.4",
|
|
49
|
+
"tree-sitter-go": "^0.25.0",
|
|
50
|
+
"tree-sitter-javascript": "^0.25.0",
|
|
51
|
+
"tree-sitter-python": "^0.25.0",
|
|
52
|
+
"tree-sitter-rust": "^0.24.0",
|
|
53
|
+
"tree-sitter-typescript": "^0.23.2",
|
|
45
54
|
"ts-morph": "^27.0.2",
|
|
55
|
+
"web-tree-sitter": "^0.25.0",
|
|
46
56
|
"zod": "^4.3.6"
|
|
47
57
|
},
|
|
48
58
|
"devDependencies": {
|
|
49
59
|
"@types/node": "^25.5.0",
|
|
50
|
-
"typescript": "^5.9.3"
|
|
60
|
+
"typescript": "^5.9.3",
|
|
61
|
+
"vitest": "^4.1.2"
|
|
51
62
|
}
|
|
52
63
|
}
|