Fix: Exclude property getters/setters from function discovery#1938
Fix: Exclude property getters/setters from function discovery#1938
Conversation
**Problem:**
When functions are defined as property getters/setters (e.g., inside
Object.defineProperty with `get: function getrouter() {...}`), they were
being discovered as optimizable functions. This caused test generation
to fail because:
1. Tests tried to call the getter function directly (e.g., `obj.getrouter()`)
2. But getters are not accessible by function name - only via property access
3. This resulted in "TypeError: Cannot read properties of undefined (reading 'bind')"
**Root Cause:**
The tree-sitter function discovery in `treesitter.py` was finding all
function_expression nodes, including those used as property getters/setters.
These are not standalone functions and cannot be called directly.
**Solution:**
Added a check in `_walk_tree_for_functions` to exclude function_expression
nodes that are:
- Inside a `pair` node (key-value pair in object literal)
- Where the key is "get" or "set" (property accessor pattern)
**Affected Trace IDs (from logs):**
- 11b5475c-91d9-43b1-a317-33b7f4811c52 (init.getrouter)
- 78d8f1de-541a-4208-85c7-77194c70ec72 (createETagGenerator.generateETag)
- And others with similar patterns
**Testing:**
- Added comprehensive test suite in test_property_getter_exclusion.py
- Verified fix on actual Express.js application.js (getrouter no longer discovered)
- All existing JavaScript function discovery tests pass
- Linting (prek) passes
Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
|
Claude finished @mohammedahmed18's task in 1m 30s —— View job PR Review Summary
Prek Checks✅ Code ReviewFix is correct for the stated use case, but has a potential false-positive edge case worth acknowledging. The logic ( if node.type == "function_expression" and node.parent and node.parent.type == "pair":
property_name_node = node.parent.child_by_field_name("key")
if property_name_node:
property_name = self.get_node_text(property_name_node, source_bytes)
if property_name in ("get", "set"):
should_include = FalseFalse positive concern: This check fires on any // HTTP route handlers - "get" is just a property name, not a getter
const routes = {
get: function handleGet(req, res) { return res.send("hello"); },
set: function handleSet(req, res) { return res.send("ok"); }
};Here More precise fix would be to walk up the parent chain and verify the pair is inside Test coverage issue: No bugs that crash at runtime. No security issues. No breaking API changes. No duplicate logic found in other language modules. Duplicate DetectionNo duplicates detected. The check is specific to Test CoverageNew test file covers the primary use case (Express.js Last updated: 2026-04-01T02:00:00Z |
| # These are defined inside Object.defineProperty or object literals | ||
| # and cannot be called directly - they're accessed via property names. | ||
| # Tests would fail trying to call obj.getterFuncName() instead of obj.propertyName | ||
| if node.type == "function_expression" and node.parent and node.parent.type == "pair": |
There was a problem hiding this comment.
We could create a list of such function types to avoid.
Problem
When functions are defined as property getters/setters (e.g., inside
Object.definePropertywithget: function getrouter() {...}), they were being discovered as optimizable functions. This caused test generation to fail with:Reproduction
In Express.js
application.js:Codeflash discovered
getrouteras an optimizable function and generated tests like:But it should be:
Solution
Added check in
_walk_tree_for_functions()to excludefunction_expressionnodes that are property getters/setters:pairnode (key-value in object literal)"get"or"set"Testing
test_property_getter_exclusion.pyAffected Trace IDs
From /workspace/logs analysis:
11b5475c-91d9-43b1-a317-33b7f4811c52(init.getrouter)78d8f1de-541a-4208-85c7-77194c70ec72(createETagGenerator.generateETag)Files Changed
codeflash/languages/javascript/treesitter.py: Added getter/setter exclusion logictests/test_property_getter_exclusion.py: Comprehensive test coverage🤖 Generated with Claude Code