Skip to content

Commit f4482d8

Browse files
misrasaurabh1claude
andcommitted
fix: validate JS/TS functions are exported before optimization
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>
1 parent 13e35ca commit f4482d8

1 file changed

Lines changed: 61 additions & 0 deletions

File tree

codeflash/discovery/functions_to_optimize.py

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,32 @@ def get_files_for_language(
216216
return files
217217

218218

219+
def _is_js_ts_function_exported(file_path: Path, function_name: str) -> tuple[bool, str | None]:
220+
"""Check if a JavaScript/TypeScript function is exported from its module.
221+
222+
For JS/TS, functions that are not exported cannot be imported by tests,
223+
making them impossible to optimize.
224+
225+
Args:
226+
file_path: Path to the source file.
227+
function_name: Name of the function to check.
228+
229+
Returns:
230+
Tuple of (is_exported, export_name). export_name may be 'default' for default exports.
231+
232+
"""
233+
from codeflash.languages.treesitter_utils import get_analyzer_for_file
234+
235+
try:
236+
source = file_path.read_text(encoding="utf-8")
237+
analyzer = get_analyzer_for_file(file_path)
238+
return analyzer.is_function_exported(source, function_name)
239+
except Exception as e:
240+
logger.debug(f"Failed to check export status for {function_name}: {e}")
241+
# Return True to avoid blocking in case of errors
242+
return True, None
243+
244+
219245
def _find_all_functions_in_python_file(file_path: Path) -> dict[Path, list[FunctionToOptimize]]:
220246
"""Find all optimizable functions in a Python file using AST parsing.
221247
@@ -338,6 +364,41 @@ def get_functions_to_optimize(
338364
exit_with_message(
339365
f"Function {only_get_this_function} not found in file {file}\nor the function does not have a 'return' statement or is a property"
340366
)
367+
368+
# For JavaScript/TypeScript, verify that the function (or its parent class) is exported
369+
# Non-exported functions cannot be imported by tests
370+
if found_function.language in ("javascript", "typescript"):
371+
# For class methods, check if the parent class is exported
372+
# For standalone functions, check if the function itself is exported
373+
if found_function.parents:
374+
# It's a class method - check if the class is exported
375+
name_to_check = found_function.top_level_parent_name
376+
else:
377+
# It's a standalone function - check if the function is exported
378+
name_to_check = found_function.function_name
379+
380+
is_exported, export_name = _is_js_ts_function_exported(file, name_to_check)
381+
if not is_exported:
382+
if is_lsp:
383+
return functions, 0, None
384+
if found_function.parents:
385+
exit_with_message(
386+
f"Class '{name_to_check}' containing method '{found_function.function_name}' "
387+
f"is not exported from {file}.\n"
388+
f"In JavaScript/TypeScript, only exported classes/functions can be optimized "
389+
f"because tests need to import them.\n"
390+
f"To fix: Add 'export' keyword to the class declaration, e.g.:\n"
391+
f" export class {name_to_check} {{ ... }}"
392+
)
393+
else:
394+
exit_with_message(
395+
f"Function '{found_function.function_name}' is not exported from {file}.\n"
396+
f"In JavaScript/TypeScript, only exported functions can be optimized because "
397+
f"tests need to import them.\n"
398+
f"To fix: Add 'export' keyword to the function declaration, e.g.:\n"
399+
f" export const {found_function.function_name} = ..."
400+
)
401+
341402
functions[file] = [found_function]
342403
else:
343404
logger.info("Finding all functions modified in the current git diff ...")

0 commit comments

Comments
 (0)