Skip to content

fix: validate JS/TS functions are exported before optimization#1214

Merged
misrasaurabh1 merged 2 commits intomainfrom
fix/validate-js-function-exports
Jan 31, 2026
Merged

fix: validate JS/TS functions are exported before optimization#1214
misrasaurabh1 merged 2 commits intomainfrom
fix/validate-js-function-exports

Conversation

@misrasaurabh1
Copy link
Contributor

Summary

  • Adds validation to check if JavaScript/TypeScript functions are exported before attempting optimization
  • Non-exported functions cannot be imported by tests, making optimization impossible
  • Shows a clear error message explaining the issue and how to fix it

Problem

Previously, codeflash would generate tests that try to import non-exported functions, leading to confusing runtime errors like:

TypeError: codeflash.capture: Expected function 'foo' but got undefined.
This usually means the function was not imported correctly.

Solution

Now codeflash validates export status early and shows a helpful error message:

Function 'getEnvLogLevel' is not exported from src/utils/AppUtils.ts.
In JavaScript/TypeScript, only exported functions can be optimized
because tests need to import them.
To fix: Add 'export' keyword to the function declaration, e.g.:
  export const getEnvLogLevel = ...

Test plan

  • Existing tests pass (pytest tests/test_languages/test_import_resolver.py -v)
  • Manual testing with non-exported function shows correct error message
  • Manual testing with exported function proceeds to optimization normally

🤖 Generated with Claude Code

For JavaScript and TypeScript, non-exported functions cannot be imported
by tests, making optimization impossible. Previously, codeflash would
generate tests that try to import non-exported functions, leading to
confusing runtime errors like "Expected function 'foo' but got undefined".

This change adds early validation when the user specifies a function to
optimize via --function flag. If the function is not exported, codeflash
now shows a clear error message explaining the issue and how to fix it.

Example error message:
> Function 'getEnvLogLevel' is not exported from src/utils/AppUtils.ts.
> In JavaScript/TypeScript, only exported functions can be optimized
> because tests need to import them.
> To fix: Add 'export' keyword to the function declaration, e.g.:
>   export const getEnvLogLevel = ...

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
@misrasaurabh1 misrasaurabh1 force-pushed the fix/validate-js-function-exports branch from a5ddcd2 to f4482d8 Compare January 31, 2026 03:11
@misrasaurabh1 misrasaurabh1 merged commit 7e34379 into main Jan 31, 2026
23 of 26 checks passed
@misrasaurabh1 misrasaurabh1 deleted the fix/validate-js-function-exports branch January 31, 2026 04:15
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