codeflash/codeflash/languages/javascript/optimizer.py
ali e1860a5637
feat: early exit on JS setup failures and introduce SetupError type
setup_test_config now returns bool so the optimizer aborts when JS
requirements (Node, npm, test framework) are missing instead of
silently continuing. Adds SetupError dataclass, fixes warning log to
print messages, and updates tests.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-31 08:04:00 +02:00

58 lines
2.2 KiB
Python

from __future__ import annotations
from typing import TYPE_CHECKING
from codeflash.cli_cmds.console import logger
from codeflash.languages.base import SetupError
from codeflash.models.models import ValidCode
if TYPE_CHECKING:
from pathlib import Path
from codeflash.verification.verification_utils import TestConfig
def prepare_javascript_module(
original_module_code: str, original_module_path: Path
) -> tuple[dict[Path, ValidCode], None]:
"""Prepare a JavaScript/TypeScript module for optimization.
Unlike Python, JS/TS doesn't need AST parsing or import analysis at this stage.
Returns a mapping of the file path to ValidCode with the source as-is.
"""
validated_original_code: dict[Path, ValidCode] = {
original_module_path: ValidCode(source_code=original_module_code, normalized_code=original_module_code)
}
return validated_original_code, None
def verify_js_requirements(test_cfg: TestConfig) -> list[SetupError]:
"""Verify JavaScript/TypeScript requirements before optimization.
Checks that Node.js, npm, and the test framework are available.
Logs warnings if requirements are not met but does not abort.
Returns: List of setup errors if requirements are not met, empty list otherwise.
"""
from codeflash.languages import get_language_support
from codeflash.languages.base import Language
from codeflash.languages.test_framework import get_js_test_framework_or_default
js_project_root = test_cfg.js_project_root
if not js_project_root:
return [SetupError("JavaScript project root not found", should_abort=True)]
try:
js_support = get_language_support(Language.JAVASCRIPT)
test_framework = get_js_test_framework_or_default()
success, errors = js_support.verify_requirements(js_project_root, test_framework)
if not success:
logger.warning("JavaScript requirements check found issues:")
for error in errors:
logger.warning(f" - {error.message}")
return errors
return []
except Exception as e:
logger.debug(f"Failed to verify JS requirements: {e}")
return [SetupError(str(e), should_abort=True)]