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.
94 lines • 3.53 kB
JavaScript
;
/**
* Functions to apply Cognitive Complexity to files and folders.
*/
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.programOutput = exports.getFolderOutput = exports.getSourceOutput = exports.getFileOutput = exports.getFileOrFolderOutput = void 0;
const fs_1 = require("fs");
const path = __importStar(require("path"));
const ts = __importStar(require("typescript"));
const util_1 = require("../util/util");
const cognitive_complexity_1 = require("./cognitive-complexity");
// API
/**
* @param entry A file system entry path
*/
async function getFileOrFolderOutput(entryPath) {
const entry = await fs_1.promises.stat(entryPath);
if (entry.isDirectory()) {
return getFolderOutput(entryPath);
}
else {
return getFileOutput(entryPath);
}
}
exports.getFileOrFolderOutput = getFileOrFolderOutput;
// API
async function getFileOutput(filePath) {
const fileContent = (await fs_1.promises.readFile(filePath)).toString();
return getSourceOutput(fileContent, path.basename(filePath));
}
exports.getFileOutput = getFileOutput;
// API
function getSourceOutput(sourceCode, fileName = "") {
const parsedFile = ts.createSourceFile(fileName, sourceCode, ts.ScriptTarget.Latest, true);
return (0, cognitive_complexity_1.fileCost)(parsedFile);
}
exports.getSourceOutput = getSourceOutput;
// API
async function getFolderOutput(folderPath) {
const folderContents = await fs_1.promises.readdir(folderPath, { withFileTypes: true });
return (0, util_1.createObjectOfPromisedValues)(folderContents, entry => entry.name, (entry) => {
if (entry.isDirectory()) {
return getFolderOutput(folderPath + "/" + entry.name);
}
// correct extension
if (entry.name.match(/.*\.[tj]sx?$/) !== null) {
return getFileOutput(folderPath + "/" + entry.name);
}
return undefined;
});
}
exports.getFolderOutput = getFolderOutput;
// API
/**
* @param entryPath Relative to cwd
*/
async function programOutput(entryPath) {
const entryName = path.parse(entryPath).base;
const resultForAllFiles = {
[entryName]: await getFileOrFolderOutput(entryPath)
};
return JSON.stringify(resultForAllFiles, (key, value) => {
// don't show empty inner
if (key === "inner" && value.length === 0) {
return undefined;
}
return value;
});
}
exports.programOutput = programOutput;
//# sourceMappingURL=output.js.map