npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

cognitive-complexity-ts

v0.6.5

Published

This program analyses TypeScript and JavaScript code according to the [Cognitive Complexity metric](https://www.sonarsource.com/docs/CognitiveComplexity.pdf). It produces a JSON summary and a GUI for exploring the complexity of your codebase.

Downloads

470

Readme

This program analyses TypeScript and JavaScript code according to the Cognitive Complexity metric. It produces a JSON summary and a GUI for exploring the complexity of your codebase.

Disclaimer

I and this project are completely unaffiliated with Sonar Source.

Install

npm install cognitive-complexity-ts

Run

# UI output (any of the following)
npx ccts [file path]
npx cognitive-complexity-ts [file path]
npx cognitive-complexity-ts-ui [file path]
npx ccts-ui [file path]

# JSON output (any of the following)
npx cognitive-complexity-ts-json [file path]
npx ccts-json [file path]

API

See src/api.ts or build/src/api.d.ts.

Functions available when this package is imported:

function getFileOrFolderOutput(entryPath: string): Promise<FileOutput | FolderOutput>;
function getFileOutput(filePath: string): Promise<FileOutput>;
function getFolderOutput(folderPath: string): Promise<FolderOutput>;
function programOutput(entryPath: string): Promise<string>;

Simple Overview of the Cognitive Complexity Metric

Each function, class, namespace, type, and file has a complexity score based on the total complexity of all code written inside. The complexity score is not increased by code that is referenced.

Inherent Cost

The following structures increase the complexity score by 1.

  • if, else, else if, ? : for types and data
  • switch
  • for, while, do while
  • catch
  • break label, continue label
  • a sequence of the same operator &&, ||, ??, intersection &, union |
  • a recursive reference to a function, class, or type
  • mapped type { [K in T]: ... }

Nesting Increments

The following structures increase the complexity score by a number equal to the depth the code is at. The depth of code is defined in the next section.

  • if, ? : for types and data
  • switch
  • for, while, do while
  • catch
  • mapped type { [K in T]: ... }

The following structures increment the depth by 1.

  • code called when the condition is met in if and else if
  • else
  • conditionally executed code in a conditional expression ? ... : ...
  • switch
  • looped code in for, while, do while
  • catch
  • nested function body, nested class body

Differences

I have taken some liberties with the guidelines set out by Sonar Source in the Cognitive Complexity whitepaper in order to account for TypeScript's features and to improve the user output.

Changes:

  • Classes have a score
  • Files have a score
  • Namespaces have a score
  • Types have a score
    • Union and intersection have an inherent increment
    • Mapped types have an inherent and nesting increments
    • Recursive types have an inherent increment
  • Recursive references have an increment, not just recursive calls
  • & and | are not supported by TypeScript as eager boolean operators, so they do not carry an inherent increment.

Output

Some anonymous classes and functions will appear with names because they are assigned to a variable/const.

Development

Install

git clone https://github.com/Deskbot/Cognitive-Complexity-TS
npm i

Build

# All builds are incremental.
# These scripts also build dependencies incrementally.
npm run build       # The main complexity analysis program and UI back-end server.
npm run build-tools
npm run build-ui    # The in-browser UI code only.

Run

# These also build dependencies.
# The binaries exposed by package.json do not.
npm run json
npm run ui

Test

npm run test [test name]

The test name can be any substring of a path to a test file. Or the test name can be left blank to run all tests.

See TypeScript Parser Output

Microsoft doesn't appear to have documentation for what all of the AST nodes are, so I created a tool to output the AST for the code.

npm run what [file path]