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

ノード

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.

移行しますか? 以下の動詞は、再構成前のフラグによるアクション(-d、-f、--count など)を置き換えるものです。移行ガイド を参照してください。

エラー検出

コード内の構文エラーを検出するには、次を実行します:

bca find -t ERROR -I "*.ext" /path/to/your/file/or/directory
  • [PATHS]... / -p, --paths:分析対象のファイルまたはディレクトリ(ディレクトリを指定するとすべてのファイルを分析します)。パスは位置引数または --paths で指定でき、両者は和集合になります。フラグはサブコマンドの後に置きます。
  • -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.

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.

ノードのカウント

count コマンドで、1 つ以上のノード種別の出現回数をカウントします:

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

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

コードの一部の分析

コードの特定の部分だけを分析するには、dump サブコマンドの --line-start と --line-end オプションを使用します。例えば、5 行目から 10 行目までの単一の関数の AST を表示するには:

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

これらのフラグは dump と find に固有のものであるため、サブコマンドの後に置く必要があります。短い綴りの --ls / --le は非推奨のエイリアスとして引き続き機能しますが、次のメジャーバージョンで削除される予定です。

関数の一覧表示

すべての関数またはメソッドとその行範囲の一覧を得るには、次を使用します:

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