New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

github.com/cyber-cicco/tree-sitter-query-builder

Package Overview
Dependencies
Alerts
File Explorer
Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

github.com/cyber-cicco/tree-sitter-query-builder

  • v1.0.7
  • Source
  • Go
  • Socket score

Version published
Created
Source

Tree-sitter Query Builder

TQB is a golang library that sould be used in conjonction with the go-tree-sitter library.

You can get more details on tree sitter queries from here

It aims to ease the construction of Pattern Macthing queries, and provide you a way create queries dynamically, like such:

func Query() string {
    query := querier.NewQB()
    i := 0
    query.
    NewSExpression("call_expression").
        If(
            i == 0,
            func(e *SExpression) {
                e.For(func() bool {return i < 10},
                func(e2 *SExpression){

                    e2.NewSExpression("string_literal").
                        If(i < 9,
                        func(e *SExpression) {e.Prop("field")},
                        ).
                        NewSExpression("string_value")
                },
                func(){i += 1})
            }).
        Var("myexpression", query).
    End()
    return query.Marshal()
}

This is the equivalent of that string :

    const QUERY = `(
(call_expression
    field: (string_literal
        (string_value))
    field: (string_literal
        (string_value))
    field: (string_literal
        (string_value))
    field: (string_literal
        (string_value))
    field: (string_literal
        (string_value))
    field: (string_literal
        (string_value))
    field: (string_literal
        (string_value))
    field: (string_literal
        (string_value))
    field: (string_literal
        (string_value))
    (string_literal
        (string_value))) @myexpression
)`

You can find example of building queries from the tree sitter documentation in this file

That way, you souldn't have to worry about syntax errors in your string queries anymore.

Performances

Benchmarking shows that it takes approximately 5x times to get a query from the query builder than the parameterized query for a medium sized query. However, doing the query takes approximately 1000x more time than getting the query object with a query builder, so the difference is slim.

FAQs

Package last updated on 14 Jun 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc