Shared utilities for all CodeGraph language plugins. Eliminates duplicated AST traversal, ID generation, and grammar helper functions across 6+ language plugins.
Language-agnostic complexity metrics computed via tree-sitter AST traversal. Covers TypeScript, Python, Java, Go, Rust, C#, PHP, and more through a unified node-type mapping.
calculateComplexity(node)-- Returns all three metrics in a single AST walk:cyclomatic: 1 + decision points + logical operatorscognitive: flow breaks + nesting-level penaltiesnestingDepth: maximum nesting depth of control structures
calculateCyclomatic(node)-- Cyclomatic complexity only.calculateCognitive(node)-- Cognitive complexity only.calculateNestingDepth(node)-- Max nesting depth only.classifyComplexity(metrics)-- Returns'low' | 'medium' | 'high' | 'critical'based on thresholds.COMPLEXITY_THRESHOLDS-- Configurable thresholds (cyclomatic: 10/20/50, cognitive: 15/30, nesting: 4/6).
Decision points recognized: if, for, while, switch/match, catch/except, ternary, logical operators (&&, ||, ??, and, or), plus language-specific variants for Rust expressions, Go select/defer, Python elif/with, and Java/C# switch labels.
findNodesOfType(root, types)-- Recursively finds all AST nodes matching the given type names. Used by every language plugin for entity extraction.
generateEntityId(filePath, type, name, line)-- Deterministic entity ID in the formatfilePath:type:name:line.
createGrammarHelpers(extensionToGrammar)-- Creates grammar lookup helpers from an extension-to-grammar mapping. Returns:getGrammarForExtension(ext)-- Lookup grammar by file extension.getSupportedExtensions()-- List all supported extensions.isSupported(ext)-- Check if an extension is supported.
ComplexityMetrics--{ cyclomatic: number; cognitive: number; nestingDepth: number }GrammarHelpers-- Return type ofcreateGrammarHelpers
import {
calculateComplexity,
findNodesOfType,
generateEntityId,
createGrammarHelpers,
} from '@codegraph/plugin-common';