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

TypeScript

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

Overview

Analyses .ts, .tsx, .mts, and .cts files using a tree-sitter grammar. Identical branch and call tracking to the JavaScript adapter; type annotations and generic type arguments are captured but carry no metric weight.

Tracked nodes

Tree-sitter nodeCaptureMetric impact
function_declaration@function.definitionDefines a metric scope
variable_declarator with arrow_function@function.definitionDefines a metric scope
method_definition@function.definitionDefines a metric scope
if_statement@branch.if+1 cyclomatic, +1 cognitive
switch_case@branch.if+1 cyclomatic, +1 cognitive
for_statement@branch.loop+1 cyclomatic, +1 cognitive
for_in_statement@branch.loop+1 cyclomatic, +1 cognitive
while_statement@branch.loop+1 cyclomatic, +1 cognitive
do_statement@branch.loop+1 cyclomatic, +1 cognitive
catch_clause@branch.catch+1 cyclomatic, +1 cognitive
ternary_expression@branch.ternary+1 cyclomatic, +1 cognitive
binary_expression "&&"@branch.logical.and+1 cyclomatic, +1 cognitive
binary_expression `““`
call_expression@call+1 ABC C-count
assignment_expression@assign+1 ABC A-count
augmented_assignment_expression@assign+1 ABC A-count
variable_declarator (with value)@assign+1 ABC A-count
type_annotation@type.annotationNone
type_arguments@type.genericNone

Assertion patterns

Same as JavaScript:

PatternMatches
^assert$assert(...)
^assert[A-Z]assertEqual(...), etc.
^expect$Jest / Vitest expect(...)
^it$it('...', ...) test blocks
^test$test('...', ...) blocks
^describe$describe('...', ...) blocks

Quirks and limitations

  • Type-only constructs (interface, type aliases, enum declarations) are not tracked as function scopes.
  • Type guards (if (x instanceof Y)) are counted as regular if_statement branches — no special handling.
  • ?? (nullish coalescing) and ?. (optional chaining) are not tracked as branches.
  • Decorators on classes and methods do not contribute to complexity.
  • TypeScript-specific syntax uses the same tree-sitter grammar revision as JavaScript (.tsx files are parsed as TSX).

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.
  • Nesting depth — Not implemented yet: the engine increments nesting only inside block-kind nodes, but tree-sitter-typescript wraps bodies in statement_block, so nesting_max stays 0. No ticket filed yet.

Example output

src/api.ts  cyclomatic=6  cognitive=8  function_lines=31  params=2
src/components/Form.tsx  cyclomatic=4  cognitive=5  function_lines=22  params=1