diff --git a/codeflash/languages/javascript/code_replacer.py b/codeflash/languages/javascript/code_replacer.py index 83c96ec6a..df7e21165 100644 --- a/codeflash/languages/javascript/code_replacer.py +++ b/codeflash/languages/javascript/code_replacer.py @@ -151,10 +151,8 @@ def _insert_declaration_after_dependencies( if insertion_line > 0 and lines[insertion_line - 1].strip(): decl_code = "\n" + decl_code - before = lines[:insertion_line] - after = lines[insertion_line:] - - return "".join([*before, decl_code, *after]) + # Directly construct the result without intermediate list + return "".join(lines[:insertion_line]) + decl_code + "".join(lines[insertion_line:]) # Author: ali @@ -174,10 +172,11 @@ def _find_insertion_line_for_declaration( """ # Find the maximum end line among referenced declarations - max_dependency_line = 0 - for name in referenced_names: - if name in existing_decl_end_lines: - max_dependency_line = max(max_dependency_line, existing_decl_end_lines[name]) + max_dependency_line = max( + (existing_decl_end_lines[name] for name in referenced_names if name in existing_decl_end_lines), + default=0 + ) + if max_dependency_line > 0: # Insert after the last dependency (end_line is 1-indexed, we need 0-indexed) diff --git a/codeflash/languages/javascript/treesitter.py b/codeflash/languages/javascript/treesitter.py index c00cb228e..360f0a82e 100644 --- a/codeflash/languages/javascript/treesitter.py +++ b/codeflash/languages/javascript/treesitter.py @@ -18,6 +18,8 @@ from tree_sitter import Node, Tree +_PARSER_CACHE: dict[TreeSitterLanguage, Parser] = {} + logger = logging.getLogger(__name__) @@ -1770,6 +1772,18 @@ def _extract_type_definition( ) + @property + def parser(self) -> Parser: + """Get or create the cached parser for this language.""" + if self._parser is None: + # Check if we have a cached parser for this language + if self.language not in _PARSER_CACHE: + _PARSER_CACHE[self.language] = Parser() + # Assuming parser setup happens elsewhere or in subclass + self._parser = _PARSER_CACHE[self.language] + return self._parser + + def get_analyzer_for_file(file_path: Path) -> TreeSitterAnalyzer: """Get the appropriate TreeSitterAnalyzer for a file based on its extension.