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

Nodes

The find, count, dump, and functions subcommands inspect the Abstract Syntax Tree (AST) that tree-sitter builds for a source file, rather than computing metrics from it. A node is one syntactic construct in that tree, and a node type is the grammar’s name for it (function_item, if_statement). Use these commands to locate constructs, count them, spot parse errors, or see how a grammar shapes a file.

Migrating? The verbs below replace the pre-restructure flag actions (-d, -f, --count, …). See the migration guide.

Error detection

To detect syntactic errors in your code, run:

bca find -t ERROR -I "*.ext" /path/to/your/file/or/directory
  • [PATHS]... / -p, --paths: file or directory to analyze (analyzes all files when given a directory). Paths are given positionally or via --paths; both are unioned. Flags follow the subcommand.
  • -t, --type: the node type to match. Repeat the flag for several types (-t function_item -t struct_item); at least one is required. A string value matches the node-type name exactly (for example function_item). A purely numeric value is instead interpreted as a raw tree-sitter kind_id and matches nodes whose internal symbol id equals that number; for example, -t 65535 matches the ERROR sentinel. Prefer -t ERROR for error detection. The numeric form is an escape hatch for grammar inspection and is unstable: a kind_id is an index into the grammar’s symbol table, so the same number names a different node after a grammar-version bump. Prefer the string form unless you specifically need a kind that has no stable name.
  • -I, --include: glob filter for selecting files by extension (*.js, *.rs). Each -I takes exactly one value, so a following positional path is never swallowed.

Reading find output

find prints a == <path> == banner for each file with a match, then one line per match in source order. The banner is printed even when --line-start / --line-end leave none of the file’s matches to print:

== src/main.rs ==
╰─ {call_expression:256} from (1, 13) to (1, 26) : f(g(1), h(2))
╰─ {call_expression:256} from (1, 15) to (1, 19) : g(1)
╰─ {call_expression:256} from (1, 21) to (1, 25) : h(2)

Each line gives the node type and its kind_id, then two 1-based (row, column) pairs: where the node starts, and the position just past its end. A node that fits on one row ends with its source text and one space; one that spans several rows prints none.

A match nested in another on the same row prints in full, as above, until the nesting passes 64 levels. From the 65th level down, a match prints … in place of each match nested in it, whose own line prints that text:

╰─ {call_expression:256} from (1, 141) to (1, 148) : f(…)
╰─ {call_expression:256} from (1, 143) to (1, 147) : f(1)

This keeps the output linear in the size of the file: printing every level in full made a file of 4,000 nested braces print 16 MB. The … is not escaped, so a … that is in the source reads the same. Real code rarely nests past 64 levels on one row: across the serde, pdf.js and DeepSpeech corpora the deepest is 42, in pdf.js under -t all.

Semantic filters

Six -t/--type values are not node-type names but semantic filters, resolved per language so that one spelling works across every grammar. They take precedence over an identically named node type, so a grammar with a literal call node cannot be matched by name; use its numeric kind_id for that.

  • all: every node, named and anonymous. bca count -t all therefore always reports 100%.
  • function: a named function, method or other callable declaration. Anonymous ones are excluded where the language distinguishes them: in the JavaScript family an arrow function or function expression bound to a name (const add = (a, b) => …) is a function match, while the same expression passed inline as a callback (xs.map(x => x * 2)) is a closure and is not.
  • call: a call site, meaning a function, method or command invocation a reader would navigate to. Object construction (new T(…)) and constructor delegation (C#’s : base(x) and a C# 12 primary constructor’s : Base(x), Java’s super(…), Kotlin’s superclass call) are deliberately excluded. They are counted by the ABC branches axis, whose Fitzpatrick rule is “function invocation or object creation”, a wider question than “where is this called”. Expect abc.branches to exceed a file’s call count in C#, Java, Groovy, Kotlin, C++, JavaScript, TypeScript, PHP, Ruby and Rust; see Where a constructor call lands. Each call matches once. In Perl, where the grammar parses a call’s name as a call of its own, foo(1) matches as the whole call only, and an auto-quoted hash key ((name => 1), $h{-name}), which it parses as a call too, does not match at all.
  • comment: a comment of any of the language’s forms.
  • string: a string literal. Type-annotation keywords that share the literal’s node name (TypeScript’s and PHP’s : string) are excluded.
  • error: a node that is or contains a parse error, so a match covers every ancestor of an ERROR node up to the root as well as the node itself. -t ERROR is the narrower spelling used above: it matches the error nodes alone, by name.

Counting nodes

Count occurrences of one or more node types with the count command:

bca count -t <NODE_TYPE> [-t <NODE_TYPE>...] -I "*.ext" \
    /path/to/your/file/or/directory

Printing the AST

To visualize the AST of a source file, use the dump command. It requires an explicit path, since a whole-tree AST dump is never useful:

bca dump /path/to/your/file/or/directory

Analyzing code portions

To analyze only a specific portion of the code, use the dump subcommand’s --line-start and --line-end options. For example, to print the AST of a single function from line 5 to line 10:

bca dump --line-start 5 --line-end 10 /path/to/your/file/or/directory

These flags are specific to dump and find, so they must follow the subcommand. The short --ls / --le spellings still work as deprecated aliases but are slated for removal in the next major.

Listing functions

For a list of every function or method and its line span, use:

bca functions /path/to/your/file/or/directory