Files
puaros/packages/core/src/infrastructure/parsers/CodeParser.ts
imfozilbek 6e24fe6ba8 feat(core): add infrastructure layer services
Add infrastructure implementations:
- BaseRepository with in-memory storage
- GitService for git operations using simple-git
- CodeParser for JS/TS parsing using tree-sitter
2025-11-23 21:43:40 +05:00

56 lines
1.5 KiB
TypeScript

import Parser from 'tree-sitter';
import JavaScript from 'tree-sitter-javascript';
import TypeScript from 'tree-sitter-typescript';
/**
* Code parser service using tree-sitter
*/
export class CodeParser {
private readonly parser: Parser;
constructor() {
this.parser = new Parser();
}
public parseJavaScript(code: string): Parser.Tree {
this.parser.setLanguage(JavaScript);
return this.parser.parse(code);
}
public parseTypeScript(code: string): Parser.Tree {
this.parser.setLanguage(TypeScript.typescript);
return this.parser.parse(code);
}
public parseTsx(code: string): Parser.Tree {
this.parser.setLanguage(TypeScript.tsx);
return this.parser.parse(code);
}
public extractFunctions(tree: Parser.Tree): string[] {
const functions: string[] = [];
const cursor = tree.walk();
const visit = (): void => {
const node = cursor.currentNode;
if (
node.type === 'function_declaration' ||
node.type === 'arrow_function' ||
node.type === 'function_expression'
) {
functions.push(node.text);
}
if (cursor.gotoFirstChild()) {
do {
visit();
} while (cursor.gotoNextSibling());
cursor.gotoParent();
}
};
visit();
return functions;
}
}