diff --git a/src/metricsAnalyzer/languages/csharpAnalyzer.ts b/src/metricsAnalyzer/languages/csharpAnalyzer.ts index f8232d0..3ccf2db 100644 --- a/src/metricsAnalyzer/languages/csharpAnalyzer.ts +++ b/src/metricsAnalyzer/languages/csharpAnalyzer.ts @@ -77,6 +77,15 @@ interface CSharpFunctionMetrics { * ``` */ export class CSharpMetricsAnalyzer { + /** Node types that represent type declarations (class, struct, interface, record, enum). */ + private static readonly TYPE_DECLARATION_TYPES: ReadonlySet = new Set([ + "class_declaration", + "struct_declaration", + "interface_declaration", + "record_declaration", + "enum_declaration", + ]); + /** Current nesting level during analysis */ private nesting = 0; /** Current complexity score during analysis */ @@ -221,16 +230,9 @@ export class CSharpMetricsAnalyzer { * @returns The enclosing type name, or null if none found */ private getEnclosingTypeName(node: Parser.SyntaxNode): string | null { - const typeDeclarations = new Set([ - "class_declaration", - "struct_declaration", - "interface_declaration", - "record_declaration", - "enum_declaration", - ]); let parent = node.parent; while (parent) { - if (typeDeclarations.has(parent.type)) { + if (CSharpMetricsAnalyzer.TYPE_DECLARATION_TYPES.has(parent.type)) { const nameNode = parent.childForFieldName("name"); if (nameNode) { return this.sourceText.substring(nameNode.startIndex, nameNode.endIndex); diff --git a/src/metricsAnalyzer/languages/goAnalyzer.ts b/src/metricsAnalyzer/languages/goAnalyzer.ts index 7f828a7..3f1cbc1 100644 --- a/src/metricsAnalyzer/languages/goAnalyzer.ts +++ b/src/metricsAnalyzer/languages/goAnalyzer.ts @@ -268,7 +268,9 @@ export class GoMetricsAnalyzer { } /** - * Finds the type identifier within a parameter list (used for method receivers). + * Finds the type node within a parameter list (used for method receivers). + * Returns the full type node via the tree-sitter "type" field, which correctly + * handles any type form the grammar may produce (pointer, qualified, slice, etc.). * * @param parameterList - The parameter list node to search * @returns The type node or null if not found @@ -278,13 +280,9 @@ export class GoMetricsAnalyzer { ): Parser.SyntaxNode | null { for (const child of parameterList.children) { if (child.type === "parameter_declaration") { - // Look for type_identifier or pointer_type - const typeNode = child.children.find( - (c) => - c.type === "type_identifier" || - c.type === "pointer_type" || - c.type === "qualified_type" - ); + // Use childForFieldName for O(1) field access rather than a linear + // scan over the parameter's children. + const typeNode = child.childForFieldName("type"); if (typeNode) { return typeNode; }