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
28 changes: 19 additions & 9 deletions src/metricsAnalyzer/languages/csharpAnalyzer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -464,16 +464,12 @@ export class CSharpMetricsAnalyzer {
private visit(node: Parser.SyntaxNode): void {
const increment = this.getComplexityIncrement(node);
if (increment > 0) {
const reason = this.getComplexityReason(node);
this.complexity += increment;

this.details.push({
this.addDetail(
increment,
reason,
line: node.startPosition.row,
column: node.startPosition.column,
nesting: this.nesting,
});
this.getComplexityReason(node),
node.startPosition.row,
node.startPosition.column
);
}

// Conditionally bump nesting, iterate children once, then restore.
Expand All @@ -491,6 +487,20 @@ export class CSharpMetricsAnalyzer {
if (isPreproc) { this.preprocessorDepth--; }
}

/**
* Records a complexity-contributing detail and adds its increment to the running total.
*/
private addDetail(increment: number, reason: string, line: number, column: number): void {
this.complexity += increment;
this.details.push({
increment,
reason,
line,
column,
nesting: this.nesting,
});
}

/**
* Calculates the complexity increment for a specific syntax node type.
*
Expand Down
37 changes: 20 additions & 17 deletions src/metricsAnalyzer/languages/goAnalyzer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -329,16 +329,12 @@ export class GoMetricsAnalyzer {
private visit(node: Parser.SyntaxNode): void {
const increment = this.getComplexityIncrement(node);
if (increment > 0) {
const reason = this.getComplexityReason(node);
this.complexity += increment;

this.details.push({
this.addDetail(
increment,
reason,
line: node.startPosition.row,
column: node.startPosition.column,
nesting: this.nesting,
});
this.getComplexityReason(node),
node.startPosition.row,
node.startPosition.column
);
}

// Conditionally bump nesting, iterate children once, then restore.
Expand Down Expand Up @@ -366,6 +362,20 @@ export class GoMetricsAnalyzer {
if (nests) { this.nesting--; }
}

/**
* Records a complexity-contributing detail and adds its increment to the running total.
*/
private addDetail(increment: number, reason: string, line: number, column: number): void {
this.complexity += increment;
this.details.push({
increment,
reason,
line,
column,
nesting: this.nesting,
});
}

/**
* Visits the alternative branch of a Go `if_statement` (the else / else-if part).
*
Expand All @@ -385,14 +395,7 @@ export class GoMetricsAnalyzer {
const reason = isElseIf ? "else if clause" : "else clause";

// Flat +1 for else/else-if — no nesting penalty.
this.complexity += 1;
this.details.push({
increment: 1,
reason,
line: node.startPosition.row,
column: node.startPosition.column,
nesting: this.nesting,
});
this.addDetail(1, reason, node.startPosition.row, node.startPosition.column);

if (isElseIf) {
// else-if: visit the inner if_statement's children at the CURRENT nesting level
Expand Down
41 changes: 25 additions & 16 deletions src/metricsAnalyzer/languages/jsLikeAnalyzer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -327,14 +327,12 @@ export class JsLikeMetricsAnalyzer {
if (!skipSelfIncrement) {
const increment = this.getComplexityIncrement(node);
if (increment > 0) {
this.details.push({
this.addDetail(
increment,
reason: this.getComplexityReason(node),
line: node.startPosition.row,
column: node.startPosition.column,
nesting: this.nesting,
});
this.complexity += increment;
this.getComplexityReason(node),
node.startPosition.row,
node.startPosition.column
);
}
}

Expand All @@ -350,15 +348,12 @@ export class JsLikeMetricsAnalyzer {
// complexity inside the nested body (ternaries, loops, etc.) counts toward
// the enclosing function rather than being silently discarded.
if (this.isNestedFunction(child)) {
const increment = 1 + this.nesting;
this.complexity += increment;
this.details.push({
increment,
reason: this.getFunctionReason(child.type),
line: child.startPosition.row,
column: child.startPosition.column,
nesting: this.nesting,
});
this.addDetail(
1 + this.nesting,
this.getFunctionReason(child.type),
child.startPosition.row,
child.startPosition.column
);
this.nesting++;
for (let i = 0; i < child.childCount; i++) {
this.analyzeNode(child.child(i)!);
Expand All @@ -381,6 +376,20 @@ export class JsLikeMetricsAnalyzer {
}
}

/**
* Records a complexity-contributing detail and adds its increment to the running total.
*/
private addDetail(increment: number, reason: string, line: number, column: number): void {
this.complexity += increment;
this.details.push({
increment,
reason,
line,
column,
nesting: this.nesting,
});
}

/**
* Checks if a node is a nested function definition.
*/
Expand Down
41 changes: 25 additions & 16 deletions src/metricsAnalyzer/languages/pythonAnalyzer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -224,15 +224,12 @@ export class PythonMetricsAnalyzer {
// that tree-sitter includes as a child inside every lambda expression node.
if (node.type === "lambda" && node.isNamed) {
if (this.nesting > 0) {
const increment = 1 + this.nesting;
this.complexity += increment;
this.details.push({
increment,
reason: "lambda (nested)",
line: node.startPosition.row,
column: node.startPosition.column,
nesting: this.nesting,
});
this.addDetail(
1 + this.nesting,
"lambda (nested)",
node.startPosition.row,
node.startPosition.column
);
}
this.nesting++;
for (let i = 0; i < node.childCount; i++) {
Expand All @@ -245,14 +242,12 @@ export class PythonMetricsAnalyzer {

const increment = this.getComplexityIncrement(node);
if (increment > 0) {
this.complexity += increment;
this.details.push({
this.addDetail(
increment,
reason: this.getComplexityReason(node),
line: node.startPosition.row,
column: node.startPosition.column,
nesting: this.nesting,
});
this.getComplexityReason(node),
node.startPosition.row,
node.startPosition.column
);
}

// Conditionally bump nesting, iterate children once, then restore.
Expand All @@ -265,6 +260,20 @@ export class PythonMetricsAnalyzer {
if (nests) { this.nesting--; }
}

/**
* Records a complexity-contributing detail and adds its increment to the running total.
*/
private addDetail(increment: number, reason: string, line: number, column: number): void {
this.complexity += increment;
this.details.push({
increment,
reason,
line,
column,
nesting: this.nesting,
});
}

/**
* Calculates the base complexity increment for a syntax node.
*
Expand Down
27 changes: 19 additions & 8 deletions src/metricsAnalyzer/languages/rustAnalyzer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -256,15 +256,12 @@ export class RustMetricsAnalyzer {
if (baseIncrement > 0) {
const nestingPenalty = this.getNestingPenalty(node);
const increment = baseIncrement + nestingPenalty;
const reason = this.getComplexityReason(node);
this.complexity += increment;
this.details.push({
this.addDetail(
increment,
reason,
line: node.startPosition.row,
column: node.startPosition.column,
nesting: this.nesting,
});
this.getComplexityReason(node),
node.startPosition.row,
node.startPosition.column
);
}

// Conditionally bump nesting, iterate children once, then restore.
Expand All @@ -280,6 +277,20 @@ export class RustMetricsAnalyzer {
if (nests) { this.nesting--; }
}

/**
* Records a complexity-contributing detail and adds its increment to the running total.
*/
private addDetail(increment: number, reason: string, line: number, column: number): void {
this.complexity += increment;
this.details.push({
increment,
reason,
line,
column,
nesting: this.nesting,
});
}

/**
* Returns the nesting penalty for a node's structural increment.
* Else/else-if clauses are counted as a flat +1 without nesting penalty.
Expand Down
Loading