{"id":15062867,"url":"https://github.com/emacs-vs/cognitive-complexity","last_synced_at":"2025-04-10T10:11:40.061Z","repository":{"id":248945279,"uuid":"830203213","full_name":"emacs-vs/cognitive-complexity","owner":"emacs-vs","description":"Show cognitive complexity of code in Emacs 29+ (treesit-based)","archived":false,"fork":false,"pushed_at":"2025-02-26T10:10:52.000Z","size":275,"stargazers_count":44,"open_issues_count":2,"forks_count":2,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-03-24T09:03:38.140Z","etag":null,"topics":["cognitive-complexity","editor","emacs","ide","programming"],"latest_commit_sha":null,"homepage":"","language":"Emacs Lisp","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":"emacs-vs/codemetrics","license":"gpl-3.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/emacs-vs.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null},"funding":{"github":["emacs-vs","jcs090218"],"patreon":null,"open_collective":null,"ko_fi":null,"tidelift":null,"community_bridge":null,"liberapay":null,"issuehunt":null,"otechie":null,"custom":null}},"created_at":"2024-07-17T20:01:08.000Z","updated_at":"2025-02-26T10:10:55.000Z","dependencies_parsed_at":"2025-01-01T10:19:15.120Z","dependency_job_id":"c7d19a24-43dc-496c-aa5c-ce64bd08f9cb","html_url":"https://github.com/emacs-vs/cognitive-complexity","commit_stats":null,"previous_names":["abougouffa/codemetrics","emacs-vs/cognitive-complexity"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/emacs-vs%2Fcognitive-complexity","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/emacs-vs%2Fcognitive-complexity/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/emacs-vs%2Fcognitive-complexity/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/emacs-vs%2Fcognitive-complexity/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/emacs-vs","download_url":"https://codeload.github.com/emacs-vs/cognitive-complexity/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248198888,"owners_count":21063628,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["cognitive-complexity","editor","emacs","ide","programming"],"created_at":"2024-09-24T23:47:43.838Z","updated_at":"2025-04-10T10:11:40.049Z","avatar_url":"https://github.com/emacs-vs.png","language":"Emacs Lisp","readme":"[![License: GPL v3](https://img.shields.io/badge/License-GPL%20v3-blue.svg)](https://www.gnu.org/licenses/gpl-3.0)\n[![JCS-ELPA](https://raw.githubusercontent.com/jcs-emacs/badges/master/elpa/v/cognitive-complexity.svg)](https://jcs-emacs.github.io/jcs-elpa/#/cognitive-complexity)\n\n# Cognitive Complexity for Emacs\nShow the cognitive complexity of the code\n\n[![CI](https://github.com/emacs-vs/cognitive-complexity/actions/workflows/test.yml/badge.svg)](https://github.com/emacs-vs/cognitive-complexity/actions/workflows/test.yml)\n\n\u003cimg src=\"./etc/demo.gif\" /\u003e\n\nThis package is a direct `treesit` port of the `tree-sitter` based\n[codemetrics](https://github.com/emacs-vs/codemetrics). It implements live\ncalculation of the **Cognitive Complexity** metric, which was proposed by G. Ann\nCampbell in [Cognitive Complexity - A new way of measuring\nunderstandability](https://www.sonarsource.com/docs/CognitiveComplexity.pdf) (c)\nSonarSource S.A. 2016-2021, Switzerland.\n\n\u003e **Abstract:** Cyclomatic Complexity was initially formulated as a measurement\n\u003e of the \"testability and maintainability\" of the control flow of a module.\n\u003e While it excels at measuring the former, its underlying mathematical model is\n\u003e unsatisfactory at producing a value that measures the latter. This white paper\n\u003e describes a new metric that breaks from the use of mathematical models to\n\u003e evaluate code in order to remedy Cyclomatic Complexity’s shortcomings and\n\u003e produce a measurement that more accurately reflects the relative difficulty of\n\u003e understanding, and therefore of maintaining methods, classes, and\n\u003e applications.\n\nPlease note that this documentation is not up-to-date (it is basically the same\nas `codemetrics`'), I will try to enrich it with `cognitive-complexity` specifc\ninformation later!\n\n## Installation\n\n### Using `straight.el` and `use-package`:\n\n```elisp\n(use-package cognitive-complexity\n  :straight (:host github :repo \"emacs-vs/cognitive-complexity\"))\n```\n\n### Manually\n\n```sh\ngit clone https://github.com/emacs-vs/cognitive-complexity /path/to/lib\n```\n\nthen in Emacs:\n\n```elisp\n(add-to-list 'load-path \"/path/to/lib\")\n(require 'cognitive-complexity)\n```\n\nor\n\n```elisp\n(use-package cognitive-complexity\n  :load-path \"/path/to/lib\")\n```\n\n## Usage\n\nThe simplest way to start using this package:\n\n```elisp\n(cognitive-complexity-mode 1)\n```\n\n### Use it as a library\n\nThese are functions you can use to analyze:\n\n| Functions                      | Description                        |\n|--------------------------------|------------------------------------|\n| `cognitive-complexity-analyze` | Analyze a string with `major-mode` |\n| `cognitive-complexity-region`  | Analyze region                     |\n| `cognitive-complexity-buffer`  | Analyze the whole buffer           |\n\nAll these functions return the score data indicating the complexity.\n\n## Supported languages\nThese languages are fairly complete:\n\n- Bash\n- C / C++ / C#\n- Elisp\n- Go\n- Java / JavaScript / JSX / Julia\n- Kotlin\n- Lua\n- PHP / Python\n- Ruby / Rust\n- Swift\n- TypeScript / TSX\n\nThese languages are in development:\n\n- Agda\n- Elm\n- Elixir\n- OCaml\n- Scala (upstream, kinda buggy)\n\n## Customization\n\nAlthough `cognitive-complexity` aims to have good analysis rules out of the box\nfor all supported definitions, people will indubitably have their own\npreferences or desired functionality. The following section outlines how to add\nyour own analysis definitions and analysis functions to make\ncognitive-complexity work for you. If there are any improvements you find for\nexisting or new languages, please do raise a PR so that others may benefit from\nbetter analysis in the future!\n\n### Analysis on new nodes\n\nCode-Metrics defines all its analysis definitions in the variable\n`cognitive-complexity-rules` which is an alist with the key of the alist being the\nmode and the value being another alist of analysis definitions.\n\n```elisp\n;; Example of cognitive-complexity-rules' structure\n'((c-mode      . c-analysis-definitions)  ; \u003clanguage\u003e-analysis-definitions is structured as shown below\n  (csharp-mode . csharp-analysis-definitions)\n  (go-mode     . go-analysis-definitions)\n  (scala-mode  . scala-analysis-definitions)\n  ...)\n\n;; Examle of a analysis definition alist\n(setq csharp-analysis-definitions\n    (if_statement   . (1 t))\n    (\"\u0026\u0026\"           . cognitive-complexity-rules--logical-operators))\n```\n\nSo you can select whatever node that you want to analyze on it.\n\nTo find what node you'll want to analyze, refer to the\n[tree-sitter documentation](https://emacs-tree-sitter.github.io/getting-started/#view-the-syntax-tree)\nabout viewing nodes. `tree-sitter-debug` and `tree-sitter-query-builder`\nare both very useful for this.\n\n#### Example\n\nLet's look at a quick example of adding a new analysis definition. Let's say you\nwant to add analysis to `go-mode`'s `if_statement`. The analysis definition that\nis needed will be `'(\"if_statement\" . (1 t))`. To add this to the\n`cognitive-complexity-rules`, you can do something like the following.\n\n```emacs-lisp\n(push '(\"if_statement\" . (1 t)) (alist-get 'go-mode cognitive-complexity-rules))\n```\n\nNow the new analysis definition should be usable by cognitive-complexity!\n\n### Writing new analysis functions\n\nFor more complex analysis, you can write your own analysis rules!\n\n- `node` - (optional) the targeted tree-sitter node, in this example,\n`if_statement` will be the targeting node.\n- `depth` - (optional) current depth of from the root tree.\n- `nested` - (optional) current nested level apply from current complexity\nalgorithm.\n\nThen the function needs to return an integer represent the score and a\nboolean represent increment of the nested level in the form\n`(score-to-add . nested?)`. This can be useful if you want to add extra\nconditional logic onto your analysis.\n\nAs an example of an analysis function, take a look at the definition of the\nbasic `cognitive-complexity-rules--class-declaration`.\n\n```elisp\n(defun cognitive-complexity-rules--class-declaration (_node depth _nested)\n  \"...\"\n  (cognitive-complexity-with-metrics\n    (if (\u003c 1 depth)  ; if class inside class,\n        '(1 nil)     ; we score 1, but don't increase nested level\n      '(0 nil))\n    '(1 nil)))\n```\n\n## References\n\n- [`codemetrics`](https://github.com/emacs-vs/codemetrics)\n- [Code Metrics - Visual Studio Code Extension](https://github.com/kisstkondoros/codemetrics)\n- [CognitiveComplexity for Rider and ReSharper](https://github.com/matkoch/resharper-cognitivecomplexity)\n- [`gocognit`](https://github.com/uudashr/gocognit)\n\n## Development\n\nTo run the test locally, you will need the following tools:\n\n- [Eask](https://emacs-eask.github.io/)\n- [Make](https://www.gnu.org/software/make/) (optional)\n\nInstall all dependencies and development dependencies:\n\n```sh\neask install-deps --dev\n```\n\nTo test the package's installation:\n\n```sh\neask package\neask install\n```\n\nTo test compilation:\n\n```sh\neask compile\n```\n\n**🪧 The following steps are optional, but we recommend you follow these lint results!**\n\nThe built-in `checkdoc` linter:\n\n```sh\neask lint checkdoc\n```\n\nThe standard `package` linter:\n\n```sh\neask lint package\n```\n\n*📝 P.S. For more information, find the Eask manual at https://emacs-eask.github.io/.*\n\n## How to add an analysis rules?\n\nWhen adding a new analysis rules, add the analysis definition function to\n`cognitive-complexity.el` itself near where the other rules functions live and\nthen add the parser to `cognitive-complexity-rules.el` file. Finally, if you are\nadding support for a new language, remember to add it to the\n`cognitive-complexity-rules` variable.\n\nWhen creating a new parser, name it `cognitive-complexity-rules-\u003clanguage\u003e`.\n\nWhen creating a new analysis function, name it\n`cognitive-complexity-rules-\u003clanguage\u003e-\u003cfeature\u003e` or something similar.\n\n## License\n\nThis program is free software; you can redistribute it and/or modify\nit under the terms of the GNU General Public License as published by\nthe Free Software Foundation, either version 3 of the License, or\n(at your option) any later version.\n\nThis program is distributed in the hope that it will be useful,\nbut WITHOUT ANY WARRANTY; without even the implied warranty of\nMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\nGNU General Public License for more details.\n\nYou should have received a copy of the GNU General Public License\nalong with this program.  If not, see \u003chttps://www.gnu.org/licenses/\u003e.\n\nSee [`LICENSE`](/LICENSE) for details.\n","funding_links":["https://github.com/sponsors/emacs-vs","https://github.com/sponsors/jcs090218"],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Femacs-vs%2Fcognitive-complexity","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Femacs-vs%2Fcognitive-complexity","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Femacs-vs%2Fcognitive-complexity/lists"}