Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 26 additions & 20 deletions src/metricsAnalyzer/languages/csharpAnalyzer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,30 @@ export class CSharpMetricsAnalyzer {
"enum_declaration",
]);

/** Node types that represent function/method declarations β€” used for O(1) type checks during traversal. */
private static readonly FUNCTION_DECLARATION_TYPES: ReadonlySet<string> = new Set([
"method_declaration",
"constructor_declaration",
"destructor_declaration",
"operator_declaration",
"conversion_operator_declaration",
"accessor_declaration",
"local_function_statement",
]);

/** Node types that increase the nesting level for cognitive complexity analysis. */
private static readonly NESTING_TYPES: ReadonlySet<string> = new Set([
"if_statement",
"while_statement",
"for_statement",
"foreach_statement",
"switch_statement",
"try_statement",
"catch_clause",
"lambda_expression",
"anonymous_method_expression",
]);

/** Current nesting level during analysis */
private nesting = 0;
/** Current complexity score during analysis */
Expand Down Expand Up @@ -168,15 +192,7 @@ export class CSharpMetricsAnalyzer {
* @returns True if the node represents a function declaration
*/
private isFunctionDeclaration(node: Parser.SyntaxNode): boolean {
return (
node.type === "method_declaration" ||
node.type === "constructor_declaration" ||
node.type === "destructor_declaration" ||
node.type === "operator_declaration" ||
node.type === "conversion_operator_declaration" ||
node.type === "accessor_declaration" ||
node.type === "local_function_statement"
);
return CSharpMetricsAnalyzer.FUNCTION_DECLARATION_TYPES.has(node.type);
}

/**
Expand Down Expand Up @@ -829,17 +845,7 @@ export class CSharpMetricsAnalyzer {
* @returns True if the node increases nesting level
*/
private increasesNesting(node: Parser.SyntaxNode): boolean {
return (
node.type === "if_statement" ||
node.type === "while_statement" ||
node.type === "for_statement" ||
node.type === "foreach_statement" ||
node.type === "switch_statement" ||
node.type === "try_statement" ||
node.type === "catch_clause" ||
node.type === "lambda_expression" ||
node.type === "anonymous_method_expression"
);
return CSharpMetricsAnalyzer.NESTING_TYPES.has(node.type);
}

/**
Expand Down
19 changes: 11 additions & 8 deletions src/metricsAnalyzer/languages/goAnalyzer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,16 @@ interface GoFunctionMetrics {
* ```
*/
export class GoMetricsAnalyzer {
/** Node types that increase the nesting level for cognitive complexity analysis. */
private static readonly NESTING_TYPES: ReadonlySet<string> = new Set([
"if_statement",
"for_statement",
"expression_switch_statement",
"type_switch_statement",
"select_statement",
"func_literal",
]);

/** Current nesting level during analysis */
private nesting = 0;
/** Current complexity score during analysis */
Expand Down Expand Up @@ -496,14 +506,7 @@ export class GoMetricsAnalyzer {
* @returns True if the node increases nesting level
*/
private increasesNesting(node: Parser.SyntaxNode): boolean {
return (
node.type === "if_statement" ||
node.type === "for_statement" ||
node.type === "expression_switch_statement" ||
node.type === "type_switch_statement" ||
node.type === "select_statement" ||
node.type === "func_literal"
);
return GoMetricsAnalyzer.NESTING_TYPES.has(node.type);
}

/**
Expand Down
34 changes: 20 additions & 14 deletions src/metricsAnalyzer/languages/javaAnalyzer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,24 @@ interface JavaFunctionMetrics {
export class JavaMetricsAnalyzer {
private static readonly ELSE_BRANCH_INDEX = 2;

/** Node types that represent method or constructor declarations. */
private static readonly METHOD_DECLARATION_TYPES: ReadonlySet<string> = new Set([
"method_declaration",
"constructor_declaration",
]);

/** Node types that increase the nesting level for cognitive complexity analysis. */
private static readonly NESTING_TYPES: ReadonlySet<string> = new Set([
"if_statement",
"while_statement",
"for_statement",
"enhanced_for_statement",
"do_statement",
"catch_clause",
"switch_expression",
"lambda_expression",
]);

/** Current nesting level during analysis */
private nesting = 0;
/** Current complexity score during analysis */
Expand Down Expand Up @@ -124,10 +142,7 @@ export class JavaMetricsAnalyzer {
* Determines if a syntax node represents a method or constructor declaration.
*/
private isMethodDeclaration(node: Parser.SyntaxNode): boolean {
return (
node.type === "method_declaration" ||
node.type === "constructor_declaration"
);
return JavaMetricsAnalyzer.METHOD_DECLARATION_TYPES.has(node.type);
}

/**
Expand Down Expand Up @@ -409,16 +424,7 @@ export class JavaMetricsAnalyzer {
* @returns True if the node increases nesting level
*/
private increasesNesting(node: Parser.SyntaxNode): boolean {
return (
node.type === "if_statement" ||
node.type === "while_statement" ||
node.type === "for_statement" ||
node.type === "enhanced_for_statement" ||
node.type === "do_statement" ||
node.type === "catch_clause" ||
node.type === "switch_expression" ||
node.type === "lambda_expression"
);
return JavaMetricsAnalyzer.NESTING_TYPES.has(node.type);
}

/**
Expand Down
54 changes: 32 additions & 22 deletions src/metricsAnalyzer/languages/jsLikeAnalyzer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,35 @@ export interface JsLikeFunctionMetrics {
* a pre-initialised `Parser` instance to the constructor.
*/
export class JsLikeMetricsAnalyzer {
/** Node types that represent top-level function-like nodes eligible for independent analysis. */
private static readonly FUNCTION_NODE_TYPES: ReadonlySet<string> = new Set([
"function_declaration",
"function_expression",
"generator_function_declaration",
"generator_function",
"method_definition",
"arrow_function",
]);

/** Node types that represent nested function expressions subject to a nesting-level increment. */
private static readonly NESTED_FUNCTION_TYPES: ReadonlySet<string> = new Set([
"function_expression",
"generator_function",
"arrow_function",
"method_definition",
]);

/** Node types that increase the nesting level for cognitive complexity analysis. */
private static readonly NESTING_TYPES: ReadonlySet<string> = new Set([
"if_statement",
"for_statement",
"for_in_statement",
"while_statement",
"do_statement",
"switch_statement",
"catch_clause",
]);

/** Current nesting level during analysis */
private nesting = 0;
/** Current complexity score during analysis */
Expand Down Expand Up @@ -112,13 +141,7 @@ export class JsLikeMetricsAnalyzer {
node: Parser.SyntaxNode,
functions: JsLikeFunctionMetrics[]
): void {
const isFunctionNode =
node.type === "function_declaration" ||
node.type === "function_expression" ||
node.type === "generator_function_declaration" ||
node.type === "generator_function" ||
node.type === "method_definition" ||
node.type === "arrow_function";
const isFunctionNode = JsLikeMetricsAnalyzer.FUNCTION_NODE_TYPES.has(node.type);

if (isFunctionNode) {
// Save current state before analyzing this function
Expand Down Expand Up @@ -328,12 +351,7 @@ export class JsLikeMetricsAnalyzer {
* Checks if a node is a nested function definition.
*/
private isNestedFunction(node: Parser.SyntaxNode): boolean {
return (
node.type === "function_expression" ||
node.type === "generator_function" ||
node.type === "arrow_function" ||
node.type === "method_definition"
);
return JsLikeMetricsAnalyzer.NESTED_FUNCTION_TYPES.has(node.type);
}

/**
Expand Down Expand Up @@ -462,14 +480,6 @@ export class JsLikeMetricsAnalyzer {
* @returns True if the node increases nesting level
*/
private increasesNesting(node: Parser.SyntaxNode): boolean {
return (
node.type === "if_statement" ||
node.type === "for_statement" ||
node.type === "for_in_statement" ||
node.type === "while_statement" ||
node.type === "do_statement" ||
node.type === "switch_statement" ||
node.type === "catch_clause"
);
return JsLikeMetricsAnalyzer.NESTING_TYPES.has(node.type);
}
}
28 changes: 14 additions & 14 deletions src/metricsAnalyzer/languages/pythonAnalyzer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,19 @@ interface PythonFunctionMetrics {
* including the exact location and reason for each complexity increment.
*/
export class PythonMetricsAnalyzer {
/** Node types that increase the nesting level for cognitive complexity analysis. */
private static readonly NESTING_TYPES: ReadonlySet<string> = new Set([
"if_statement",
"for_statement",
"while_statement",
"except_clause",
"match_statement",
"list_comprehension",
"set_comprehension",
"dictionary_comprehension",
"generator_expression",
]);

/** Current nesting level during analysis */
private nesting = 0;
/** Current complexity score during analysis */
Expand Down Expand Up @@ -353,20 +366,7 @@ export class PythonMetricsAnalyzer {
* @returns True if traversing into this node's children should use an increased nesting level
*/
private increasesNesting(node: Parser.SyntaxNode): boolean {
switch (node.type) {
case "if_statement":
case "for_statement":
case "while_statement":
case "except_clause":
case "match_statement":
case "list_comprehension":
case "set_comprehension":
case "dictionary_comprehension":
case "generator_expression":
return true;
default:
return false;
}
return PythonMetricsAnalyzer.NESTING_TYPES.has(node.type);
}

/**
Expand Down
19 changes: 11 additions & 8 deletions src/metricsAnalyzer/languages/rustAnalyzer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,16 @@ interface RustFunctionMetrics {
* ```
*/
export class RustMetricsAnalyzer {
/** Node types that increase the nesting level for cognitive complexity analysis. */
private static readonly NESTING_TYPES: ReadonlySet<string> = new Set([
"if_expression",
"for_expression",
"while_expression",
"loop_expression",
"match_expression",
"closure_expression",
]);

/** Current nesting level during analysis */
private nesting = 0;
/** Current complexity score during analysis */
Expand Down Expand Up @@ -429,14 +439,7 @@ export class RustMetricsAnalyzer {
* @returns True if the node increases nesting level
*/
private increasesNesting(node: Parser.SyntaxNode): boolean {
return (
node.type === "if_expression" ||
node.type === "for_expression" ||
node.type === "while_expression" ||
node.type === "loop_expression" ||
node.type === "match_expression" ||
node.type === "closure_expression"
);
return RustMetricsAnalyzer.NESTING_TYPES.has(node.type);
}

/**
Expand Down
Loading