Skip to content

docs(spec): pin the cognitive complexity algorithm per language - #27

Merged
ElbertePlinio merged 2 commits into
mainfrom
docs/7-cognitive-spec
Sep 28, 2026
Merged

ElbertePlinio merged 2 commits into
mainfrom
docs/7-cognitive-spec

Conversation

@ElbertePlinio

@ElbertePlinio ElbertePlinio commented Sep 27, 2026 •

Copy link
Copy Markdown
Member

PR 1 of #7 (parent #6). Spec only; #28 implements it with the metric and fixtures.

Adds a Cognitive complexity section to docs/spec.md. It follows SonarSource's white paper: structural constructs add 1 plus nesting, else if / else / labeled jumps / boolean sequences add a flat 1, and a switch or match costs 1 however many arms it has. There is one rule table per language, with node kinds and fields checked against the pinned grammars by dumping real parse trees. Svelte template rules are defined on their own, follow the per-block and snippet units from #25, and parse template expressions with the script grammar instead of scanning text.

Decisions worth a look:

cognitive replaces complexity as the default gate. complexity is still measured but its default limit is null, and any limit accepts null to turn that check off. A repo that wants cyclomatic back sets "limits": {"complexity": 15}. A backstop at 40 was considered and dropped, because on the corpus it catches nothing cognitive and bool_ops don't already fail (numbers in #28).

Recursion is not counted. Finding it needs call resolution that a single-file syntactic pass cannot do honestly.

Nested functions are scored separately and do not raise nesting in the parent, matching every other metric. Boolean sequences follow the parse tree, so a || b && c || d scores 2 where Sonar's token reading gives 3. All deviations from the paper are listed in the section.

An independent Fable review found ten issues in the first draft (Svelte else-block nesting, Rust let chains, Dart do condition and collection else if, unpinned Svelte expression parsing, and smaller wording gaps). All are fixed.

Rebased onto main after #25 merged; the Golden fixtures paragraph now keeps #25's Svelte requirements.

Validation: review only, no code changes.

Refs #7

Specify cognitive complexity as a per-function metric, with rule tables
for every supported language and independent Svelte template rules.
Cyclomatic complexity becomes a backstop whose default is calibrated
when the metric ships.

Refs #7
cognitive replaces complexity as the default gate; a null limit turns
any check off. Cognitive template rules follow the per-block and
snippet units from #8.
@ElbertePlinio
ElbertePlinio merged commit da2c32b into main Sep 28, 2026
8 checks passed
@ElbertePlinio
ElbertePlinio deleted the docs/7-cognitive-spec branch September 28, 2026 21:18
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