feat: analyze generator functions in JS/TS#435
Open
github-actions[bot] wants to merge 1 commit into
Open
Conversation
Generator function declarations (function* foo() {}) and generator
function expressions (const g = function* () {}) were previously
invisible to the complexity analyser — the parser produced
generator_function_declaration and generator_function node types which
were not included in any of the collection or nesting checks.
This commit adds both types to:
- collectFunctions isFunctionNode check (top-level collection)
- isNestedFunction check (nesting penalty when inside another function)
- getFunctionName (name extraction via childForFieldName)
- getFunctionReason (human-readable label in complexity details)
async function* is already covered because tree-sitter emits
generator_function_declaration for it as well.
Generator class methods (*method() {}) were already handled as
method_definition and remain unchanged.
Six new unit tests cover:
- top-level generator function declaration (JS)
- async generator function declaration (JS)
- named generator function expression (JS)
- anonymous generator function expression (JS)
- generator expression nested inside a regular function (JS)
- TypeScript generator function with return-type annotation (TS)
All 165 unit tests pass.
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
5 tasks
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #435 +/- ##
==========================================
+ Coverage 78.59% 78.62% +0.03%
==========================================
Files 13 13
Lines 4205 4211 +6
Branches 469 474 +5
==========================================
+ Hits 3305 3311 +6
Misses 898 898
Partials 2 2 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
Contributor
There was a problem hiding this comment.
Pull request overview
This PR updates the JS/TS cognitive complexity analyzer to recognize generator functions (declarations and expressions) so they are collected, named, and (for expression forms) treated as nested functions for nesting-penalty purposes, and adds unit tests to prevent regressions.
Changes:
- Extend JS/TS function collection and naming logic to include
generator_function_declarationandgenerator_function. - Treat nested generator expressions as nested functions to apply nesting penalty and include their body complexity in the enclosing function.
- Add unit tests covering JS and TS generator forms (sync/async, named/anonymous, nested).
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| src/metricsAnalyzer/languages/jsLikeAnalyzer.ts | Adds generator node types to function detection, nested-function handling, naming, and nested-function reason text. |
| src/unit/unit.test.ts | Adds a new test suite validating generator function collection and complexity behavior across JS and TS. |
Comment on lines
+2993
to
+2996
| // nested generator adds +1 (for the nesting penalty) | ||
| // the if inside the generator adds +1 base + 1 nesting = 2 | ||
| // total outer = 1 (nested generator) + 2 (if inside) = 3 | ||
| assert.ok(results[0].complexity >= 1, "nested generator should contribute complexity to outer"); |
| if (node.type === "function_declaration" || node.type === "function_expression") { | ||
| if (node.type === "function_declaration" || node.type === "function_expression" || | ||
| node.type === "generator_function_declaration" || node.type === "generator_function") { | ||
| // Use childForFieldName for O(1) field lookup (tree-sitter exposes "name" for both node types) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
🤖 This is an automated pull request from Repo Assist.
Summary
Generator functions (
function* foo() {},async function* foo() {}, and the expression formconst g = function* () {}) were completely invisible to the JS/TS complexity analyser. Tree-sitter emitsgenerator_function_declarationandgenerator_functionnode types for these constructs, neither of which appeared in any collection, nesting, or naming check.Root Cause
collectFunctionsmatched onlyfunction_declaration | function_expression | method_definition | arrow_function. Generator node types were silently skipped — their bodies were never traversed, producing zero complexity scores.Changes to
jsLikeAnalyzer.tscollectFunctionsisFunctionNodegenerator_function_declarationandgenerator_functionisNestedFunctiongenerator_function(expression form nested inside another function)getFunctionNamechildForFieldName("name")branch to cover both generator typesgetFunctionReasongenerator_function→"generator function expression (nested)"Not affected: generator class methods (
*gen() {}) already producedmethod_definitionnodes and were handled correctly.async generators: tree-sitter emits
generator_function_declarationforasync function* foo() {}as well — covered by the same check.New Tests (6)
function*declaration (JS)async function*declaration (JS)Test Status
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 workflows to your repo
To install this agentic workflow, run