Repository: eslint/eslint
File: lib/rules/complexity.js (JavaScript)

Code before:

        return {

            onCodePathStart() {

                // The initial complexity is 1, representing one execution path in the CodePath
                complexities.push(1);
            },

            // Each branching in the code adds 1 to the complexity
            CatchClause: increaseComplexity,
            ConditionalExpression: increaseComplexity,
            LogicalExpression: increaseComplexity,
            ForStatement: increaseComplexity,
            ForInStatement: increaseComplexity,
            ForOfStatement: increaseComplexity,
            IfStatement: increaseComplexity,
            WhileStatement: increaseComplexity,
            DoWhileStatement: increaseComplexity,
            AssignmentPattern: increaseComplexity,

            // Avoid `default`
            "SwitchCase[test]": () => IS_MODIFIED_COMPLEXITY || increaseComplexity(),
            SwitchStatement: () => IS_MODIFIED_COMPLEXITY && increaseComplexity(),

            // Logical assignment operators have short-circuiting behavior
            AssignmentExpression(node) {
                if (astUtils.isLogicalAssignmentOperator(node.operator)) {
                    increaseComplexity();
                }
            },

            MemberExpression(node) {
                if (node.optional === true) {
                    increaseComplexity();
                }
            },

            CallExpression(node) {
                if (node.optional === true) {
                    increaseComplexity();
                }
            },

            onCodePathEnd(codePath, node) {
                const complexity = complexities.pop();

                /*
                 * This rule only evaluates complexity of functions, so "program" is excluded.
                 * Class field initializers and class static blocks are implicit functions. Therefore,
                 * they shouldn't contribute to the enclosing function's complexity, but their

Review comment (performance):
By default (classic cyclomatic complexity), empty switch statements and switch statements with only `default` clause count as 0, but with modified cyclomatic complexity they would count as 1?

Code after:
        // Public API
        //--------------------------------------------------------------------------

        return {

            onCodePathStart() {

                // The initial complexity is 1, representing one execution path in the CodePath
                complexities.push(1);
            },

            // Each branching in the code adds 1 to the complexity
            CatchClause: increaseComplexity,
            ConditionalExpression: increaseComplexity,
            LogicalExpression: increaseComplexity,
            ForStatement: increaseComplexity,
            ForInStatement: increaseComplexity,
            ForOfStatement: increaseComplexity,
            IfStatement: increaseComplexity,
            WhileStatement: increaseComplexity,
            DoWhileStatement: increaseComplexity,
            AssignmentPattern: increaseComplexity,

            // Avoid `default`
            "SwitchCase[test]": () => IS_MODIFIED_COMPLEXITY || increaseComplexity(),
            SwitchStatement: () => IS_MODIFIED_COMPLEXITY && increaseComplexity(),

            // Logical assignment operators have short-circuiting behavior
            AssignmentExpression(node) {
                if (astUtils.isLogicalAssignmentOperator(node.operator)) {
                    increaseComplexity();
                }
            },

            MemberExpression(node) {
                if (node.optional === true) {
                    increaseComplexity();
                }
            },

            CallExpression(node) {
                if (node.optional === true) {
                    increaseComplexity();
                }
            },

            onCodePathEnd(codePath, node) {
                const complexity = complexities.pop();

                /*
                 * This rule only evaluates complexity of functions, so "program" is excluded.