@nirnex/parser 2.0.3
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/index.d.ts +22 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +107 -0
- package/dist/index.js.map +1 -0
- package/package.json +43 -0
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
export interface ParsedModule {
|
|
2
|
+
path: string;
|
|
3
|
+
name: string;
|
|
4
|
+
language: string;
|
|
5
|
+
loc: number;
|
|
6
|
+
exports: Array<{
|
|
7
|
+
name: string;
|
|
8
|
+
isDefault: boolean;
|
|
9
|
+
}>;
|
|
10
|
+
imports: Array<{
|
|
11
|
+
source: string;
|
|
12
|
+
specifiers: string[];
|
|
13
|
+
}>;
|
|
14
|
+
declarations: Array<{
|
|
15
|
+
name: string;
|
|
16
|
+
kind: 'function' | 'class';
|
|
17
|
+
startLine: number;
|
|
18
|
+
endLine: number;
|
|
19
|
+
}>;
|
|
20
|
+
}
|
|
21
|
+
export declare function parseFile(filePath: string): ParsedModule | null;
|
|
22
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAOA,MAAM,WAAW,YAAY;IAC3B,IAAI,EAAE,MAAM,CAAC;IACb,IAAI,EAAE,MAAM,CAAC;IACb,QAAQ,EAAE,MAAM,CAAC;IACjB,GAAG,EAAE,MAAM,CAAC;IACZ,OAAO,EAAE,KAAK,CAAC;QAAE,IAAI,EAAE,MAAM,CAAC;QAAC,SAAS,EAAE,OAAO,CAAA;KAAE,CAAC,CAAC;IACrD,OAAO,EAAE,KAAK,CAAC;QAAE,MAAM,EAAE,MAAM,CAAC;QAAC,UAAU,EAAE,MAAM,EAAE,CAAA;KAAE,CAAC,CAAC;IACzD,YAAY,EAAE,KAAK,CAAC;QAAE,IAAI,EAAE,MAAM,CAAC;QAAC,IAAI,EAAE,UAAU,GAAG,OAAO,CAAC;QAAC,SAAS,EAAE,MAAM,CAAC;QAAC,OAAO,EAAE,MAAM,CAAA;KAAE,CAAC,CAAC;CACvG;AAED,wBAAgB,SAAS,CAAC,QAAQ,EAAE,MAAM,GAAG,YAAY,GAAG,IAAI,CAoG/D"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
import Parser from 'tree-sitter';
|
|
2
|
+
import tsLanguage from 'tree-sitter-typescript';
|
|
3
|
+
import fs from 'node:fs';
|
|
4
|
+
import path from 'node:path';
|
|
5
|
+
const parser = new Parser();
|
|
6
|
+
export function parseFile(filePath) {
|
|
7
|
+
try {
|
|
8
|
+
const ext = path.extname(filePath);
|
|
9
|
+
if (!['.ts', '.tsx'].includes(ext)) {
|
|
10
|
+
return null;
|
|
11
|
+
}
|
|
12
|
+
const content = fs.readFileSync(filePath, 'utf-8');
|
|
13
|
+
const tsLang = tsLanguage;
|
|
14
|
+
if (ext === '.tsx') {
|
|
15
|
+
parser.setLanguage(tsLang.tsx);
|
|
16
|
+
}
|
|
17
|
+
else {
|
|
18
|
+
parser.setLanguage(tsLang.typescript);
|
|
19
|
+
}
|
|
20
|
+
const tree = parser.parse(content);
|
|
21
|
+
const imports = [];
|
|
22
|
+
const exports = [];
|
|
23
|
+
const declarations = [];
|
|
24
|
+
const traverse = (node) => {
|
|
25
|
+
if (node.type === 'import_statement') {
|
|
26
|
+
let source = '';
|
|
27
|
+
const specifiers = [];
|
|
28
|
+
for (const child of node.children) {
|
|
29
|
+
if (child.type === 'string') {
|
|
30
|
+
source = child.text.slice(1, -1);
|
|
31
|
+
}
|
|
32
|
+
else if (child.type === 'import_clause') {
|
|
33
|
+
for (const c of child.children) {
|
|
34
|
+
if (c.type === 'identifier') {
|
|
35
|
+
specifiers.push(c.text);
|
|
36
|
+
}
|
|
37
|
+
else if (c.type === 'named_imports') {
|
|
38
|
+
for (const nc of c.children) {
|
|
39
|
+
if (nc.type === 'import_specifier') {
|
|
40
|
+
specifiers.push(nc.text);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
if (source)
|
|
48
|
+
imports.push({ source, specifiers });
|
|
49
|
+
}
|
|
50
|
+
if (node.type === 'export_statement') {
|
|
51
|
+
let isDefault = false;
|
|
52
|
+
for (const child of node.children) {
|
|
53
|
+
if (child.type === 'default')
|
|
54
|
+
isDefault = true;
|
|
55
|
+
if (child.type === 'export_clause') {
|
|
56
|
+
for (const c of child.children) {
|
|
57
|
+
if (c.type === 'export_specifier')
|
|
58
|
+
exports.push({ name: c.text, isDefault: false });
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
if (child.type === 'class_declaration' || child.type === 'function_declaration' || child.type === 'lexical_declaration') {
|
|
62
|
+
const nameNode = child.children.find(c => c.type === 'identifier');
|
|
63
|
+
if (nameNode) {
|
|
64
|
+
exports.push({ name: nameNode.text, isDefault });
|
|
65
|
+
}
|
|
66
|
+
else if (child.type === 'lexical_declaration') {
|
|
67
|
+
const declC = child.children.find(c => c.type === 'variable_declarator');
|
|
68
|
+
if (declC) {
|
|
69
|
+
const idNode = declC.children.find(c => c.type === 'identifier');
|
|
70
|
+
if (idNode)
|
|
71
|
+
exports.push({ name: idNode.text, isDefault });
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
if (node.type === 'function_declaration' || node.type === 'class_declaration') {
|
|
78
|
+
const nameNode = node.children.find(c => c.type === 'identifier');
|
|
79
|
+
if (nameNode) {
|
|
80
|
+
declarations.push({
|
|
81
|
+
name: nameNode.text,
|
|
82
|
+
kind: node.type.split('_')[0],
|
|
83
|
+
startLine: node.startPosition.row + 1,
|
|
84
|
+
endLine: node.endPosition.row + 1
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
for (const child of node.children)
|
|
89
|
+
traverse(child);
|
|
90
|
+
};
|
|
91
|
+
traverse(tree.rootNode);
|
|
92
|
+
return {
|
|
93
|
+
path: filePath,
|
|
94
|
+
name: path.basename(filePath),
|
|
95
|
+
language: ext === '.tsx' ? 'tsx' : 'typescript',
|
|
96
|
+
loc: content.split('\n').length,
|
|
97
|
+
imports,
|
|
98
|
+
exports,
|
|
99
|
+
declarations
|
|
100
|
+
};
|
|
101
|
+
}
|
|
102
|
+
catch (error) {
|
|
103
|
+
console.error(`Error parsing file ${filePath}`, error);
|
|
104
|
+
return null;
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,MAAM,MAAM,aAAa,CAAC;AACjC,OAAO,UAAU,MAAM,wBAAwB,CAAC;AAChD,OAAO,EAAE,MAAM,SAAS,CAAC;AACzB,OAAO,IAAI,MAAM,WAAW,CAAC;AAE7B,MAAM,MAAM,GAAG,IAAI,MAAM,EAAE,CAAC;AAY5B,MAAM,UAAU,SAAS,CAAC,QAAgB;IACxC,IAAI,CAAC;QACH,MAAM,GAAG,GAAG,IAAI,CAAC,OAAO,CAAC,QAAQ,CAAC,CAAC;QACnC,IAAI,CAAC,CAAC,KAAK,EAAE,MAAM,CAAC,CAAC,QAAQ,CAAC,GAAG,CAAC,EAAE,CAAC;YACnC,OAAO,IAAI,CAAC;QACd,CAAC;QACD,MAAM,OAAO,GAAG,EAAE,CAAC,YAAY,CAAC,QAAQ,EAAE,OAAO,CAAC,CAAC;QAEnD,MAAM,MAAM,GAAG,UAAsD,CAAC;QACtE,IAAI,GAAG,KAAK,MAAM,EAAE,CAAC;YACnB,MAAM,CAAC,WAAW,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC;QACjC,CAAC;aAAM,CAAC;YACN,MAAM,CAAC,WA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C/C,GAAG,EAAE,OAAO,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC,MAAM;YAC/B,OAAO;YACP,OAAO;YACP,YAAY;SACb,CAAC;IACJ,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,OAAO,CAAC,KAAK,CAAC,sBAAsB,QAAQ,EAAE,EAAE,KAAK,CAAC,CAAC;QACvD,OAAO,IAAI,CAAC;IACd,CAAC;AACH,CAAC"}
|
package/package.json
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@nirnex/parser",
|
|
3
|
+
"version": "2.0.3",
|
|
4
|
+
"description": "Nirnex parser — Tree-sitter based source code analysis",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./dist/index.js",
|
|
7
|
+
"types": "./dist/index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"import": "./dist/index.js",
|
|
11
|
+
"types": "./dist/index.d.ts"
|
|
12
|
+
},
|
|
13
|
+
"./dist/*.js": {
|
|
14
|
+
"import": "./dist/*.js",
|
|
15
|
+
"types": "./dist/*.d.ts"
|
|
16
|
+
},
|
|
17
|
+
"./dist/*": "./dist/*"
|
|
18
|
+
},
|
|
19
|
+
"files": [
|
|
20
|
+
"dist",
|
|
21
|
+
"README.md"
|
|
22
|
+
],
|
|
23
|
+
"scripts": {
|
|
24
|
+
"build": "tsc --build",
|
|
25
|
+
"clean": "rm -rf dist"
|
|
26
|
+
},
|
|
27
|
+
"engines": {
|
|
28
|
+
"node": ">=24"
|
|
29
|
+
},
|
|
30
|
+
"repository": {
|
|
31
|
+
"type": "git",
|
|
32
|
+
"url": "https://github.com/nirnex-ai/nirnex.git",
|
|
33
|
+
"directory": "packages/parser"
|
|
34
|
+
},
|
|
35
|
+
"license": "MIT",
|
|
36
|
+
"dependencies": {
|
|
37
|
+
"tree-sitter": "^0.21.1",
|
|
38
|
+
"tree-sitter-typescript": "^0.23.2"
|
|
39
|
+
},
|
|
40
|
+
"devDependencies": {
|
|
41
|
+
"typescript": "^5.4.0"
|
|
42
|
+
}
|
|
43
|
+
}
|