Skip to content

fix: convert import statements inside function bodies to require() calls#1901

Open
mohammedahmed18 wants to merge 1 commit intomainfrom
cf-fix-imports-inside-blocks
Open

fix: convert import statements inside function bodies to require() calls#1901
mohammedahmed18 wants to merge 1 commit intomainfrom
cf-fix-imports-inside-blocks

Conversation

@mohammedahmed18
Copy link
Contributor

Summary

  • AI-generated tests sometimes place import X from 'Y' inside jest.mock() callbacks, describe() blocks, or other function bodies — this is invalid JS syntax (imports must be top-level)
  • Adds fix_imports_inside_blocks() to edit_tests.py that converts indented import statements to equivalent const X = require('Y') calls
  • Integrated into postprocess_generated_tests() so all generated tests are automatically fixed
  • Handles default imports (import X from 'Y'), named imports (import { X } from 'Y'), and namespace imports (import * as X from 'Y')
  • ~79/1026 Strapi log files showed SWC "import/export cannot be used outside of module code" errors

Test plan

  • Unit tests for all import patterns (default, named, namespace)
  • Tests verify top-level imports are preserved
  • Tests verify mixed scenarios work correctly
  • uv run prek passes

🤖 Generated with Claude Code

@claude
Copy link
Contributor

claude bot commented Mar 25, 2026

Claude finished @mohammedahmed18's task in 1m 38s —— View job


PR Review Summary

  • Triage PR size — SMALL (41 lines of production code)
  • Run lint/typecheck — prek passed, no issues
  • Resolve stale review threads — none found
  • Review code
  • Duplicate detection
  • Post summary

Prek Checks

✅ All checks passed (ruff check, ruff format). No fixes needed.


Code Review

One style violation:

fix_imports_inside_blocks in edit_tests.py:168 has a docstring. Per the repo's code-style guidelines, docstrings should not be added to new/changed code unless explicitly requested. The function name is already self-documenting, and the logic comments above it cover the "why". Fix this →

One design note:

The indentation-based heuristic (any import line starting with whitespace is "inside a block") is a reasonable approximation for AI-generated tests. The edge case where a developer manually indents a top-level import would incorrectly convert it, but that's an extremely rare JS style and not a practical concern for this use case.

Everything else looks correct:

  • Regex patterns handle all three import forms correctly (default, named, namespace)
  • Top-level imports are preserved (no leading whitespace → no match)
  • Named imports retain internal spacing via the captured group (e.g., import { X, Y }const { X, Y } = require(...))
  • Model fields (generated_original_test_source, instrumented_behavior_test_source, instrumented_perf_test_source) are all str, so no None-safety concerns
  • Integration point in postprocess_generated_tests is correct — applied after all other transforms

Duplicate Detection

No duplicates detected. The function is unique to the JS language module.


@codeflash-ai
Copy link
Contributor

codeflash-ai bot commented Mar 25, 2026

⚡️ Codeflash found optimizations for this PR

📄 171% (1.71x) speedup for fix_imports_inside_blocks in codeflash/languages/javascript/edit_tests.py

⏱️ Runtime : 3.59 milliseconds 1.32 milliseconds (best of 88 runs)

A dependent PR with the suggested changes has been created. Please review:

If you approve, it will be merged into this PR (branch cf-fix-imports-inside-blocks).

Static Badge

mohammedahmed18 added a commit that referenced this pull request Mar 25, 2026
…2026-03-25T17.27.15

⚡️ Speed up function `fix_imports_inside_blocks` by 171% in PR #1901 (`cf-fix-imports-inside-blocks`)
@codeflash-ai
Copy link
Contributor

codeflash-ai bot commented Mar 25, 2026

AI-generated tests sometimes place `import X from 'Y'` inside jest.mock()
callbacks, describe() blocks, or other function bodies. This is invalid
JavaScript syntax (import must be top-level). Add a post-processing step
that converts indented import statements to equivalent require() calls.

Affects ~79/1026 Strapi log files showing "import/export cannot be used
outside of module code" SWC syntax errors.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@mohammedahmed18 mohammedahmed18 force-pushed the cf-fix-imports-inside-blocks branch from 5a05a77 to c9855e4 Compare March 25, 2026 18:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant