mirror of
https://github.com/samiyev/puaros.git
synced 2025-12-28 07:16:53 +05:00
Add infrastructure implementations: - BaseRepository with in-memory storage - GitService for git operations using simple-git - CodeParser for JS/TS parsing using tree-sitter
56 lines
1.5 KiB
TypeScript
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;
|
|
}
|
|
} |