tree-sitter-json

repository·master·Indexed 42 days ago

https://github.com/tree-sitter/tree-sitter-json

A JSON grammar implementation for the tree-sitter incremental parsing library, version 0.24.8. It enables high-performance, syntax-aware analysis of JSON files by building a concrete syntax tree, with available Node.js bindings and integration support for ESLint via eslint-config-treesitter.

Tokens
555
Snippets
3
Records
4
Agent score
3%

What's inside tree-sitter-json

  1. Integrate tree-sitter-json with ESLint using eslint-config-treesitter

    master

    To use the tree-sitter configurations in your ESLint setup, import eslint-config-treesitter and spread its exported configuration array into your eslint.config.mjs (Flat Config) file. This allows you to apply tree-sitter-based linting rules to your project.

    import treesitter from 'eslint-config-treesitter';
    
    export default [
      ...treesitter,
    ];
  2. Use tree-sitter-json in Node.js

    master

    The tree-sitter-json package provides Node.js bindings for the JSON grammar. It can be required directly in a Node.js or Bun environment. The module exports the compiled Tree-sitter grammar object, which is used to parse JSON strings into syntax trees.

    If available, the module also exports nodeTypeInfo, which contains metadata about the different node types defined in the JSON grammar (loaded from src/node-types.json).

    const Parser = require('tree-sitter');
    const JSON = require('tree-sitter-json');
    
    const parser = new Parser();
    parser.setLanguage(JSON);
    
    const sourceCode = '{"key": "value"}';
    const tree = parser.parse(sourceCode);
    console.log(tree.to_string());
  3. Access JSON node type information via nodeTypeInfo

    master

    The tree-sitter-json module may export a nodeTypeInfo property. This property contains a mapping of node type names to their respective metadata, which is useful for tools that need to understand the structure of the JSON grammar without manually inspecting the tree.

    const JSON = require('tree-sitter-json');
    
    if (JSON.nodeTypeInfo) {
      console.log(JSON.nodeTypeInfo);
    }