npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

tree-sitter-bats

v0.2.0

Published

Bats (Bash Automated Testing System) grammar for tree-sitter, extending tree-sitter-bash.

Readme

tree-sitter-bats

CI

Bats (Bash Automated Testing System) grammar for tree-sitter, extending tree-sitter-bash.

Status

Early development. Until 1.0, a minor release may change node types; CHANGELOG.md notes each change.

Install

npm install tree-sitter-bats tree-sitter # Node.js
cargo add tree-sitter-bats tree-sitter   # Rust
pip install tree-sitter-bats tree-sitter # Python

Go and Swift users depend on a release tag, such as v0.2.0. Each GitHub Release attaches a Wasm build, tree-sitter-bats.wasm.

Tools that load a compiled grammar, such as ast-grep, need a build from source:

git clone --branch v0.2.0 https://github.com/nertzy/tree-sitter-bats
cd tree-sitter-bats
tree-sitter build --output bats.dylib # bats.so on Linux

Parse Bats files

Every Bash construct parses as it does in tree-sitter-bash. Bats test definitions become test_block nodes with a name field and a body field (a compound_statement). The name is a test_name node spanning everything between @test and {, as Bats reads it, so it holds a string, a raw_string, or the words of an unquoted name. This test

@test "addition using bc" {
  result="$(echo 2+2 | bc)"
  [ "$result" -eq 4 ]
}

parses as (abbreviated):

(program
  (test_block
    name: (test_name (string (string_content)))
    body: (compound_statement
      (variable_assignment ...)
      (test_command ...))))

Bats also runs a function as a test when a # @test comment ends the line that opens its body, as in name() { # @test. That comment becomes a test_marker_comment node, the first child of the function's compound_statement, instead of a comment. Its @test is a separate anonymous "@test" token, the same one a test_block starts with, so highlights can color it as a keyword. A # @test anywhere else stays a comment, since Bats ignores it.

setup, teardown, setup_file, and teardown_file are ordinary function_definition nodes, and run, load, and bats_require_minimum_version are ordinary command nodes.

With ast-grep

Register the compiled library as an ast-grep custom language. Bash reads $NAME as an expansion, so set an expandoChar for metavariables:

# sgconfig.yml
customLanguages:
  bats:
    libraryPath: path/to/bats.dylib
    extensions: [bats]
    expandoChar: _
ast-grep run --lang bats --pattern '@test $NAME { $$$BODY; }'
ast-grep scan --inline-rules '{ id: test-names, language: bats, rule: { kind: test_block } }'

Highlighting

queries/highlights.scm covers the Bash constructs plus Bats syntax: @test, test names, the setup and teardown hooks, helpers such as run, load, and skip, and the variables Bats sets ($status, $output, $lines, $stderr, $stderr_lines, and $BATS_*). tree-sitter.json points editors and tree-sitter highlight at it.

No two patterns capture the same node, so editors that prefer the first matching pattern (tree-sitter-highlight) and those that prefer the last (Zed) highlight the same way. The Rust binding test enforces this on test/highlight/ and, once script/fetch-examples has fetched them, on the example suites; CI runs it on both. tree-sitter test runs the assertions in test/highlight/.

Neovim

queries/neovim/ holds highlight, fold, indent, injection, and locals queries for nvim-treesitter. Each extends nvim-treesitter's Bash queries with Bats test blocks, hooks, helpers, and variables. Register the parser as a custom language:

vim.api.nvim_create_autocmd('User', {
  pattern = 'TSUpdate',
  callback = function()
    require('nvim-treesitter.parsers').bats = {
      install_info = {
        url = 'https://github.com/nertzy/tree-sitter-bats',
        revision = 'v0.2.0',
        queries = 'queries/neovim',
      },
      requires = { 'bash' },
    }
  end,
})

Then run :TSInstall bats, which installs the bash parser too, and enable highlighting, folds, and indentation for the bats filetype. Neovim 0.13 detects .bats files as bats; Neovim 0.12 detects them as sh, so add:

vim.filetype.add({ extension = { bats = 'bats' } })

Scope and limitations

Bats test files are Bash scripts with extra syntax. This grammar parses them by extending tree-sitter-bash 0.25.1, so anything tree-sitter-bash misparses is misparsed here too unless this grammar works around it.

Worked around:

  • A [ ... ] test, later commands with a redirect, and a following [ ... ] merge into one test_command without an ERROR node (tree-sitter-bash#316). Here a redirect inside single brackets, as in [ ! command -v go &>/dev/null ], must be a command with single-word redirect targets followed by ], and the separate-statements parse wins any remaining ambiguity.
  • A herestring after a file or heredoc redirect, as in cat > file <<<"text", produces an ERROR (tree-sitter-bash#232, #282). Here a herestring may follow the first file or heredoc redirect; a herestring before it still belongs to the command.

Not yet worked around:

  • A line with a redirect after a pipeline of three or more commands merges into the pipeline's last command (same issue, comment).
  • A heredoc whose line continues past the opener with ;, &, or more than one further statement, as in cat <<EOF; echo next or cat <<EOF | grep x; echo y, produces an ERROR. Proposed fixes: tree-sitter-bash#355 and #356.
  • More than one heredoc opened on a line, as in cat <<A <<B or cat <<A; cat <<B, produces an ERROR.
  • An argument after a herestring that follows a file redirect, as in cat > out <<< x -, produces an ERROR.
  • The read-write redirect <> produces an ERROR (tree-sitter-bash#352).
  • A quoted string containing a space inside a glob after = in [[ ... ]], as in [[ $output = *"a b"* ]], produces an ERROR (tree-sitter-bash#321); == parses.
  • A double-quoted string followed by a group in a =~ regex, as in [[ $output =~ "a "([^ ]+) ]], produces an ERROR.
  • An escaped backtick inside a quoted glob in [[ ... ]], as in [[ $output = *"\`cmd\`"* ]], produces an ERROR.

CI parses the .bats files of several public Bats suites at pinned commits. script/known-failures.txt lists the files that still produce ERROR nodes; each traces to one of the limitations above or to a deliberately malformed fixture.

Development

Requires the tree-sitter CLI 0.27.1 (pinned in package.json and CI), Node.js, and a C compiler. See CONTRIBUTING.md.

References

License

MIT. See LICENSE.