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.
136 lines • 4.16 kB
JavaScript
import { UniqueId } from "../framework.js";
import { isFileOutput } from "./output.js";
import { concatFilePath } from "./path.js";
;
export function isSortedFileOutput(output) {
return ("score" in output) && !("line" in output);
}
export function isSortedFolderOutput(output) {
return !("score" in output);
}
export function isSortedContainerOutput(output) {
return "line" in output;
}
export function cloneSortedOutput(output) {
return JSON.parse(JSON.stringify(output));
}
function compareOutputsByName(left, right) {
const leftName = left.name.toLowerCase();
const rightName = right.name.toLowerCase();
return leftName < rightName
? -1
: leftName > rightName
? 1
: 0;
}
function compareSortedOutputComplexity(left, right) {
const leftHasScore = !isSortedFolderOutput(left);
const rightHasScore = !isSortedFolderOutput(right);
if (leftHasScore && rightHasScore) {
const leftScore = left.score;
const rightScore = right.score;
return rightScore - leftScore;
}
if (!leftHasScore && !rightHasScore) {
return 0;
}
if (!leftHasScore) {
return 1;
}
if (!rightHasScore) {
return -1;
}
return 0;
}
function compareSortedOutputOrder(left, right) {
const leftIsContainer = isSortedContainerOutput(left);
const rightIsContainer = isSortedContainerOutput(right);
if (leftIsContainer && rightIsContainer) {
const leftContainer = left;
const rightContainer = right;
const lineDiff = leftContainer.line - rightContainer.line;
if (lineDiff !== 0)
return lineDiff;
return leftContainer.column - rightContainer.column;
}
return compareOutputsByName(left, right);
}
export function convertToSortedOutput(programOutput) {
return convertToSortedFolder("", "", 0, programOutput);
}
function convertToSortedContainer(path, depth, containerOutput) {
return {
id: UniqueId.next(),
kind: containerOutput.kind,
column: containerOutput.column,
line: containerOutput.line,
name: containerOutput.name,
path,
depth,
score: containerOutput.score,
inner: containerOutput.inner.map(container => convertToSortedContainer(path, depth + 1, container)),
};
}
function convertToSortedFile(path, name, depth, fileOutput) {
const inner = [];
const innerPath = concatFilePath(path, name);
for (const container of fileOutput.inner) {
inner.push(convertToSortedContainer(innerPath, depth + 1, container));
}
return {
id: UniqueId.next(),
kind: "file",
name,
path,
depth,
score: fileOutput.score,
inner,
};
}
function convertToSortedFolder(path, name, depth, folderOutput) {
const inner = [];
const innerPath = concatFilePath(path, name);
for (const name in folderOutput) {
const entry = folderOutput[name];
if (isFileOutput(entry)) {
inner.push(convertToSortedFile(innerPath, name, depth + 1, entry));
}
else {
inner.push(convertToSortedFolder(innerPath, name, depth + 1, entry));
}
}
return {
id: UniqueId.next(),
name,
path,
depth,
inner,
};
}
function sortFileOrContainer(file, sorter) {
file.inner.sort(sorter);
for (const container of file.inner) {
sortFileOrContainer(container);
}
}
function sortProgram(program, sorter) {
program.inner.sort(sorter);
for (const fileOrFolder of program.inner) {
if (isSortedFileOutput(fileOrFolder)) {
sortFileOrContainer(fileOrFolder, sorter);
}
else {
sortProgram(fileOrFolder, sorter);
}
}
}
export function sortProgramByComplexity(program) {
sortProgram(program, compareSortedOutputComplexity);
}
export function sortProgramByName(program) {
sortProgram(program, compareOutputsByName);
}
export function sortProgramInOrder(program) {
sortProgram(program, compareSortedOutputOrder);
}
//# sourceMappingURL=sortedOutput.js.map