feat: name anonymous function expressions from assignment and object context - #515
Conversation
…context
When a function_expression or generator_function has no internal name,
infer the name from the parent AST context:
- { getData: function() {} } → 'getData' (pair parent, identifier key)
- exports.bar = function() {} → 'bar' (assignment, member_expression)
- handler = function() {} → 'handler' (assignment, identifier)
This mirrors the existing arrow_function behaviour (which already resolves
names via variable_declarator and pair parent nodes) and makes CodeLens
output consistent regardless of whether a developer writes a function
expression or an arrow function in object literals and module patterns.
Named function expressions (e.g. { getData: function inner() {} }) and
IIFE patterns (e.g. (function() {})()) are unaffected.
Adds 4 new tests covering all new cases; 207 tests pass, lint clean.
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
|
@copilot resolve the merge conflicts in this pull request |
…non-fn-assignment-naming-20260807-1c46293e37c60b64 # Conflicts: # src/metricsAnalyzer/languages/jsLikeAnalyzer.ts Co-authored-by: askpt <2493377+askpt@users.noreply.github.com>
Merge conflicts resolved in commit |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #515 +/- ##
==========================================
+ Coverage 81.68% 81.76% +0.07%
==========================================
Files 13 13
Lines 4367 4386 +19
Branches 445 449 +4
==========================================
+ Hits 3567 3586 +19
Misses 799 799
Partials 1 1 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
…g-20260807-1c46293e37c60b64
There was a problem hiding this comment.
Pull request overview
Adds contextual naming for anonymous JavaScript/TypeScript function and generator expressions.
Changes:
- Infers names from object properties and assignment targets.
- Adds unit tests for contextual naming scenarios.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
src/metricsAnalyzer/languages/jsLikeAnalyzer.ts |
Adds assignment-based name inference. |
src/unit/unit.test.ts |
Adds naming behavior tests. |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
Co-authored-by: askpt <2493377+askpt@users.noreply.github.com>
🤖 This PR was created by Repo Assist, an automated AI assistant.
Summary
Task 5 — Coding Improvement: When a
function_expressionorgenerator_functionhas no internal name, the analyzer now infers a human-readable name from the parent AST context instead of always falling back to(anonymous).New name inference rules
{ getData: function() {} }(anonymous)getData{ getStream: function*() {} }(anonymous)getStreamexports.bar = function() {}(anonymous)barobj.method = function() {}(anonymous)methodhandler = function() {}(anonymous)handler(function() {})()(IIFE)(anonymous)(anonymous)✅ unchanged{ getData: function inner() {} }innerinner✅ unchangedThis mirrors the existing behaviour for
arrow_function, which already resolves names viavariable_declaratorandpairparent nodes. The change makes CodeLens output consistent regardless of whether a developer uses arrow syntax or thefunctionkeyword in object literals and CommonJS module patterns.Implementation
In
jsLikeAnalyzer.ts, afterchildForFieldName("name")returnsnullfor anonymousfunction_expression/generator_function, the code now checks:pairparent ({ key: function() {} }): use the key if it is aproperty_identifieroridentifierassignment_expressionparent (x = function() {}): use the property name formember_expressionleft-hand side, or the identifier name for a simpleidentifierTest Status
Adds 4 new tests covering all new naming cases. Coverage: jsLikeAnalyzer.ts 100% statements, 97.41% branches (was 98.14%).
Warning
Firewall blocked 1 domain
The following domain was blocked by the firewall during workflow execution:
releaseassets.githubusercontent.comSee Network Configuration for more information.
Add this agentic workflow to your repo
To install this agentic workflow, run