Skip to content

Latest commit

 

History

History
74 lines (64 loc) · 1.73 KB

File metadata and controls

74 lines (64 loc) · 1.73 KB

Tree-Sitter Query Language Integration

This could open up many possibilities, like:

Advanced Code Analysis: provide statistics about functions in the project

QUERY LANGUAGE 'tree-sitter'
FROM PROJECT
PATTERN '''
(function_definition
  name: (identifier) @func_name
  parameters: (parameters) @params
  body: (block 
    (return_statement) @return_stmt))
'''
WITH ANALYSIS
  COUNT @func_name AS "Total Functions"
  AVERAGE (LENGTH @params) AS "Avg Parameters"
  PERCENTAGE (IS_PRESENT @return_stmt) AS "Functions with Return";

Concisely modify all methods

Find all classes and their methods in Python files, then insert a print statement after each method definition:

QUERY LANGUAGE 'tree-sitter'
FROM PROJECT
PATTERN '''
(class_definition
  name: (identifier) @class_name
  body: (block
    (function_definition
      name: (identifier) @method_name)))
'''
WITH ACTIONS
  INSERT AFTER @method_name
  CONTENT '''
@0:    print("Method called:", @method_name)
''';

Cross-language refactoring: replace all calls to "deprecated_function" across Python, JavaScript, and TypeScript files.

QUERY LANGUAGE 'tree-sitter'
FROM PROJECT
LANGUAGES ["python", "javascript", "typescript"]
PATTERN '''
(call_expression
  function: (identifier) @func_name
  (#eq? @func_name "deprecated_function"))
'''
WITH ACTIONS
  REPLACE @func_name
  WITH CONTENT "new_function";

Custom Linting Rules:

We can define project-specific linting rules using Tree-sitter queries:

QUERY LANGUAGE 'tree-sitter'
FROM PROJECT
PATTERN '''
(import_statement
  (dotted_name) @import_name
  (#match? @import_name "^(os|sys)$"))
'''
WITH LINT
  SEVERITY "WARNING"
  MESSAGE "Direct import of system modules discouraged. Use custom wrappers instead.";