Skip to content

inherit cognitive nesting without penalizing parent functions (#165) - #167

Merged
itaymendel merged 1 commit into
mainfrom
fix/165-nested-function-cognitive-complexity
Oct 6, 2026
Merged

itaymendel merged 1 commit into
mainfrom
fix/165-nested-function-cognitive-complexity

Conversation

@itaymendel

@itaymendel itaymendel commented Oct 6, 2026 •

Copy link
Copy Markdown
Owner

Summary by CodeRabbit

  • Bug Fixes
    • Corrected cognitive complexity scoring for nested functions. Nested functions, callbacks, class field initializers, and static blocks are scored independently, without adding points to the enclosing function.
    • Nested code inherits an additional nesting level, which can affect the score of decisions inside it.
  • Documentation
    • Clarified how nested code units are scored and reported.

@coderabbitai

coderabbitai Bot commented Oct 6, 2026 •

Copy link
Copy Markdown

Review in Change Stack →

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration
  • Configuration used: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: a3b90ef0-04ce-4b11-a067-fbbcd3987e76
📥 Commits

Reviewing files that changed from the base of the PR and between 3331218 and cdadbd4.

📒 Files selected for processing (22)
  • CHANGELOG.md
  • README.md
  • src/cognitive/handlers.ts
  • src/cognitive/visitor.ts
  • src/combined-visitor.ts
  • src/rules/shared.ts
  • src/visitor.ts
  • tests/cognitive-compatibility.test.ts
  • tests/fixtures/js/class-field-callbacks.js
  • tests/fixtures/js/class-in-function.js
  • tests/fixtures/js/function-as-branch.js
  • tests/fixtures/js/nested-functions.js
  • tests/fixtures/jsx/list-rendering.jsx
  • tests/fixtures/ts/class-member-keys.ts
  • tests/fixtures/ts/class-static-methods.ts
  • tests/fixtures/ts/extraction-typed.ts
  • tests/fixtures/ts/promise-chains.ts
  • tests/fixtures/tsx/hooks-component.tsx
  • tests/fixtures/vue/complex-logic.vue
  • tests/fixtures/vue/composition-api.vue
  • tests/fixtures/vue/script-setup-ts.vue
  • tests/rule-reporting.test.ts
💤 Files with no reviewable changes (1)
  • src/rules/shared.ts

Included review availability: This review used your included allowance. Your plan provides up to 1 included review per hour; 0 remain after this review.


📝 Walkthrough

Walkthrough

Nested functions no longer add cognitive-complexity points to enclosing scopes. Nested functions, class field initializers, and static blocks are scored separately, with nesting inherited from the enclosing scope. Tests and fixture annotations cover the updated scores.

Changes

Cognitive complexity scoring

Layer / File(s) Summary
Scope traversal and scoring
src/visitor.ts, src/cognitive/visitor.ts, src/combined-visitor.ts, src/cognitive/handlers.ts, src/rules/shared.ts, README.md, CHANGELOG.md
Function scopes now reach the cognitive handler, which sets nesting from the parent scope without adding a nested-function point. Documentation and the changelog describe the scoring change.
Scoring regression coverage
tests/cognitive-compatibility.test.ts, tests/rule-reporting.test.ts, tests/fixtures/js/*, tests/fixtures/jsx/*, tests/fixtures/ts/*, tests/fixtures/tsx/*, tests/fixtures/vue/*
Tests and fixture annotations cover separately scored nested functions and nesting-adjusted complexity for functions, class members, and callbacks.

Priority: ➖ Normal

Estimated code review effort: 3 (Moderate) | ~20 minutes

Change: Bug fix

Merge Risk: ⚪ Minimal · up to cdadb

This change stops nested functions from adding complexity points to their parent functions, and nested units now inherit nesting from the enclosing scope. Scores for functions with nested callbacks will change, but that is the documented intent and the tests and fixtures were updated to match. No merge-blocking risk was identified.

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly summarizes the main change: nested functions inherit cognitive nesting without adding complexity points to parent functions.
Docstring Coverage ✅ Passed Docstring coverage is 80.95% which is sufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 21 functions across 16 files. (5 skipped: 5…
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
📝 Generate docstrings
  • Commit to this branch
  • Create a new PR
🧪 Generate unit tests (beta)
  • Commit to this branch
  • Create a new PR
  • Autopilot · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

I’m a rabbit counting branches in the glade,
Nested scopes now keep their points apart.
A field, a callback, each gets its own score,
I mark the paths, then hop across the floor.
The tests all bloom with numbers true,
And I tuck the changelog in my burrow too.

Comment @coderabbitai help to get the list of available commands.

@itaymendel
itaymendel merged commit 0bc4e62 into main Oct 6, 2026
2 checks passed
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