-
Notifications
You must be signed in to change notification settings - Fork 22
feat: add early module import validation for JavaScript/TypeScript #1756
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
aseembits93
wants to merge
2
commits into
main
Choose a base branch
from
fix/early-module-import-validation-js
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
| """JavaScript/TypeScript module resolution validation utilities.""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from typing import TYPE_CHECKING | ||
|
|
||
| from codeflash.code_utils.config_js import detect_module_root, get_package_json_data | ||
| from codeflash.languages.javascript.test_runner import find_node_project_root | ||
|
|
||
| if TYPE_CHECKING: | ||
| from pathlib import Path | ||
|
|
||
|
|
||
| def validate_js_module_resolution(source_file: Path, project_root: Path, module_root: Path) -> tuple[bool, str]: | ||
| """Validate that a JS/TS source file can be resolved within the configured module root. | ||
|
|
||
| Checks: | ||
| 1. Source file exists | ||
| 2. Source file is within project_root | ||
| 3. package.json exists in project_root | ||
| 4. Source file is within module_root | ||
|
|
||
| Returns: | ||
| (True, "") on success, (False, error_message) on failure. | ||
|
|
||
| """ | ||
| source_file = source_file.resolve() | ||
| project_root = project_root.resolve() | ||
| module_root = module_root.resolve() | ||
|
|
||
| if not source_file.exists(): | ||
| return False, f"Source file does not exist: {source_file}" | ||
|
|
||
| try: | ||
| source_file.relative_to(project_root) | ||
| except ValueError: | ||
| return False, f"Source file {source_file} is not within project root {project_root}" | ||
|
|
||
| package_json = project_root / "package.json" | ||
| if not package_json.exists(): | ||
| return False, f"No package.json found at {project_root}" | ||
|
|
||
| try: | ||
| source_file.relative_to(module_root) | ||
| except ValueError: | ||
| return False, ( | ||
| f"Source file {source_file} is not within module root {module_root}. " | ||
| f"Check the 'codeflash.moduleRoot' setting in package.json." | ||
| ) | ||
|
|
||
| return True, "" | ||
|
|
||
|
|
||
| def infer_js_module_root(source_file: Path, project_root: Path | None = None) -> Path: | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There are functions for this purpose we can strength the logic there.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. or look into |
||
| """Infer the JavaScript/TypeScript module root for a source file. | ||
|
|
||
| Uses find_node_project_root to locate package.json, then detect_module_root | ||
| to determine the module root from package.json fields and directory conventions. | ||
|
|
||
| Falls back to the source file's parent directory if no package.json is found. | ||
|
|
||
| Returns: | ||
| Absolute path to the inferred module root. | ||
|
|
||
| """ | ||
| source_file = source_file.resolve() | ||
|
|
||
| if project_root is None: | ||
| project_root = find_node_project_root(source_file) | ||
|
|
||
| if project_root is None: | ||
| return source_file.parent | ||
|
|
||
| project_root = project_root.resolve() | ||
| package_json_path = project_root / "package.json" | ||
| package_data = get_package_json_data(package_json_path) | ||
|
|
||
| if package_data is None: | ||
| return project_root | ||
|
|
||
| detected = detect_module_root(project_root, package_data) | ||
| return (project_root / detected).resolve() | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,128 @@ | ||
| """Tests for JavaScript/TypeScript module resolution validation utilities.""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import json | ||
| from typing import TYPE_CHECKING | ||
|
|
||
| from codeflash.code_utils.config_js_validation import infer_js_module_root, validate_js_module_resolution | ||
|
|
||
| if TYPE_CHECKING: | ||
| from pathlib import Path | ||
|
|
||
|
|
||
| class TestValidateJsModuleResolution: | ||
| def test_valid_source_in_module_root(self, tmp_path: Path) -> None: | ||
| project_root = tmp_path / "project" | ||
| project_root.mkdir() | ||
| (project_root / "package.json").write_text("{}", encoding="utf-8") | ||
| src = project_root / "src" | ||
| src.mkdir() | ||
| source_file = src / "index.js" | ||
| source_file.write_text("export function foo() {}", encoding="utf-8") | ||
|
|
||
| valid, error = validate_js_module_resolution(source_file, project_root, src) | ||
| assert valid is True | ||
| assert error == "" | ||
|
|
||
| def test_source_does_not_exist(self, tmp_path: Path) -> None: | ||
| project_root = tmp_path / "project" | ||
| project_root.mkdir() | ||
| (project_root / "package.json").write_text("{}", encoding="utf-8") | ||
| source_file = project_root / "src" / "missing.js" | ||
|
|
||
| valid, error = validate_js_module_resolution(source_file, project_root, project_root) | ||
| assert valid is False | ||
| assert "does not exist" in error | ||
|
|
||
| def test_source_outside_project_root(self, tmp_path: Path) -> None: | ||
| project_root = tmp_path / "project" | ||
| project_root.mkdir() | ||
| (project_root / "package.json").write_text("{}", encoding="utf-8") | ||
| outside_file = tmp_path / "outside.js" | ||
| outside_file.write_text("export function foo() {}", encoding="utf-8") | ||
|
|
||
| valid, error = validate_js_module_resolution(outside_file, project_root, project_root) | ||
| assert valid is False | ||
| assert "not within project root" in error | ||
|
|
||
| def test_no_package_json(self, tmp_path: Path) -> None: | ||
| project_root = tmp_path / "project" | ||
| project_root.mkdir() | ||
| source_file = project_root / "index.js" | ||
| source_file.write_text("export function foo() {}", encoding="utf-8") | ||
|
|
||
| valid, error = validate_js_module_resolution(source_file, project_root, project_root) | ||
| assert valid is False | ||
| assert "No package.json" in error | ||
|
|
||
| def test_source_outside_module_root(self, tmp_path: Path) -> None: | ||
| project_root = tmp_path / "project" | ||
| project_root.mkdir() | ||
| (project_root / "package.json").write_text("{}", encoding="utf-8") | ||
| src = project_root / "src" | ||
| src.mkdir() | ||
| other = project_root / "other" | ||
| other.mkdir() | ||
| source_file = other / "index.js" | ||
| source_file.write_text("export function foo() {}", encoding="utf-8") | ||
|
|
||
| valid, error = validate_js_module_resolution(source_file, project_root, src) | ||
| assert valid is False | ||
| assert "not within module root" in error | ||
| assert "moduleRoot" in error | ||
|
|
||
| def test_module_root_equals_project_root(self, tmp_path: Path) -> None: | ||
| project_root = tmp_path / "project" | ||
| project_root.mkdir() | ||
| (project_root / "package.json").write_text("{}", encoding="utf-8") | ||
| source_file = project_root / "index.js" | ||
| source_file.write_text("export function foo() {}", encoding="utf-8") | ||
|
|
||
| valid, error = validate_js_module_resolution(source_file, project_root, project_root) | ||
| assert valid is True | ||
| assert error == "" | ||
|
|
||
|
|
||
| class TestInferJsModuleRoot: | ||
| def test_infers_src_directory(self, tmp_path: Path) -> None: | ||
| project_root = tmp_path / "project" | ||
| project_root.mkdir() | ||
| src = project_root / "src" | ||
| src.mkdir() | ||
| source_file = src / "index.js" | ||
| source_file.write_text("export function foo() {}", encoding="utf-8") | ||
| (project_root / "package.json").write_text("{}", encoding="utf-8") | ||
|
|
||
| result = infer_js_module_root(source_file, project_root) | ||
| assert result == src.resolve() | ||
|
|
||
| def test_infers_from_package_json_main_field(self, tmp_path: Path) -> None: | ||
| project_root = tmp_path / "project" | ||
| project_root.mkdir() | ||
| app = project_root / "app" | ||
| app.mkdir() | ||
| source_file = app / "index.js" | ||
| source_file.write_text("export function foo() {}", encoding="utf-8") | ||
| (project_root / "package.json").write_text(json.dumps({"main": "app/index.js"}), encoding="utf-8") | ||
|
|
||
| result = infer_js_module_root(source_file, project_root) | ||
| assert result == app.resolve() | ||
|
|
||
| def test_falls_back_to_project_root(self, tmp_path: Path) -> None: | ||
| project_root = tmp_path / "project" | ||
| project_root.mkdir() | ||
| source_file = project_root / "index.js" | ||
| source_file.write_text("export function foo() {}", encoding="utf-8") | ||
| (project_root / "package.json").write_text("{}", encoding="utf-8") | ||
|
|
||
| result = infer_js_module_root(source_file, project_root) | ||
| assert result == project_root.resolve() | ||
|
|
||
| def test_falls_back_to_parent_without_package_json(self, tmp_path: Path) -> None: | ||
| source_file = tmp_path / "standalone" / "index.js" | ||
| source_file.parent.mkdir(parents=True) | ||
| source_file.write_text("export function foo() {}", encoding="utf-8") | ||
|
|
||
| result = infer_js_module_root(source_file, project_root=None) | ||
| assert result == source_file.parent.resolve() |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Best to place and dedupe with logic present in file 'init_javascript.py'