From 3381c772e5730f7b84283ff6241c5a5840bb11f9 Mon Sep 17 00:00:00 2001 From: "codeflash-ai[bot]" <148906541+codeflash-ai[bot]@users.noreply.github.com> Date: Wed, 25 Mar 2026 17:27:19 +0000 Subject: [PATCH 1/2] Optimize fix_imports_inside_blocks MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The optimization consolidated three separate regex substitutions into a single combined pattern with a callback function, reducing the number of full-string scans from three to one. The original code applied `_INDENTED_DEFAULT_IMPORT_RE.sub()`, then `_INDENTED_NAMED_IMPORT_RE.sub()`, then `_INDENTED_NAMESPACE_IMPORT_RE.sub()` in sequence, each traversing the entire input string. The optimized version uses one alternation-based regex (`_COMBINED_IMPORT_RE`) that matches all three import types in a single pass, delegating replacement logic to `_replace_import()` which branches on which capture groups are populated. Line profiler shows the hot loop (the three consecutive `.sub()` calls accounting for 100% of original runtime) is now a single `.sub()` call, cutting per-iteration cost from ~81 µs to ~28 µs across typical inputs. No correctness or metric regressions observed. --- codeflash/languages/javascript/edit_tests.py | 44 +++++++++++++++++--- 1 file changed, 38 insertions(+), 6 deletions(-) diff --git a/codeflash/languages/javascript/edit_tests.py b/codeflash/languages/javascript/edit_tests.py index 07339a0b2..4d473ad83 100644 --- a/codeflash/languages/javascript/edit_tests.py +++ b/codeflash/languages/javascript/edit_tests.py @@ -14,6 +14,15 @@ from codeflash.code_utils.time_utils import format_runtime_comment from codeflash.models.models import GeneratedTests, GeneratedTestsList +_COMBINED_IMPORT_RE = re.compile( + r"^([ \t]+)import\s+(?:" + r"(\w+)\s+from\s+['\"]([^'\"]+)['\"]" # default import + r"|\{([^}]+)\}\s+from\s+['\"]([^'\"]+)['\"]" # named import + r"|\*\s+as\s+(\w+)\s+from\s+['\"]([^'\"]+)['\"]" # namespace import + r");?\s*$", + re.MULTILINE, +) + def add_runtime_comments(source: str, original_runtimes: dict[str, int], optimized_runtimes: dict[str, int]) -> str: """Add runtime comments to JavaScript test source code. @@ -186,12 +195,7 @@ def fix_imports_inside_blocks(source: str) -> str: Only converts indented imports (those starting with whitespace), preserving top-level imports which are valid ESM syntax. """ - # Convert: import X from 'Y' -> const X = require('Y') - source = _INDENTED_DEFAULT_IMPORT_RE.sub(r"\1const \2 = require('\3');", source) - # Convert: import { X, Y } from 'Z' -> const { X, Y } = require('Z') - source = _INDENTED_NAMED_IMPORT_RE.sub(r"\1const {\2} = require('\3');", source) - # Convert: import * as X from 'Y' -> const X = require('Y') - return _INDENTED_NAMESPACE_IMPORT_RE.sub(r"\1const \2 = require('\3');", source) + return _COMBINED_IMPORT_RE.sub(_replace_import, source) # Patterns for normalizing codeflash imports (legacy -> npm package) @@ -682,3 +686,31 @@ def _find_block_end(source: str, start: int) -> int: i += 1 return start + + +def _replace_import(match: re.Match) -> str: + groups = match.groups() + indent = groups[0] + + # default import + if groups[1]: + return f"{indent}const {groups[1]} = require('{groups[2]}');" + # named import + if groups[3]: + return f"{indent}const {{{groups[3]}}} = require('{groups[4]}');" + # namespace import + return f"{indent}const {groups[5]} = require('{groups[6]}');" + + +def _replace_import(match: re.Match) -> str: + groups = match.groups() + indent = groups[0] + + # default import + if groups[1]: + return f"{indent}const {groups[1]} = require('{groups[2]}');" + # named import + if groups[3]: + return f"{indent}const {{{groups[3]}}} = require('{groups[4]}');" + # namespace import + return f"{indent}const {groups[5]} = require('{groups[6]}');" From 1cd08307ce8a07fe42b5882b6c432e718417ded0 Mon Sep 17 00:00:00 2001 From: "claude[bot]" <41898282+claude[bot]@users.noreply.github.com> Date: Wed, 25 Mar 2026 17:29:48 +0000 Subject: [PATCH 2/2] fix: remove duplicate _replace_import definition and fix re.Match type annotation --- codeflash/languages/javascript/edit_tests.py | 16 +--------------- 1 file changed, 1 insertion(+), 15 deletions(-) diff --git a/codeflash/languages/javascript/edit_tests.py b/codeflash/languages/javascript/edit_tests.py index 4d473ad83..92d9e520d 100644 --- a/codeflash/languages/javascript/edit_tests.py +++ b/codeflash/languages/javascript/edit_tests.py @@ -688,21 +688,7 @@ def _find_block_end(source: str, start: int) -> int: return start -def _replace_import(match: re.Match) -> str: - groups = match.groups() - indent = groups[0] - - # default import - if groups[1]: - return f"{indent}const {groups[1]} = require('{groups[2]}');" - # named import - if groups[3]: - return f"{indent}const {{{groups[3]}}} = require('{groups[4]}');" - # namespace import - return f"{indent}const {groups[5]} = require('{groups[6]}');" - - -def _replace_import(match: re.Match) -> str: +def _replace_import(match: re.Match[str]) -> str: groups = match.groups() indent = groups[0]