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).

143 lines 5.83 kB
import { Container } from "../component/tree/Container.js"; import { File } from "../component/tree/File.js"; import { Folder } from "../component/tree/Folder.js"; import { FolderContents } from "../component/tree/FolderContents.js"; import { cloneSortedOutput, convertToSortedOutput, isSortedContainerOutput, isSortedFileOutput, sortProgramByComplexity, sortProgramInOrder } from "../domain/sortedOutput.js"; import { element } from "../framework.js"; import { removeAll } from "../util/util.js"; export var Include; (function (Include) { Include[Include["folders"] = 1] = "folders"; Include[Include["files"] = 2] = "files"; Include[Include["containers"] = 3] = "containers"; })(Include || (Include = {})); var Sort; (function (Sort) { Sort[Sort["inOrder"] = 1] = "inOrder"; Sort[Sort["complexity"] = 2] = "complexity"; })(Sort || (Sort = {})); export class DataController { constructor(progComp, treeController) { this.containerMap = new Map(); this.folderMap = new Map(); this.fileMap = new Map(); this.folderContentsMap = new Map(); this.include = Include.folders; this.sortMethod = Sort.inOrder; this.dom = element("div"); this.complexity = convertToSortedOutput(progComp); this.initialComplexity = cloneSortedOutput(this.complexity); sortProgramInOrder(this.complexity); this.treeController = treeController; this.makeTree(); } makeTree() { const contents = this.makeFolderContents(this.complexity); const onlyOneTopLevelNode = this.complexity.inner.length <= 1; if (onlyOneTopLevelNode) { contents.setOpenness(true); } this.dom.innerHTML = ""; this.dom.appendChild(contents.dom); } makeContainer(containerOutput) { if (this.containerMap.has(containerOutput)) { return this.containerMap.get(containerOutput); } const container = new Container(containerOutput, containerOutput.path, containerOutput.inner.map(inner => this.makeContainer(inner))); this.treeController.register(container); this.containerMap.set(containerOutput, container); return container; } makeFile(fileOutput) { if (this.fileMap.has(fileOutput)) { return this.fileMap.get(fileOutput); } const children = []; for (const containerOutput of fileOutput.inner) { children.push(this.makeContainer(containerOutput)); } const file = new File(fileOutput.path, fileOutput.name, fileOutput.score, children); this.treeController.register(file); this.fileMap.set(fileOutput, file); return file; } makeFolderContents(folderOutput) { if (this.folderContentsMap.has(folderOutput)) { return this.folderContentsMap.get(folderOutput); } const folderContents = new FolderContents(folderOutput.inner.map((folderEntry) => { const folderEntryComponent = isSortedFileOutput(folderEntry) ? this.makeFile(folderEntry) : this.makeFolder(folderEntry); return folderEntryComponent; })); this.folderContentsMap.set(folderOutput, folderContents); return folderContents; } makeFolder(folderOutput) { if (this.folderMap.has(folderOutput)) { return this.folderMap.get(folderOutput); } const folder = new Folder(folderOutput.path, folderOutput.name, this.makeFolderContents(folderOutput)); this.treeController.register(folder); this.folderMap.set(folderOutput, folder); return folder; } reChild() { for (const [complexity, folderContents] of this.folderContentsMap) { folderContents.setChildren(complexity.inner.map((folderEntry) => { const folderEntryComponent = isSortedFileOutput(folderEntry) ? this.makeFile(folderEntry) : this.makeFolder(folderEntry); return folderEntryComponent; })); } for (const [complexity, file] of this.fileMap) { file.setChildren(complexity.inner.map(containerOutput => this.makeContainer(containerOutput))); } for (const [complexity, container] of this.containerMap) { container.setChildren(complexity.inner.map(containerOutput => this.makeContainer(containerOutput))); } } sort() { if (this.sortMethod === Sort.inOrder) { sortProgramInOrder(this.complexity); } else if (this.sortMethod === Sort.complexity) { sortProgramByComplexity(this.complexity); } this.reChild(); } sortByComplexity() { this.sortMethod = Sort.complexity; this.sort(); } sortInOrder() { this.sortMethod = Sort.inOrder; this.sort(); } filter() { this.complexity = cloneSortedOutput(this.initialComplexity); this.sort(); const removeWhat = this.include === Include.folders ? () => false : this.include === Include.files ? data => !isSortedContainerOutput(data) && !isSortedFileOutput(data) : data => !isSortedContainerOutput(data); this.removeComplexityNodes(this.complexity.inner, removeWhat); this.makeTree(); } removeComplexityNodes(inner, removeWhat) { const removed = removeAll(inner, removeWhat); if (removed.length > 0) { inner.push(...removed.flatMap(removedElem => removedElem.inner)); this.removeComplexityNodes(inner, removeWhat); } } setInclude(include) { this.include = include; this.filter(); } } //# sourceMappingURL=DataController.js.map