Skip to content

feat(kotlin): Implement complete metrics support for Kotlin language#1226

Open
marlon-costa-dc wants to merge 2 commits intomozilla:masterfrom
marlon-costa-dc:feature/kotlin-metrics
Open

feat(kotlin): Implement complete metrics support for Kotlin language#1226
marlon-costa-dc wants to merge 2 commits intomozilla:masterfrom
marlon-costa-dc:feature/kotlin-metrics

Conversation

@marlon-costa-dc
Copy link
Contributor

Summary

Implements all 11 metric modules for Kotlin, replacing the stub implementations generated by implement_metric_trait!.

Depends on #1225 (tree-sitter 0.26.5 update — provides the tree-sitter-kotlin-codanna grammar).

Metrics implemented

Metric Description
ABC Assignment-Branch-Condition with Kotlin-specific helpers
Cognitive Nesting-aware complexity (if, when, for, while, do, catch, &&, ||)
Cyclomatic Control flow complexity
Exit Return statement detection
Halstead Full operator/operand classification for Kotlin tokens
LOC Lines of code
NArgs Number of arguments
NOM Number of methods
NPA Number of public attributes (via modifier list traversal)
NPM Number of public methods (via modifier list traversal)
WMC Weighted method complexity

Key implementation details

  • Checker trait (checker.rs): Maps tree-sitter-kotlin-codanna node types to is_comment, is_func_space, is_func, is_closure, is_string, is_call, is_non_arg
  • Getter trait (getter.rs): Full Halstead classification — 34 operators (including ?., ?:, !!, as?, !is, !in, ..<) and all operand types
  • Cognitive complexity: Follows Java pattern with nesting penalties and else if chain handling
  • NPA/NPM: Uses traverse_children to navigate property_declaration → modifiers → visibility_modifier paths
  • WMC: Extracted shared compute_wmc helper for cross-language reuse

Files changed

  • src/checker.rs — KotlinCode Checker implementation
  • src/getter.rs — KotlinCode Getter with Halstead classification
  • src/metrics/abc.rs — ABC metric with 3 Kotlin helper functions
  • src/metrics/cognitive.rs — Cognitive complexity
  • src/metrics/cyclomatic.rs — Cyclomatic complexity
  • src/metrics/exit.rs — Exit/return detection
  • src/metrics/halstead.rs — Halstead metric
  • src/metrics/loc.rs — Lines of code
  • src/metrics/nargs.rs — Number of arguments
  • src/metrics/npa.rs — Number of public attributes
  • src/metrics/npm.rs — Number of public methods
  • src/metrics/wmc.rs — Weighted method complexity
  • Also includes node.rs + asttools.rs extensions needed by metrics

Replaces

Replaces #1215 (closed due to messy branch history). Clean version rebased on feature/tree-sitter-0.26.5.

Test Plan

  • cargo check passes
  • All 252 existing tests pass (cargo test --workspace)
  • 2 clean commits (tree-sitter update base + kotlin metrics)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant