-
Notifications
You must be signed in to change notification settings - Fork 46
feat: add TypeScript and TSX analyzer #610
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
davidsuarezcdo
wants to merge
1
commit into
FalkorDB:staging
Choose a base branch
from
davidsuarezcdo:feat/typescript-analyzer
base: staging
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,119 @@ | ||
| import subprocess | ||
| from pathlib import Path | ||
|
|
||
| from multilspy import SyncLanguageServer | ||
| from ...entities import * | ||
| from typing import Optional | ||
| from ..analyzer import AbstractAnalyzer | ||
|
|
||
| import tree_sitter_typescript as tstypescript | ||
| from tree_sitter import Language, Node | ||
|
|
||
| import logging | ||
| logger = logging.getLogger('code_graph') | ||
|
|
||
| class TypeScriptAnalyzer(AbstractAnalyzer): | ||
| def __init__(self) -> None: | ||
| super().__init__(Language(tstypescript.language_typescript())) | ||
|
|
||
| def add_dependencies(self, path: Path, files: list[Path]): | ||
| if Path(f"{path}/node_modules").is_dir(): | ||
| return | ||
| if Path(f"{path}/package.json").is_file(): | ||
| subprocess.run(["npm", "install"], cwd=str(path)) | ||
|
|
||
| def get_entity_label(self, node: Node) -> str: | ||
| if node.type == 'class_declaration': | ||
| return "Class" | ||
| elif node.type == 'interface_declaration': | ||
| return "Interface" | ||
| elif node.type == 'function_declaration': | ||
| return "Function" | ||
| elif node.type == 'method_definition': | ||
| return "Method" | ||
| raise ValueError(f"Unknown entity type: {node.type}") | ||
|
|
||
| def get_entity_name(self, node: Node) -> str: | ||
| if node.type in ['class_declaration', 'interface_declaration', 'function_declaration', 'method_definition']: | ||
| name_node = node.child_by_field_name('name') | ||
| if name_node: | ||
| return name_node.text.decode('utf-8') | ||
| return '' | ||
| raise ValueError(f"Unknown entity type: {node.type}") | ||
|
|
||
| def get_entity_docstring(self, node: Node) -> Optional[str]: | ||
| if node.type in ['class_declaration', 'interface_declaration', 'function_declaration', 'method_definition']: | ||
| sibling = node.prev_sibling | ||
| if sibling and sibling.type == 'comment': | ||
| return sibling.text.decode('utf-8') | ||
| return None | ||
| raise ValueError(f"Unknown entity type: {node.type}") | ||
|
|
||
| def get_entity_types(self) -> list[str]: | ||
| return ['class_declaration', 'interface_declaration', 'function_declaration', 'method_definition'] | ||
|
|
||
| def add_symbols(self, entity: Entity) -> None: | ||
| if entity.node.type == 'class_declaration': | ||
| heritage = self._captures("(class_heritage (extends_clause value: (_) @base_class))", entity.node) | ||
| if 'base_class' in heritage: | ||
| for base in heritage['base_class']: | ||
| entity.add_symbol("base_class", base) | ||
| implements = self._captures("(class_heritage (implements_clause (type_list (_) @interface)))", entity.node) | ||
| if 'interface' in implements: | ||
| for iface in implements['interface']: | ||
| entity.add_symbol("implement_interface", iface) | ||
| elif entity.node.type == 'interface_declaration': | ||
| extends = self._captures("(interface_declaration (extends_clause (type_list (_) @type)))?", entity.node) | ||
| if 'type' in extends: | ||
| for t in extends['type']: | ||
| entity.add_symbol("extend_interface", t) | ||
| elif entity.node.type in ['function_declaration', 'method_definition']: | ||
| captures = self._captures("(call_expression) @reference.call", entity.node) | ||
| if 'reference.call' in captures: | ||
| for caller in captures['reference.call']: | ||
| entity.add_symbol("call", caller) | ||
| params = self._captures("(formal_parameters (required_parameter type: (_) @parameter))", entity.node) | ||
| if 'parameter' in params: | ||
| for param in params['parameter']: | ||
| entity.add_symbol("parameters", param) | ||
| return_type = entity.node.child_by_field_name('return_type') | ||
| if return_type: | ||
| entity.add_symbol("return_type", return_type) | ||
|
|
||
| def is_dependency(self, file_path: str) -> bool: | ||
| return "node_modules" in file_path | ||
|
|
||
| def resolve_path(self, file_path: str, path: Path) -> str: | ||
| return file_path | ||
|
|
||
| def resolve_type(self, files: dict[Path, File], lsp: SyncLanguageServer, file_path: Path, path: Path, node: Node) -> list[Entity]: | ||
| res = [] | ||
| for file, resolved_node in self.resolve(files, lsp, file_path, path, node): | ||
| type_dec = self.find_parent(resolved_node, ['class_declaration', 'interface_declaration']) | ||
| if type_dec in file.entities: | ||
| res.append(file.entities[type_dec]) | ||
| return res | ||
|
|
||
| def resolve_method(self, files: dict[Path, File], lsp: SyncLanguageServer, file_path: Path, path: Path, node: Node) -> list[Entity]: | ||
| res = [] | ||
| if node.type == 'call_expression': | ||
| func_node = node.child_by_field_name('function') | ||
| if func_node and func_node.type == 'member_expression': | ||
| func_node = func_node.child_by_field_name('property') | ||
| if func_node: | ||
| node = func_node | ||
| for file, resolved_node in self.resolve(files, lsp, file_path, path, node): | ||
| method_dec = self.find_parent(resolved_node, ['function_declaration', 'method_definition', 'class_declaration', 'interface_declaration']) | ||
| if method_dec and method_dec.type in ['class_declaration', 'interface_declaration']: | ||
| continue | ||
| if method_dec in file.entities: | ||
| res.append(file.entities[method_dec]) | ||
| return res | ||
|
|
||
| def resolve_symbol(self, files: dict[Path, File], lsp: SyncLanguageServer, file_path: Path, path: Path, key: str, symbol: Node) -> list[Entity]: | ||
| if key in ["implement_interface", "base_class", "extend_interface", "parameters", "return_type"]: | ||
| return self.resolve_type(files, lsp, file_path, path, symbol) | ||
| elif key in ["call"]: | ||
| return self.resolve_method(files, lsp, file_path, path, symbol) | ||
| else: | ||
| raise ValueError(f"Unknown key {key}") | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add error handling for
npm installand consider security implications.filesparameter is unused (Ruff ARG002).subprocess.runwithoutcheck=Truesilently ignores failures.🛡️ Proposed fix with error handling
🧰 Tools
🪛 Ruff (0.15.5)
[warning] 19-19: Unused method argument:
files(ARG002)
[error] 23-23: Starting a process with a partial executable path
(S607)
🤖 Prompt for AI Agents