Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

static analysis #1266

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions pr_agent/static_analysis/queries/Credits.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@

# Credits

These queries, and some of the logic, were adopted from the excellent [Aider](https://github.com/paul-gauthier/aider/blob/main/aider/queries/README.md) project.

9 changes: 9 additions & 0 deletions pr_agent/static_analysis/queries/tree-sitter-c-tags.scm
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
(struct_specifier name: (type_identifier) @name.definition.class body:(_)) @definition.class

(declaration type: (union_specifier name: (type_identifier) @name.definition.class)) @definition.class

(function_declarator declarator: (identifier) @name.definition.function) @definition.function

(type_definition declarator: (type_identifier) @name.definition.type) @definition.type

(enum_specifier name: (type_identifier) @name.definition.type) @definition.type
46 changes: 46 additions & 0 deletions pr_agent/static_analysis/queries/tree-sitter-c_sharp-tags.scm
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
(class_declaration
name: (identifier) @name.definition.class
) @definition.class

(class_declaration
bases: (base_list (_) @name.reference.class)
) @reference.class

(interface_declaration
name: (identifier) @name.definition.interface
) @definition.interface

(interface_declaration
bases: (base_list (_) @name.reference.interface)
) @reference.interface

(method_declaration
name: (identifier) @name.definition.method
) @definition.method

(object_creation_expression
type: (identifier) @name.reference.class
) @reference.class

(type_parameter_constraints_clause
target: (identifier) @name.reference.class
) @reference.class

(type_constraint
type: (identifier) @name.reference.class
) @reference.class

(variable_declaration
type: (identifier) @name.reference.class
) @reference.class

(invocation_expression
function:
(member_access_expression
name: (identifier) @name.reference.send
)
) @reference.send

(namespace_declaration
name: (identifier) @name.definition.module
) @definition.module
15 changes: 15 additions & 0 deletions pr_agent/static_analysis/queries/tree-sitter-cpp-tags.scm
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
(struct_specifier name: (type_identifier) @name.definition.class body:(_)) @definition.class

(declaration type: (union_specifier name: (type_identifier) @name.definition.class)) @definition.class

(function_declarator declarator: (identifier) @name.definition.function) @definition.function

(function_declarator declarator: (field_identifier) @name.definition.function) @definition.function

(function_declarator declarator: (qualified_identifier scope: (namespace_identifier) @scope name: (identifier) @name.definition.method)) @definition.method

(type_definition declarator: (type_identifier) @name.definition.type) @definition.type

(enum_specifier name: (type_identifier) @name.definition.type) @definition.type

(class_specifier name: (type_identifier) @name.definition.class) @definition.class
8 changes: 8 additions & 0 deletions pr_agent/static_analysis/queries/tree-sitter-elisp-tags.scm
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
;; defun/defsubst
(function_definition name: (symbol) @name.definition.function) @definition.function

;; Treat macros as function definitions for the sake of TAGS.
(macro_definition name: (symbol) @name.definition.function) @definition.function

;; Match function calls
(list (symbol) @name.reference.function) @reference.function
54 changes: 54 additions & 0 deletions pr_agent/static_analysis/queries/tree-sitter-elixir-tags.scm
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
; Definitions

; * modules and protocols
(call
target: (identifier) @ignore
(arguments (alias) @name.definition.module)
(#match? @ignore "^(defmodule|defprotocol)$")) @definition.module

; * functions/macros
(call
target: (identifier) @ignore
(arguments
[
; zero-arity functions with no parentheses
(identifier) @name.definition.function
; regular function clause
(call target: (identifier) @name.definition.function)
; function clause with a guard clause
(binary_operator
left: (call target: (identifier) @name.definition.function)
operator: "when")
])
(#match? @ignore "^(def|defp|defdelegate|defguard|defguardp|defmacro|defmacrop|defn|defnp)$")) @definition.function

; References

; ignore calls to kernel/special-forms keywords
(call
target: (identifier) @ignore
(#match? @ignore "^(def|defp|defdelegate|defguard|defguardp|defmacro|defmacrop|defn|defnp|defmodule|defprotocol|defimpl|defstruct|defexception|defoverridable|alias|case|cond|else|for|if|import|quote|raise|receive|require|reraise|super|throw|try|unless|unquote|unquote_splicing|use|with)$"))

; ignore module attributes
(unary_operator
operator: "@"
operand: (call
target: (identifier) @ignore))

; * function call
(call
target: [
; local
(identifier) @name.reference.call
; remote
(dot
right: (identifier) @name.reference.call)
]) @reference.call

; * pipe into function call
(binary_operator
operator: "|>"
right: (identifier) @name.reference.call) @reference.call

; * modules
(alias) @name.reference.module @reference.module
19 changes: 19 additions & 0 deletions pr_agent/static_analysis/queries/tree-sitter-elm-tags.scm
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
(value_declaration (function_declaration_left (lower_case_identifier) @name.definition.function)) @definition.function

(function_call_expr (value_expr (value_qid) @name.reference.function)) @reference.function
(exposed_value (lower_case_identifier) @name.reference.function) @reference.function
(type_annotation ((lower_case_identifier) @name.reference.function) (colon)) @reference.function

(type_declaration ((upper_case_identifier) @name.definition.type)) @definition.type

(type_ref (upper_case_qid (upper_case_identifier) @name.reference.type)) @reference.type
(exposed_type (upper_case_identifier) @name.reference.type) @reference.type

(type_declaration (union_variant (upper_case_identifier) @name.definition.union)) @definition.union

(value_expr (upper_case_qid (upper_case_identifier) @name.reference.union)) @reference.union


(module_declaration
(upper_case_qid (upper_case_identifier)) @name.definition.module
) @definition.module
30 changes: 30 additions & 0 deletions pr_agent/static_analysis/queries/tree-sitter-go-tags.scm
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
(
(comment)* @doc
.
(function_declaration
name: (identifier) @name.definition.function) @definition.function
(#strip! @doc "^//\\s*")
(#set-adjacent! @doc @definition.function)
)

(
(comment)* @doc
.
(method_declaration
name: (field_identifier) @name.definition.method) @definition.method
(#strip! @doc "^//\\s*")
(#set-adjacent! @doc @definition.method)
)

(call_expression
function: [
(identifier) @name.reference.call
(parenthesized_expression (identifier) @name.reference.call)
(selector_expression field: (field_identifier) @name.reference.call)
(parenthesized_expression (selector_expression field: (field_identifier) @name.reference.call))
]) @reference.call

(type_spec
name: (type_identifier) @name.definition.type) @definition.type

(type_identifier) @name.reference.type @reference.type
20 changes: 20 additions & 0 deletions pr_agent/static_analysis/queries/tree-sitter-java-tags.scm
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
(class_declaration
name: (identifier) @name.definition.class) @definition.class

(method_declaration
name: (identifier) @name.definition.method) @definition.method

(method_invocation
name: (identifier) @name.reference.call
arguments: (argument_list) @reference.call)

(interface_declaration
name: (identifier) @name.definition.interface) @definition.interface

(type_list
(type_identifier) @name.reference.implementation) @reference.implementation

(object_creation_expression
type: (type_identifier) @name.reference.class) @reference.class

(superclass (type_identifier) @name.reference.class) @reference.class
88 changes: 88 additions & 0 deletions pr_agent/static_analysis/queries/tree-sitter-javascript-tags.scm
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
(
(comment)* @doc
.
(method_definition
name: (property_identifier) @name.definition.method) @definition.method
(#not-eq? @name.definition.method "constructor")
(#strip! @doc "^[\\s\\*/]+|^[\\s\\*/]$")
(#select-adjacent! @doc @definition.method)
)

(
(comment)* @doc
.
[
(class
name: (_) @name.definition.class)
(class_declaration
name: (_) @name.definition.class)
] @definition.class
(#strip! @doc "^[\\s\\*/]+|^[\\s\\*/]$")
(#select-adjacent! @doc @definition.class)
)

(
(comment)* @doc
.
[
(function
name: (identifier) @name.definition.function)
(function_declaration
name: (identifier) @name.definition.function)
(generator_function
name: (identifier) @name.definition.function)
(generator_function_declaration
name: (identifier) @name.definition.function)
] @definition.function
(#strip! @doc "^[\\s\\*/]+|^[\\s\\*/]$")
(#select-adjacent! @doc @definition.function)
)

(
(comment)* @doc
.
(lexical_declaration
(variable_declarator
name: (identifier) @name.definition.function
value: [(arrow_function) (function)]) @definition.function)
(#strip! @doc "^[\\s\\*/]+|^[\\s\\*/]$")
(#select-adjacent! @doc @definition.function)
)

(
(comment)* @doc
.
(variable_declaration
(variable_declarator
name: (identifier) @name.definition.function
value: [(arrow_function) (function)]) @definition.function)
(#strip! @doc "^[\\s\\*/]+|^[\\s\\*/]$")
(#select-adjacent! @doc @definition.function)
)

(assignment_expression
left: [
(identifier) @name.definition.function
(member_expression
property: (property_identifier) @name.definition.function)
]
right: [(arrow_function) (function)]
) @definition.function

(pair
key: (property_identifier) @name.definition.function
value: [(arrow_function) (function)]) @definition.function

(
(call_expression
function: (identifier) @name.reference.call) @reference.call
(#not-match? @name.reference.call "^(require)$")
)

(call_expression
function: (member_expression
property: (property_identifier) @name.reference.call)
arguments: (_) @reference.call)

(new_expression
constructor: (_) @name.reference.class) @reference.class
Loading
Loading