UNPKG

cognitive-complexity-ts

Version:

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.

132 lines 4.69 kB
import { cloneSortedOutput, convertToSortedOutput, isSortedContainerOutput, isSortedFileOutput, isSortedFolderOutput, sortProgramByComplexity, sortProgramByName, sortProgramInOrder } from "../domain/sortedOutput.js"; import { removeAll } from "../util.js"; export var Include; (function (Include) { Include[Include["folders"] = 1] = "folders"; Include[Include["files"] = 2] = "files"; Include[Include["namespaces"] = 3] = "namespaces"; Include[Include["classes"] = 4] = "classes"; Include[Include["functionsAndTypes"] = 5] = "functionsAndTypes"; })(Include || (Include = {})); export var Sort; (function (Sort) { Sort[Sort["inOrder"] = 1] = "inOrder"; Sort[Sort["complexity"] = 2] = "complexity"; })(Sort || (Sort = {})); const nodeKindIncludeMap = { class: Include.classes, file: Include.files, function: Include.functionsAndTypes, module: Include.namespaces, type: Include.functionsAndTypes, }; export function convertNodeKindToInclude(kind) { return nodeKindIncludeMap[kind]; } export class ComplexityController { constructor(progComp, model, view) { this.include = { [Include.folders]: true, [Include.files]: true, [Include.namespaces]: true, [Include.classes]: true, [Include.functionsAndTypes]: true, }; this.sortMethod = Sort.inOrder; this.model = model; this.view = view; this.complexity = convertToSortedOutput(progComp); this.initialComplexity = cloneSortedOutput(this.complexity); this.sort(); this.model.overwriteComplexity(this.complexity); this.view.makeTree(this.complexity); } collapseAll() { this.view.collapseAll(); } expandAll() { this.view.expandAll(); } sort() { if (this.sortMethod === Sort.inOrder) { const noFilesOrFolders = !this.include[Include.folders] && !this.include[Include.files]; if (noFilesOrFolders) { sortProgramByName(this.complexity); } else { sortProgramInOrder(this.complexity); } } else if (this.sortMethod === Sort.complexity) { sortProgramByComplexity(this.complexity); } } setSortBy(method) { this.sortMethod = method; this.sort(); this.model.overwriteComplexity(this.complexity); } filter() { this.complexity = cloneSortedOutput(this.initialComplexity); const removeWhat = (data) => { if (!this.include[Include.folders]) { if (isSortedFolderOutput(data)) { return true; } } if (!this.include[Include.files]) { if (isSortedFileOutput(data)) { return true; } } if (!isSortedContainerOutput(data)) { return false; } if (!this.include[Include.namespaces]) { if (data.kind === "module") { return true; } } if (!this.include[Include.classes]) { if (data.kind === "class") { return true; } } return false; }; this.moveComplexityNodes(this.complexity.inner, removeWhat); this.reDepth(); this.sort(); this.model.overwriteComplexity(this.complexity); } moveComplexityNodes(inner, removeWhat) { for (const child of inner) { this.moveComplexityNodes(child.inner, removeWhat); } const removed = removeAll(inner, removeWhat); if (removed.length > 0) { inner.push(...removed.flatMap(removedElem => removedElem.inner)); for (const removedElem of removed) { removedElem.inner.splice(0); if (isSortedFolderOutput(removedElem)) { this.model.updateFolder(removedElem); } else if (isSortedFileOutput(removedElem)) { this.model.updateFile(removedElem); } else if (isSortedContainerOutput(removedElem)) { this.model.updateContainer(removedElem); } } } } reDepth(complexity = this.complexity, depth = 0) { complexity.depth = depth; complexity.inner.forEach(child => this.reDepth(child, depth + 1)); } setInclude(include, value) { this.include[include] = value; this.filter(); } } //# sourceMappingURL=ComplexityController.js.map