feat: add early module import validation for JavaScript/TypeScript#1756
feat: add early module import validation for JavaScript/TypeScript#1756aseembits93 wants to merge 2 commits intomainfrom
Conversation
Validate that the source file is within the configured module root before making expensive AI API calls. If misconfigured, auto-correct by inferring the module root from package.json fields and directory conventions (src/, lib/, etc.), matching the existing Python validation pattern. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
|
Claude finished @aseembits93's task in 8m 58s —— View job PR Review Summary
Prek ChecksRuff check and format both pass. Two mypy errors were found in the new code and auto-fixed in commit
Code ReviewBug / Dead code — self.args.project_root = project_root # project_root = self.project_root — unchanged
self.project_root = project_root # same: self.project_root = self.project_root
Double validation — Side effect concern — auto-modifying Late import of from codeflash.code_utils.config_js import PACKAGE_JSON_DATA_CACHE
PACKAGE_JSON_DATA_CACHE.pop(package_json_path, None)This import is inside a Duplicate DetectionMEDIUM confidence — No other duplicates detected. Test Coverage
The one uncovered line is All 16 new tests pass. Last updated: 2026-03-04T03:22Z |
- Add null guards for self.args in try_correct_module_root() and can_be_optimized() - Fix Language import to use codeflash.languages.language_enum Co-authored-by: Aseem Saxena <undefined@users.noreply.github.com>
| from pathlib import Path | ||
|
|
||
|
|
||
| def validate_js_module_resolution(source_file: Path, project_root: Path, module_root: Path) -> tuple[bool, str]: |
There was a problem hiding this comment.
Best to place and dedupe with logic present in file 'init_javascript.py'
| return True, "" | ||
|
|
||
|
|
||
| def infer_js_module_root(source_file: Path, project_root: Path | None = None) -> Path: |
There was a problem hiding this comment.
There are functions for this purpose we can strength the logic there.
collect_js_setup_info
There was a problem hiding this comment.
or look into detect_module_root in config_js.py
Summary
moduleRootpackage.jsonfields and directory conventions (src/,lib/, etc.), and writes the corrected value back topackage.jsonChanges
codeflash/code_utils/config_js_validation.py—validate_js_module_resolution()andinfer_js_module_root()utilitiescodeflash/languages/javascript/function_optimizer.py— Addedtry_correct_module_root()andcan_be_optimized()override toJavaScriptFunctionOptimizertests/code_utils/test_config_js_validation.py— 10 unit tests for validation utilitiestests/test_languages/test_js_function_optimizer_validation.py— 6 integration tests for optimizer methodsTest plan
uv run pytest tests/code_utils/test_config_js_validation.py -v— 10/10 passuv run pytest tests/test_languages/test_js_function_optimizer_validation.py -v— 6/6 passuv run pytest tests/test_languages/test_javascript_test_runner.py -v— 29/29 pass (no regressions)uv run ruff checkanduv run ruff format --check— all clean🤖 Generated with Claude Code