|
11 | 11 | import re |
12 | 12 | from typing import TYPE_CHECKING |
13 | 13 |
|
| 14 | +from codeflash.languages.current import is_typescript |
| 15 | + |
14 | 16 | if TYPE_CHECKING: |
15 | 17 | from pathlib import Path |
16 | 18 |
|
@@ -307,36 +309,89 @@ def replace_default(match) -> str: |
307 | 309 | return default_import.sub(replace_default, code) |
308 | 310 |
|
309 | 311 |
|
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: |
311 | 353 | """Ensure code uses the correct module system syntax. |
312 | 354 |
|
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. |
315 | 358 |
|
316 | 359 | Args: |
317 | 360 | code: JavaScript code to check and potentially convert. |
318 | 361 | target_module_system: Target ModuleSystem (COMMONJS or ES_MODULE). |
| 362 | + project_root: Project root directory for ts-jest detection. |
319 | 363 |
|
320 | 364 | Returns: |
321 | | - Code with correct module system syntax. |
| 365 | + Converted code, or unchanged if ts-jest handles interop. |
322 | 366 |
|
323 | 367 | """ |
| 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 | + |
324 | 376 | # Detect current module system in code |
325 | 377 | has_require = "require(" in code |
| 378 | + has_module_exports = "module.exports" in code or "exports." in code |
326 | 379 | 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) |
327 | 389 |
|
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) |
339 | 393 |
|
| 394 | + logger.debug("No module system conversion needed") |
340 | 395 | return code |
341 | 396 |
|
342 | 397 |
|
|
0 commit comments