-
Notifications
You must be signed in to change notification settings - Fork 49
Add support for JavaScript #593
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
Merged
Merged
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
4e44a0a
Add support for JavaScript
gkorland 0f193ce
fix: address review feedback for JavaScript analyzer
gkorland 1de114e
fix(javascript): address review comments and improve test coverage
gkorland bbad52d
fix(javascript): skip NullLanguageServer in second pass, add integrat…
gkorland 5780d2b
fix(analyzers): guard against KeyError for ignored files in second_pass
gkorland 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
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
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,104 @@ | ||
| from pathlib import Path | ||
| from typing import Optional | ||
|
|
||
| from multilspy import SyncLanguageServer | ||
| from ...entities.entity import Entity | ||
| from ...entities.file import File | ||
| from ..analyzer import AbstractAnalyzer | ||
|
|
||
| import tree_sitter_javascript as tsjs | ||
| from tree_sitter import Language, Node | ||
|
|
||
| import logging | ||
| logger = logging.getLogger('code_graph') | ||
|
|
||
|
|
||
| class JavaScriptAnalyzer(AbstractAnalyzer): | ||
| def __init__(self) -> None: | ||
| super().__init__(Language(tsjs.language())) | ||
|
|
||
| def add_dependencies(self, path: Path, files: list[Path]): | ||
| pass | ||
|
|
||
| def get_entity_label(self, node: Node) -> str: | ||
| if node.type == 'function_declaration': | ||
| return "Function" | ||
| elif node.type == 'class_declaration': | ||
| return "Class" | ||
| 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 ['function_declaration', 'class_declaration', 'method_definition']: | ||
| name_node = node.child_by_field_name('name') | ||
| if name_node is None: | ||
| return '' | ||
| return name_node.text.decode('utf-8') | ||
| raise ValueError(f"Unknown entity type: {node.type}") | ||
|
|
||
| def get_entity_docstring(self, node: Node) -> Optional[str]: | ||
| if node.type in ['function_declaration', 'class_declaration', 'method_definition']: | ||
| if node.prev_sibling and node.prev_sibling.type == 'comment': | ||
| return node.prev_sibling.text.decode('utf-8') | ||
| return None | ||
| raise ValueError(f"Unknown entity type: {node.type}") | ||
|
|
||
| def get_entity_types(self) -> list[str]: | ||
| return ['function_declaration', 'class_declaration', 'method_definition'] | ||
|
|
||
| def add_symbols(self, entity: Entity) -> None: | ||
| if entity.node.type == 'class_declaration': | ||
| # Check for `extends` clause via class_heritage | ||
| for child in entity.node.children: | ||
| if child.type == 'class_heritage': | ||
| for heritage_child in child.children: | ||
| if heritage_child.type == 'identifier': | ||
| entity.add_symbol("base_class", heritage_child) | ||
| 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) | ||
| captures = self._captures("(formal_parameters (identifier) @parameter)", entity.node) | ||
| if 'parameter' in captures: | ||
| for parameter in captures['parameter']: | ||
| entity.add_symbol("parameters", parameter) | ||
|
|
||
| def is_dependency(self, file_path: str) -> bool: | ||
| return "node_modules" in file_path | ||
|
coderabbitai[bot] marked this conversation as resolved.
Outdated
|
||
|
|
||
| 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']) | ||
| 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']) | ||
| if method_dec and method_dec.type == 'class_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 ["base_class", "parameters"]: | ||
| 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
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
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,31 @@ | ||
| /** | ||
| * Base class for shapes | ||
| */ | ||
| class Shape { | ||
| constructor(name) { | ||
| this.name = name; | ||
| } | ||
|
|
||
| area() { | ||
| return 0; | ||
| } | ||
| } | ||
|
|
||
| class Circle extends Shape { | ||
| constructor(radius) { | ||
| super(radius); | ||
| this.radius = radius; | ||
| } | ||
|
|
||
| area() { | ||
| return Math.PI * this.radius * this.radius; | ||
| } | ||
| } | ||
|
|
||
| function calculateTotal(shapes) { | ||
| let total = 0; | ||
| for (const shape of shapes) { | ||
| total += shape.area(); | ||
| } | ||
| return total; | ||
| } |
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,102 @@ | ||
| """Tests for the JavaScript analyzer - extraction only (no DB required).""" | ||
|
|
||
| import unittest | ||
| from pathlib import Path | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
|
|
||
| from api.analyzers.javascript.analyzer import JavaScriptAnalyzer | ||
| from api.entities.entity import Entity | ||
| from api.entities.file import File | ||
|
|
||
|
|
||
| def _entity_name(analyzer, entity): | ||
| """Get the name of an entity using the analyzer.""" | ||
| return analyzer.get_entity_name(entity.node) | ||
|
|
||
|
|
||
| class TestJavaScriptAnalyzer(unittest.TestCase): | ||
| @classmethod | ||
| def setUpClass(cls): | ||
| cls.analyzer = JavaScriptAnalyzer() | ||
| source_dir = Path(__file__).parent / "source_files" / "javascript" | ||
| cls.sample_path = source_dir / "sample.js" | ||
| source = cls.sample_path.read_bytes() | ||
| tree = cls.analyzer.parser.parse(source) | ||
| cls.file = File(cls.sample_path, tree) | ||
|
|
||
| # Walk AST and extract entities (mirrors create_hierarchy without Graph) | ||
| types = cls.analyzer.get_entity_types() | ||
| stack = [tree.root_node] | ||
| while stack: | ||
| node = stack.pop() | ||
| if node.type in types: | ||
| entity = Entity(node) | ||
| cls.analyzer.add_symbols(entity) | ||
| cls.file.add_entity(entity) | ||
| # Also recurse into entity children (e.g., class body methods) | ||
| stack.extend(node.children) | ||
| else: | ||
| stack.extend(node.children) | ||
|
|
||
| def _entity_names(self): | ||
| return [_entity_name(self.analyzer, e) for e in self.file.entities.values()] | ||
|
|
||
| def test_discovers_js_files(self): | ||
| """SourceAnalyzer should enumerate .js files.""" | ||
| source_dir = Path(__file__).parent / "source_files" / "javascript" | ||
| js_files = list(source_dir.rglob("*.js")) | ||
| self.assertTrue(len(js_files) > 0, "Should find .js files") | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
|
|
||
| def test_entity_types(self): | ||
| """Analyzer should recognise JS entity types.""" | ||
| self.assertEqual( | ||
| self.analyzer.get_entity_types(), | ||
| ['function_declaration', 'class_declaration', 'method_definition'], | ||
| ) | ||
|
|
||
| def test_class_extraction(self): | ||
| """Classes should be extracted from sample.js.""" | ||
| names = self._entity_names() | ||
| self.assertIn("Shape", names) | ||
| self.assertIn("Circle", names) | ||
|
|
||
| def test_function_extraction(self): | ||
| """Top-level functions should be extracted.""" | ||
| names = self._entity_names() | ||
| self.assertIn("calculateTotal", names) | ||
|
|
||
| def test_method_extraction(self): | ||
| """Class methods should be extracted.""" | ||
| names = self._entity_names() | ||
| self.assertIn("area", names) | ||
| self.assertIn("constructor", names) | ||
|
|
||
| def test_class_labels(self): | ||
| """Classes should get the 'Class' label.""" | ||
| for entity in self.file.entities.values(): | ||
| if _entity_name(self.analyzer, entity) in ("Shape", "Circle"): | ||
| self.assertEqual(self.analyzer.get_entity_label(entity.node), "Class") | ||
|
|
||
| def test_function_label(self): | ||
| """Functions should get the 'Function' label.""" | ||
| for entity in self.file.entities.values(): | ||
| if _entity_name(self.analyzer, entity) == "calculateTotal": | ||
| self.assertEqual(self.analyzer.get_entity_label(entity.node), "Function") | ||
|
|
||
| def test_base_class_symbol(self): | ||
| """Circle should have Shape as a base_class symbol.""" | ||
| for entity in self.file.entities.values(): | ||
| if _entity_name(self.analyzer, entity) == "Circle": | ||
| base_names = [ | ||
| s.symbol.text.decode("utf-8") | ||
| for s in entity.symbols.get("base_class", []) | ||
| ] | ||
| self.assertIn("Shape", base_names) | ||
|
|
||
| def test_is_dependency(self): | ||
| """node_modules paths should be flagged as dependencies.""" | ||
| self.assertTrue(self.analyzer.is_dependency("foo/node_modules/bar/index.js")) | ||
| self.assertFalse(self.analyzer.is_dependency("src/utils.js")) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| unittest.main() | ||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.