0

The problem is divided in 2 pieces:

  1. Search for all the function calls with the name 'run_query' and extract the first argument.
  2. Second, use the first argument to find the definition of that variable (if the first argument is a variable).

Example:

Code (Python):

if some_bool:
  some_query = "SELECT * FROM users"
else:
  some_query = "SELECT COUNT(*) FROM users"
run_query(some_query)

Tree-sitter matches both string contents:

SELECT * FROM users
SELECT COUNT(*) FROM users

So far, I have a query that extracts the first argument from the function call

(call
 function: (attribute
            attribute: (identifier) @func_name
           )
 arguments: (argument_list
             ((identifier)+ @first_arg))
 (#match? @func_name "run\_query")
)

And a query that extracts the string content from the variable definition.

; @var_def is the SQL code surrounded by quotes
(expression_statement
 (_
  left: (identifier) @var_name
  right: [(string (string_content) @var_def)
          (concatenated_string
           (string (string_content) @var_def))
         ]
 )
)

However I am not able to glue these queries, because the function call and the variable definition are not direct siblings. I can write an specific query for this example, however I would like the query to work for any level of nesting.

0 Answers0