Skip to content

Commit 2c95695

Browse files
authored
Merge branch 'main' into js-suggester
2 parents 941da6c + e957843 commit 2c95695

File tree

7 files changed

+464
-47
lines changed

7 files changed

+464
-47
lines changed

code_to_optimize/js/code_to_optimize_ts/package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

codeflash/languages/current.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,6 @@ def set_current_language(language: Language | str) -> None:
5858
5959
"""
6060
global _current_language
61-
62-
if _current_language is not None:
63-
return
6461
_current_language = Language(language) if isinstance(language, str) else language
6562

6663

codeflash/languages/javascript/module_system.py

Lines changed: 70 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
import re
1212
from typing import TYPE_CHECKING
1313

14+
from codeflash.languages.current import is_typescript
15+
1416
if TYPE_CHECKING:
1517
from pathlib import Path
1618

@@ -307,36 +309,89 @@ def replace_default(match) -> str:
307309
return default_import.sub(replace_default, code)
308310

309311

310-
def ensure_module_system_compatibility(code: str, target_module_system: str) -> str:
312+
def uses_ts_jest(project_root: Path) -> bool:
313+
"""Check if the project uses ts-jest for TypeScript transformation.
314+
315+
ts-jest handles module interoperability internally, allowing mixed
316+
CommonJS/ESM imports without explicit conversion.
317+
318+
Args:
319+
project_root: The project root directory.
320+
321+
Returns:
322+
True if ts-jest is being used, False otherwise.
323+
324+
"""
325+
# Check for ts-jest in devDependencies or dependencies
326+
package_json = project_root / "package.json"
327+
if package_json.exists():
328+
try:
329+
with package_json.open("r") as f:
330+
pkg = json.load(f)
331+
dev_deps = pkg.get("devDependencies", {})
332+
deps = pkg.get("dependencies", {})
333+
if "ts-jest" in dev_deps or "ts-jest" in deps:
334+
return True
335+
except Exception as e:
336+
logger.debug(f"Failed to read package.json for ts-jest detection: {e}") # noqa: G004
337+
338+
# Also check for jest.config with ts-jest preset
339+
for config_file in ["jest.config.js", "jest.config.cjs", "jest.config.ts", "jest.config.mjs"]:
340+
config_path = project_root / config_file
341+
if config_path.exists():
342+
try:
343+
content = config_path.read_text()
344+
if "ts-jest" in content:
345+
return True
346+
except Exception as e:
347+
logger.debug(f"Failed to read {config_file}: {e}") # noqa: G004
348+
349+
return False
350+
351+
352+
def ensure_module_system_compatibility(code: str, target_module_system: str, project_root: Path | None = None) -> str:
311353
"""Ensure code uses the correct module system syntax.
312354
313-
Detects the current module system in the code and converts if needed.
314-
Handles mixed-style code (e.g., ESM imports with CommonJS require for npm packages).
355+
If the project uses ts-jest, no conversion is performed because ts-jest
356+
handles module interoperability internally. Otherwise, converts between
357+
CommonJS and ES Modules as needed.
315358
316359
Args:
317360
code: JavaScript code to check and potentially convert.
318361
target_module_system: Target ModuleSystem (COMMONJS or ES_MODULE).
362+
project_root: Project root directory for ts-jest detection.
319363
320364
Returns:
321-
Code with correct module system syntax.
365+
Converted code, or unchanged if ts-jest handles interop.
322366
323367
"""
368+
# If ts-jest is installed, skip conversion - it handles interop natively
369+
if is_typescript() and project_root and uses_ts_jest(project_root):
370+
logger.debug(
371+
f"Skipping module system conversion (target was {target_module_system}). " # noqa: G004
372+
"ts-jest handles interop natively."
373+
)
374+
return code
375+
324376
# Detect current module system in code
325377
has_require = "require(" in code
378+
has_module_exports = "module.exports" in code or "exports." in code
326379
has_import = "import " in code and "from " in code
380+
has_export = "export " in code
381+
382+
is_commonjs = has_require or has_module_exports
383+
is_esm = has_import or has_export
384+
385+
# Convert if needed
386+
if target_module_system == ModuleSystem.ES_MODULE and is_commonjs and not is_esm:
387+
logger.debug("Converting CommonJS to ES Module syntax")
388+
return convert_commonjs_to_esm(code)
327389

328-
if target_module_system == ModuleSystem.ES_MODULE:
329-
# Convert any require() statements to imports for ESM projects
330-
# This handles mixed code (ESM imports + CommonJS requires for npm packages)
331-
if has_require:
332-
logger.debug("Converting CommonJS requires to ESM imports")
333-
return convert_commonjs_to_esm(code)
334-
elif target_module_system == ModuleSystem.COMMONJS:
335-
# Convert any import statements to requires for CommonJS projects
336-
if has_import:
337-
logger.debug("Converting ESM imports to CommonJS requires")
338-
return convert_esm_to_commonjs(code)
390+
if target_module_system == ModuleSystem.COMMONJS and is_esm and not is_commonjs:
391+
logger.debug("Converting ES Module to CommonJS syntax")
392+
return convert_esm_to_commonjs(code)
339393

394+
logger.debug("No module system conversion needed")
340395
return code
341396

342397

codeflash/languages/treesitter_utils.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,11 @@ def _walk_tree_for_functions(
258258
if func_info.is_arrow and not include_arrow_functions:
259259
should_include = False
260260

261+
# Skip arrow functions that are object properties (e.g., { foo: () => {} })
262+
# These are not standalone functions - they're values in object literals
263+
if func_info.is_arrow and node.parent and node.parent.type == "pair":
264+
should_include = False
265+
261266
if should_include:
262267
functions.append(func_info)
263268

codeflash/verification/verifier.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,10 @@ def generate_tests(
8282
)
8383

8484
# Convert module system if needed (e.g., CommonJS -> ESM for ESM projects)
85-
generated_test_source = ensure_module_system_compatibility(generated_test_source, project_module_system)
85+
# Skip conversion if ts-jest is installed (handles interop natively)
86+
generated_test_source = ensure_module_system_compatibility(
87+
generated_test_source, project_module_system, test_cfg.tests_project_rootdir
88+
)
8689

8790
# Ensure vitest imports are present when using vitest framework
8891
generated_test_source = ensure_vitest_imports(generated_test_source, test_cfg.test_framework)

0 commit comments

Comments
 (0)