Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Go

Reference for which tree-sitter nodes pretender tracks for Go files, how each contributes to pretender check metrics, and known measurement gaps. Consult this page when tuning thresholds or diagnosing unexpected scores.

Overview

Analyses .go files using a tree-sitter grammar. Tracks both top-level functions and method declarations. Go’s select/case and type-switch cases each count as a branch, matching Go’s concurrency and type-dispatch idioms.

Tracked nodes

Tree-sitter nodeCaptureMetric impact
function_declaration@function.definitionDefines a metric scope
method_declaration@function.definitionDefines a metric scope
if_statement@branch.if+1 cyclomatic, +1 cognitive
for_statement@branch.loop+1 cyclomatic, +1 cognitive
expression_case (in switch)@branch.case+1 cyclomatic, +1 cognitive
type_case (in type switch)@branch.case+1 cyclomatic, +1 cognitive
communication_case (in select)@branch.case+1 cyclomatic, +1 cognitive
binary_expression "&&"@branch.logical.and+1 cyclomatic, +1 cognitive
binary_expression `““`
call_expression@call+1 ABC C-count
assignment_statement@assign+1 ABC A-count
short_var_declaration@assign+1 ABC A-count
var_declaration@assign+1 ABC A-count

Assertion patterns

PatternMatches
^assertassert* (testify-style)
^requirerequire.* (testify require package)
^t\.Errort.Error(...), t.Errorf(...)
^t\.Fatalt.Fatal(...), t.Fatalf(...)
^t\.Failt.Fail(), t.FailNow()

Quirks and limitations

  • Go has no else if node — else if chains are parsed as nested if_statement nodes, so each branch is counted independently (which is the intended behaviour).
  • No ternary operator — Go does not have ?:, so no ternary branches are ever counted.
  • default case in a switch is not captured as a branch — only explicit case labels contribute.
  • select blocks used for concurrency can produce high branch counts in functions that multiplex many channels.

Known unsupported metrics

  • min_assertions — Not implemented yet: metrics.scm has no @assert.* capture, so unit assertion totals are always 0 (the [assertions] patterns serve rule scopes only). No ticket filed yet.

Example output

pkg/handler.go  cyclomatic=7  cognitive=9  function_lines=42  params=2
internal/router.go  cyclomatic=11  cognitive=15  function_lines=48  params=3
  ↳ Route()  cyclomatic=11  [threshold: 10]