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.

253 lines 7.84 kB
"use strict"; /** * Purpose: get children of a node organised by their depth. */ 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 () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); Object.defineProperty(exports, "__esModule", { value: true }); exports.whereAreChildren = whereAreChildren; const ts = __importStar(require("typescript")); const node_util_1 = require("../util/node-util"); const node_inspection_1 = require("./node-inspection"); ; /** * @param node The node whose children to categorise by depth * @returns a tuple of [children and the same depth, children at one level below] */ function whereAreChildren(node) { if (ts.isArrowFunction(node)) { return arrowFunction(node); } else if (ts.isBinaryExpression(node)) { return binaryExpression(node); } else if (ts.isCatchClause(node)) { return catchClause(node); } else if (ts.isConditionalExpression(node)) { return conditionalExpression(node); } else if (ts.isConditionalTypeNode(node)) { return conditionalType(node); } else if (ts.isDoStatement(node)) { return doStatement(node); } else if ((0, node_inspection_1.isForLikeStatement)(node)) { return forLikeStatement(node); } else if (ts.isFunctionDeclaration(node)) { return functionDeclaration(node); } else if (ts.isFunctionExpression(node)) { return functionExpression(node); } else if (ts.isIfStatement(node)) { return ifStatement(node); } else if (ts.isMethodDeclaration(node)) { return methodDeclaration(node); } else if (ts.isSwitchStatement(node)) { return switchStatement(node); } else if (ts.isWhileStatement(node)) { return whileStatement(node); } else { return { sameDepth: node.getChildren(), below: [], }; } } function arrowFunction(node) { const children = node.getChildren(); // aggregate code inside SyntaxList const sameDepth = children.slice(1, -1); // aggregate code inside arrow function const below = [children[children.length - 1]]; return { sameDepth, below }; } function binaryExpression(node) { return { sameDepth: node.getChildren(), below: [], }; } // function binaryTypeExpression(node: ts.UnionTypeNode | ts.IntersectionTypeNode): DepthOfChildren { // return { // sameDepth: [node.types[0]], // right: node.types.slice(1), // below: [], // } // } function catchClause(node) { const children = node.getChildren(); const variableDeclaration = children.find(child => ts.isVariableDeclaration(child)); const block = children.find(child => ts.isBlock(child)); if (block === undefined) throw new node_util_1.UnreachableNodeState(node, "catch clause has no block"); return { sameDepth: variableDeclaration ? [variableDeclaration] : [], below: [block], }; } function conditionalExpression(node) { const children = node.getChildren(); const condition = children[0]; const thenCode = children[2]; const elseCode = children[4]; return { sameDepth: [condition], below: [thenCode, elseCode] }; } function conditionalType(node) { const children = node.getChildren(); const endOfCondition = children .findIndex(child => child.kind === ts.SyntaxKind.QuestionToken); const endOfThen = children .findIndex(child => child.kind === ts.SyntaxKind.ColonToken); const condition = children.slice(0, endOfCondition); // then code const below = children.slice(endOfCondition + 1, endOfThen); // else code below.push(...children.slice(endOfThen + 1)); return { sameDepth: condition, below, }; } function doStatement(node) { const sameDepth = []; const below = []; const children = node.getChildren(); // aggregate block below.push(children[1]); // aggregate condition sameDepth.push(children[4]); return { sameDepth, below }; } function forLikeStatement(node) { const sameDepth = []; const below = []; const children = node.getChildren(); // consume everything form the open parenthesis to the close parenthesis let i = 2; while (true) { const child = children[i++]; if (ts.isToken(child) && child.kind === ts.SyntaxKind.CloseParenToken) { break; } sameDepth.push(child); } // consume looped code below.push(children[i]); return { sameDepth, below }; } function functionDeclaration(node) { const children = node.getChildren(); const sameDepth = children.slice(1, -1); const below = [children[children.length - 1]]; return { sameDepth, below }; } function functionExpression(node) { const children = node.getChildren(); const functionBody = children.slice(-1); const functionDecl = children.slice(0, -1); return { sameDepth: functionBody, below: functionDecl }; } function ifStatement(node) { const children = node.getChildren(); const condition = children[2]; const thenCode = children[4]; const elseCode = children[6]; if (elseCode) { if (ts.isIfStatement(elseCode)) { // an else if structure is on the same depth return { sameDepth: [condition, elseCode], below: [thenCode] }; } else { // the contents of a solo else are at one depth below return { sameDepth: [condition], below: [thenCode, elseCode] }; } } return { sameDepth: [condition], below: [thenCode] }; } function methodDeclaration(node) { const sameDepth = []; const below = []; for (const child of node.getChildren()) { if (ts.isBlock(child)) { below.push(child); break; } sameDepth.push(child); } return { sameDepth, below }; } function switchStatement(node) { const children = node.getChildren(); // aggregate condition const condition = [children[2]]; // consume cases const cases = [children[4]]; return { sameDepth: condition, below: cases }; } function whileStatement(node) { const children = node.getChildren(); const condition = children[2]; const loopCode = children[4]; return { sameDepth: [condition], below: [loopCode] }; } //# sourceMappingURL=depth.js.map