UNPKG

eslint-plugin-sonarjs

Version:
129 lines (128 loc) • 4.5 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ComplexityTracker = exports.ComplexityAccumulator = void 0; const ancestor_js_1 = require("../helpers/ancestor.js"); const location_js_1 = require("../helpers/location.js"); /** Read branch tokens and function parents through ESLint. */ class EslintComplexityAstAccess { constructor(context) { this.context = context; } getBranchToken(node) { return getComplexityToken(node, this.context); } getFunctionParent(node) { return (0, ancestor_js_1.getParent)(this.context, node); } } /** Store branch tokens for the current function, separate from its nested functions. */ class ComplexityAccumulator { constructor(ast) { this.ast = ast; this.functions = []; this.functionStack = []; this.functionsWithNested = new Set(); } enterFunction(node) { const info = { node, parent: this.ast.getFunctionParent(node), tokens: [] }; this.functions.push(info); const enclosing = this.functionStack.at(-1); if (enclosing) { this.functionsWithNested.add(enclosing.node); } this.functionStack.push(info); } leaveFunction() { this.functionStack.pop(); } addBranch(node) { const enclosing = this.functionStack.at(-1); if (enclosing) { const token = this.ast.getBranchToken(node); if (token) { enclosing.tokens.push(token); } } } hasNestedFunction(node) { return this.functionsWithNested.has(node); } getFunctions() { return this.functions; } } exports.ComplexityAccumulator = ComplexityAccumulator; /** Connect complexity counting to ESLint token locations and issue reporting. */ class ComplexityTracker { constructor(context) { this.context = context; this.accumulator = new ComplexityAccumulator(new EslintComplexityAstAccess(context)); } enterFunction(node) { this.accumulator.enterFunction(node); } leaveFunction() { this.accumulator.leaveFunction(); } addBranch(node) { this.accumulator.addBranch(node); } hasNestedFunction(node) { return this.accumulator.hasNestedFunction(node); } reportOverThreshold(threshold, isExcluded) { for (const { node, parent, tokens } of this.accumulator.getFunctions()) { if (!isExcluded(node)) { raiseOnUnauthorizedComplexity(node, parent, tokens, threshold, this.context); } } } } exports.ComplexityTracker = ComplexityTracker; function raiseOnUnauthorizedComplexity(node, parent, branchTokens, threshold, context) { const complexity = branchTokens.length + 1; if (complexity <= threshold) { return; } const primaryLocation = (0, location_js_1.getMainFunctionTokenLocation)(node, parent, context); const tokens = [{ loc: primaryLocation }, ...branchTokens]; context.report({ message: toEncodedMessage(complexity, threshold, tokens), loc: primaryLocation, }); } function toEncodedMessage(complexity, threshold, tokens) { const encodedMessage = { message: `Function has a complexity of ${complexity} which is greater than ${threshold} authorized.`, cost: complexity - threshold, secondaryLocations: tokens.map(node => toSecondaryLocation(node)), }; return JSON.stringify(encodedMessage); } function toSecondaryLocation(token) { return { line: token.loc.start.line, column: token.loc.start.column, endLine: token.loc.end.line, endColumn: token.loc.end.column, message: '+1', }; } function getComplexityToken(node, context) { const { sourceCode } = context; switch (node.type) { case 'ConditionalExpression': return sourceCode.getFirstTokenBetween(node.test, node.consequent, token => token.value === '?'); case 'SwitchCase': return node.test ? sourceCode.getFirstToken(node) : undefined; case 'IfStatement': case 'ForStatement': case 'ForInStatement': case 'ForOfStatement': case 'WhileStatement': case 'DoWhileStatement': return sourceCode.getFirstToken(node); case 'LogicalExpression': return sourceCode.getFirstTokenBetween(node.left, node.right, token => token.value === node.operator); } }