Skip to content

Comments

feat: add .call() pattern support to JS test instrumentation#1523

Open
mohammedahmed18 wants to merge 5 commits intomainfrom
feat/js-dot-call-instrumentation
Open

feat: add .call() pattern support to JS test instrumentation#1523
mohammedahmed18 wants to merge 5 commits intomainfrom
feat/js-dot-call-instrumentation

Conversation

@mohammedahmed18
Copy link
Contributor

Summary

  • Add instrumentation support for the .call() pattern (funcName.call(thisArg, args)) in JavaScript test code
  • Transform .call() invocations to codeflash.capture('name', 'id', func.bind(thisArg), args) in both standalone and expect-wrapped contexts
  • Add split_call_args() helper to correctly parse .call() arguments handling nested parens, brackets, braces, and string literals

Test plan

  • All 69 existing + new instrumentation tests pass
  • No regressions in assertion removal tests
  • Full integration test with real-world IdempotencyInterceptor.prototype.getIdempotencyKey.call() pattern

🤖 Generated with Claude Code

mohammedahmed18 and others added 2 commits February 18, 2026 14:46
Instrument funcName.call(thisArg, args) patterns in both standalone and
expect-wrapped contexts, transforming them to codeflash.capture() with
func.bind(thisArg).

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@claude
Copy link
Contributor

claude bot commented Feb 18, 2026

PR Review Summary

Prek Checks

✅ All checks pass — ruff check and ruff format both passed with no issues.

Mypy

✅ No new mypy issues introduced by this PR. Pre-existing issues only:

  • import-untyped for codeflash.code_utils.code_position (project-wide)
  • no-untyped-def on test methods (matches existing test style in the file)

Code Review

✅ No critical issues found. Changes reviewed:

  • split_call_args() — Correctly handles nested parens, brackets, braces, and string literals when splitting .call() arguments
  • .call() pattern detection — Both StandaloneCallTransformer and ExpectCallTransformer properly detect and transform funcName.call(thisArg, args) patterns
  • _is_function_used_in_test() — Correctly detects .call() usage patterns for function discovery
  • Performance optimization (commit 41f14f76) — Incremental string state tracking in ExpectCallTransformer.transform avoids O(n²) rescanning
  • Type annotations (commit 1136a797) — re.Matchre.Match[str] is a correct style improvement

Test Coverage

File Main PR Change
codeflash/languages/javascript/instrument.py 67% 76% +9%
tests/test_languages/test_javascript_instrumentation.py 100% 100%
  • Coverage improved by 9 percentage points for the modified source file
  • 25 new test methods added covering .call() patterns (unit + integration)
  • All 69 tests pass (44 existing + 25 new)
  • 8 unrelated test failures in tests/test_tracer.py (pre-existing, not related to this PR)

Codeflash Optimization PRs


Last updated: 2026-02-18T14:30 UTC

@codeflash-ai
Copy link
Contributor

codeflash-ai bot commented Feb 18, 2026

⚡️ Codeflash found optimizations for this PR

📄 147% (1.47x) speedup for StandaloneCallTransformer._parse_bracket_standalone_call in codeflash/languages/javascript/instrument.py

⏱️ Runtime : 1.72 milliseconds 697 microseconds (best of 144 runs)

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

If you approve, it will be merged into this PR (branch feat/js-dot-call-instrumentation).

Static Badge

The optimized code achieves a **172% speedup** (85.2ms → 31.3ms) by eliminating a critical O(n²) performance bottleneck in the `transform()` method.

## Key Optimization

**Problem**: The original code called `is_inside_string(code, match.start())` for every regex match found. This function scans from position 0 to the match position each time, resulting in O(n²) complexity when processing code with many matches.

**Solution**: The optimization replaces these repeated scans with **incremental string state tracking** directly in the main loop. Instead of rescanning from the beginning for each match, the code maintains `in_string`, `string_char`, and `last_checked_pos` variables that preserve state between iterations. When a new match is found, only the code between `last_checked_pos` and `match_start` is scanned to update the string state.

## Performance Impact

The line profiler data clearly shows the improvement:
- **Original**: `is_inside_string()` consumed 0.618s (95.2% of transform time) with 432 calls scanning 887,510 characters total
- **Optimized**: The inline tracking logic in transform() consumes only 0.021s (33% of transform time) by scanning just 28,273 characters incrementally

Test results demonstrate strong gains on workloads with many matches:
- `test_transform_many_invocations`: 12.3ms → 4.84ms (155% faster)
- `test_transform_large_code_file`: 40.6ms → 14.0ms (191% faster)
- `test_transform_alternating_patterns`: 2.64ms → 519μs (408% faster)
- `test_transform_mixed_qualified_names`: 14.0ms → 5.14ms (173% faster)

The optimization particularly benefits code with frequent expect() calls, as each avoided `is_inside_string()` call saves scanning hundreds or thousands of characters. This makes the transformer significantly faster on realistic test files with multiple assertions.
@codeflash-ai
Copy link
Contributor

codeflash-ai bot commented Feb 18, 2026

⚡️ Codeflash found optimizations for this PR

📄 173% (1.73x) speedup for ExpectCallTransformer.transform in codeflash/languages/javascript/instrument.py

⏱️ Runtime : 85.2 milliseconds 31.3 milliseconds (best of 65 runs)

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

If you approve, it will be merged into this PR (branch feat/js-dot-call-instrumentation).

Static Badge

…2026-02-18T13.29.34

⚡️ Speed up method `ExpectCallTransformer.transform` by 173% in PR #1523 (`feat/js-dot-call-instrumentation`)
@codeflash-ai
Copy link
Contributor

codeflash-ai bot commented Feb 18, 2026

@codeflash-ai
Copy link
Contributor

codeflash-ai bot commented Feb 18, 2026

⚡️ Codeflash found optimizations for this PR

📄 15,016% (150.16x) speedup for ExpectCallTransformer._parse_expect_call in codeflash/languages/javascript/instrument.py

⏱️ Runtime : 659 microseconds 4.36 microseconds (best of 5 runs)

A new Optimization Review has been created.

🔗 Review here

Static Badge

@codeflash-ai
Copy link
Contributor

codeflash-ai bot commented Feb 18, 2026

⚡️ Codeflash found optimizations for this PR

📄 772% (7.72x) speedup for ExpectCallTransformer._parse_expect_dot_call in codeflash/languages/javascript/instrument.py

⏱️ Runtime : 6.67 milliseconds 764 microseconds (best of 37 runs)

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

If you approve, it will be merged into this PR (branch feat/js-dot-call-instrumentation).

Static Badge

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