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.

116 lines 4.58 kB
import { Container } from "./Container.js"; import { File } from "./File.js"; import { Folder } from "./Folder.js"; import { FolderContents } from "./FolderContents.js"; import { isSortedContainerOutput, isSortedFileOutput } from "../../domain/sortedOutput.js"; import { element } from "../../framework.js"; export class Tree { constructor() { this.containerMap = new Map(); this.folderMap = new Map(); this.fileMap = new Map(); this.folderContentsMap = new Map(); this.dom = element("div"); } makeTree(complexity) { const contents = this.makeFolderContents(complexity); const onlyOneTopLevelNode = complexity.inner.length === 1; if (onlyOneTopLevelNode) { contents.setOpenness(true); } this.dom.innerHTML = ""; this.dom.appendChild(contents.dom); } makeContainer(containerOutput) { if (this.containerMap.has(containerOutput.id)) { return this.containerMap.get(containerOutput.id); } const container = new Container(containerOutput, containerOutput.inner.map(inner => this.makeContainer(inner))); this.containerMap.set(containerOutput.id, container); return container; } makeFile(fileOutput) { if (this.fileMap.has(fileOutput.id)) { return this.fileMap.get(fileOutput.id); } const children = []; for (const containerOutput of fileOutput.inner) { children.push(this.makeContainer(containerOutput)); } const file = new File(fileOutput, children); this.fileMap.set(fileOutput.id, file); return file; } makeFolderContents(folderOutput) { if (this.folderContentsMap.has(folderOutput.id)) { return this.folderContentsMap.get(folderOutput.id); } const folderContents = new FolderContents(folderOutput.inner.map((folderEntry) => { const folderEntryComponent = isSortedContainerOutput(folderEntry) ? this.makeContainer(folderEntry) : isSortedFileOutput(folderEntry) ? this.makeFile(folderEntry) : this.makeFolder(folderEntry); return folderEntryComponent; })); this.folderContentsMap.set(folderOutput.id, folderContents); return folderContents; } makeFolder(folderOutput) { if (this.folderMap.has(folderOutput.id)) { return this.folderMap.get(folderOutput.id); } const folder = new Folder(folderOutput, this.makeFolderContents(folderOutput)); this.folderMap.set(folderOutput.id, folder); return folder; } reChildFolderContents(complexity) { const folderContents = this.folderContentsMap.get(complexity.id); folderContents.setChildren(complexity.inner.map((folderEntry) => { const folderEntryComponent = isSortedContainerOutput(folderEntry) ? this.makeContainer(folderEntry) : isSortedFileOutput(folderEntry) ? this.makeFile(folderEntry) : this.makeFolder(folderEntry); return folderEntryComponent; })); } reChildFile(complexity) { const file = this.fileMap.get(complexity.id); file.setChildren(complexity.inner.map(containerOutput => this.makeContainer(containerOutput))); } reChildContainer(complexity) { const containers = this.containerMap.get(complexity.id); containers.setChildren(complexity.inner.map(containerOutput => this.makeContainer(containerOutput))); } reDepthContainer(complexity) { const container = this.containerMap.get(complexity.id); container.setDepth(complexity.depth); } reDepthFile(complexity) { const file = this.fileMap.get(complexity.id); file.setDepth(complexity.depth); } reDepthFolder(complexity) { const folder = this.folderMap.get(complexity.id); folder.setDepth(complexity.depth); } collapseAll() { this.setTreeOpenness(false); } expandAll() { this.setTreeOpenness(true); } setTreeOpenness(isOpen) { for (const component of this.containerMap.values()) { component.setOpenness(isOpen); } for (const component of this.folderMap.values()) { component.setOpenness(isOpen); } for (const component of this.fileMap.values()) { component.setOpenness(isOpen); } } } //# sourceMappingURL=Tree.js.map