tree-sitter-go-types
v0.1.0
Published
Generate Go constants from tree-sitter node-types.json
Downloads
98
Maintainers
Readme
tree-sitter-go-types
Generate Go constants from a tree-sitter grammar's node-types.json.
Replaces hardcoded string literals like "pipeline_chain" with typed constants like NodePipelineChain, giving you compile-time safety and IDE discoverability when working with tree-sitter ASTs in Go.
Install
npm install --save-dev tree-sitter-go-typesOr run directly with npx:
npx tree-sitter-go-typesUsage
Run in a tree-sitter grammar repo root (where src/node-types.json exists):
# Uses defaults: reads src/node-types.json, writes node_types.go
npx tree-sitter-go-types
# Explicit paths and options
npx tree-sitter-go-types -i src/node-types.json -o node_types.go -p my_packageOptions
| Flag | Description | Default |
|------|-------------|---------|
| -i, --input <path> | Path to node-types.json | src/node-types.json |
| -o, --output <path> | Output .go file | node_types.go |
| -p, --package <name> | Go package name | tree_sitter_<grammar> |
| -g, --grammar <name> | Grammar name override | From tree-sitter.json or directory name |
Typical workflow
Add it to your grammar's build step, right after tree-sitter generate:
{
"scripts": {
"generate": "tree-sitter generate && npx tree-sitter-go-types"
}
}Output
For a PowerShell grammar, the tool generates:
// Code generated by tree-sitter-go-types v0.1.0; DO NOT EDIT.
package tree_sitter_powershell
// Node kind constants for the powershell grammar.
const (
NodeCommand = "command"
NodeCommandName = "command_name"
NodePipeline = "pipeline"
NodePipelineChain = "pipeline_chain"
NodeVariable = "variable"
// ... 152 constants total
)
// Field name constants for the powershell grammar.
const (
FieldCommandName = "command_name"
FieldCondition = "condition"
FieldValue = "value"
// ...
)Consumers use the constants instead of string literals:
import ps "github.com/wharflab/tree-sitter-powershell"
if node.Kind() == ps.NodePipelineChain {
// ...
}What it reads
The tool reads src/node-types.json, which is auto-generated by tree-sitter generate. It contains the complete schema of named node kinds and their field names. Only named node types (not anonymous tokens like "+" or "if") become constants.
Zero dependencies
The tool is a single JavaScript file with no runtime dependencies. It requires Node.js >= 18.
License
MIT
