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
47 changes: 28 additions & 19 deletions src/metricsAnalyzer/languages/csharpAnalyzer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,26 +183,31 @@ export class CSharpMetricsAnalyzer {
*/
public analyzeFunctions(sourceText: string): CSharpFunctionMetrics[] {
this.sourceText = sourceText;
const tree = this.parser.parse(sourceText);
const functions: CSharpFunctionMetrics[] = [];

const visit = (node: Parser.SyntaxNode) => {
if (this.isFunctionDeclaration(node)) {
const result = this.analyzeFunction(node);
if (result) {
functions.push(result);
this.heuristicReasonCache = undefined;
try {
const tree = this.parser.parse(sourceText);
const functions: CSharpFunctionMetrics[] = [];

const visit = (node: Parser.SyntaxNode) => {
if (this.isFunctionDeclaration(node)) {
const result = this.analyzeFunction(node);
if (result) {
functions.push(result);
}
}
}

// Continue traversing child nodes
for (let i = 0; i < node.childCount; i++) {
const child = node.child(i)!;
visit(child);
}
};
// Continue traversing child nodes
for (let i = 0; i < node.childCount; i++) {
const child = node.child(i)!;
visit(child);
}
};

visit(tree.rootNode);
return functions;
visit(tree.rootNode);
return functions;
} finally {
this.heuristicReasonCache = undefined;
}
}

/**
Expand Down Expand Up @@ -934,7 +939,11 @@ export class CSharpMetricsAnalyzer {
* ```
*/
public static analyzeFile(sourceText: string): CSharpFunctionMetrics[] {
const analyzer = new CSharpMetricsAnalyzer();
return analyzer.analyzeFunctions(sourceText);
return _analyzerInstance.analyzeFunctions(sourceText);
Comment thread
askpt marked this conversation as resolved.
}
}

// Module-level singleton: avoids object allocation on every analyzeFile() call.
// CSharpMetricsAnalyzer resets its mutable state at the start of each top-level
// function analysis (save/restore pattern), so the singleton is safe to reuse.
const _analyzerInstance = new CSharpMetricsAnalyzer();
8 changes: 6 additions & 2 deletions src/metricsAnalyzer/languages/goAnalyzer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -608,7 +608,11 @@ export class GoMetricsAnalyzer {
* ```
*/
public static analyzeFile(sourceText: string): GoFunctionMetrics[] {
const analyzer = new GoMetricsAnalyzer();
return analyzer.analyzeFunctions(sourceText);
return _analyzerInstance.analyzeFunctions(sourceText);
}
}

// Module-level singleton: avoids object allocation on every analyzeFile() call.
// GoMetricsAnalyzer resets its mutable state at the start of each top-level
// function analysis (save/restore pattern), so the singleton is safe to reuse.
const _analyzerInstance = new GoMetricsAnalyzer();
8 changes: 6 additions & 2 deletions src/metricsAnalyzer/languages/javaAnalyzer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -420,7 +420,11 @@ export class JavaMetricsAnalyzer {
* @returns An array of complexity analysis results for all methods found
*/
public static analyzeFile(sourceText: string): JavaFunctionMetrics[] {
const analyzer = new JavaMetricsAnalyzer();
return analyzer.analyzeFunctions(sourceText);
return _analyzerInstance.analyzeFunctions(sourceText);
}
}

// Module-level singleton: avoids object allocation on every analyzeFile() call.
// JavaMetricsAnalyzer resets its mutable state at the start of each top-level
// function analysis (save/restore pattern), so the singleton is safe to reuse.
const _analyzerInstance = new JavaMetricsAnalyzer();
8 changes: 6 additions & 2 deletions src/metricsAnalyzer/languages/pythonAnalyzer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,11 @@ export class PythonMetricsAnalyzer {
* @returns An array of complexity analysis results for all functions found
*/
public static analyzeFile(sourceText: string): PythonFunctionMetrics[] {
const analyzer = new PythonMetricsAnalyzer();
return analyzer.analyzeFunctions(sourceText);
return _analyzerInstance.analyzeFunctions(sourceText);
}
}

// Module-level singleton: avoids object allocation on every analyzeFile() call.
// PythonMetricsAnalyzer resets its mutable state at the start of each top-level
// function analysis (save/restore pattern), so the singleton is safe to reuse.
const _analyzerInstance = new PythonMetricsAnalyzer();
8 changes: 6 additions & 2 deletions src/metricsAnalyzer/languages/rustAnalyzer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -447,7 +447,11 @@ export class RustMetricsAnalyzer {
* ```
*/
public static analyzeFile(sourceText: string): RustFunctionMetrics[] {
const analyzer = new RustMetricsAnalyzer();
return analyzer.analyzeFunctions(sourceText);
return _analyzerInstance.analyzeFunctions(sourceText);
}
}

// Module-level singleton: avoids object allocation on every analyzeFile() call.
// RustMetricsAnalyzer resets its mutable state at the start of each top-level
// function analysis (save/restore pattern), so the singleton is safe to reuse.
const _analyzerInstance = new RustMetricsAnalyzer();