Skip to content

Commit b6c258d

Browse files
authored
Merge pull request #1600 from codeflash-ai/codeflash/optimize-pr1561-2026-02-20T11.55.27
⚡️ Speed up method `TreeSitterAnalyzer._extract_import_info` by 36% in PR #1561 (`add/support_react`)
2 parents 2c33ed4 + 6fdf22e commit b6c258d

1 file changed

Lines changed: 11 additions & 6 deletions

File tree

codeflash/languages/javascript/treesitter_utils.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -513,15 +513,18 @@ def _extract_import_info(self, node: Node, source_bytes: bytes) -> ImportInfo |
513513
namespace_import = None
514514
is_type_only = False
515515

516+
# Decode once for performance
517+
source_text = source_bytes.decode("utf8")
518+
516519
# Get the module path (source)
517520
source_node = node.child_by_field_name("source")
518521
if source_node:
519522
# Remove quotes from string
520-
module_path = self.get_node_text(source_node, source_bytes).strip("'\"")
523+
module_path = source_text[source_node.start_byte : source_node.end_byte].strip("'\"")
521524

522525
# Check for type-only import (TypeScript)
523526
for child in node.children:
524-
if child.type == "type" or self.get_node_text(child, source_bytes) == "type":
527+
if child.type == "type" or source_text[child.start_byte : child.end_byte] == "type":
525528
is_type_only = True
526529
break
527530

@@ -532,21 +535,23 @@ def _extract_import_info(self, node: Node, source_bytes: bytes) -> ImportInfo |
532535
# Re-extract after processing
533536
for clause_child in child.children:
534537
if clause_child.type == "identifier":
535-
default_import = self.get_node_text(clause_child, source_bytes)
538+
default_import = source_text[clause_child.start_byte : clause_child.end_byte]
536539
elif clause_child.type == "named_imports":
537540
for spec in clause_child.children:
538541
if spec.type == "import_specifier":
539542
name_node = spec.child_by_field_name("name")
540543
alias_node = spec.child_by_field_name("alias")
541544
if name_node:
542-
name = self.get_node_text(name_node, source_bytes)
543-
alias = self.get_node_text(alias_node, source_bytes) if alias_node else None
545+
name = source_text[name_node.start_byte : name_node.end_byte]
546+
alias = (
547+
source_text[alias_node.start_byte : alias_node.end_byte] if alias_node else None
548+
)
544549
named_imports.append((name, alias))
545550
elif clause_child.type == "namespace_import":
546551
# import * as X
547552
for ns_child in clause_child.children:
548553
if ns_child.type == "identifier":
549-
namespace_import = self.get_node_text(ns_child, source_bytes)
554+
namespace_import = source_text[ns_child.start_byte : ns_child.end_byte]
550555

551556
if not module_path:
552557
return None

0 commit comments

Comments
 (0)