Repository navigation
docs(spec): pin the cognitive complexity algorithm per language - #27
Merged
Merged
Conversation
This was referenced Sep 27, 2026
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
force-pushed
the
docs/7-cognitive-spec
branch
from
September 28, 2026 12:40
60a09db to
92e861c
Compare
14 tasks done
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.
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:
cognitivereplacescomplexityas the default gate.complexityis still measured but its default limit isnull, and any limit acceptsnullto 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 nothingcognitiveandbool_opsdon'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 || dscores 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
docondition and collectionelse if, unpinned Svelte expression parsing, and smaller wording gaps). All are fixed.Rebased onto
mainafter #25 merged; the Golden fixtures paragraph now keeps #25's Svelte requirements.Validation: review only, no code changes.
Refs #7