diff --git a/languages/tree-sitter-stack-graphs-python/src/stack-graphs.tsg b/languages/tree-sitter-stack-graphs-python/src/stack-graphs.tsg index f9798fe54..6b5efd35f 100644 --- a/languages/tree-sitter-stack-graphs-python/src/stack-graphs.tsg +++ b/languages/tree-sitter-stack-graphs-python/src/stack-graphs.tsg @@ -798,15 +798,21 @@ inherit .parent_module let @body.class_member_attr_scope = #null } -(function_definition - parameters: (parameters - . (identifier) @param) - body: (block) @body) +; method definition +(class_definition + body: (block + (function_definition + parameters: + (parameters . (identifier) @first_param) + body: (block) @method_body + ) + ) +) { - edge @param.def -> @param.class_self_scope - edge @param.class_member_attr_scope -> @param.output - edge @param.output -> @body.after_scope - attr (@param.output) push_node = @param + edge @first_param.def -> @first_param.class_self_scope + edge @first_param.class_member_attr_scope -> @first_param.output + edge @first_param.output -> @method_body.after_scope + attr (@first_param.output) push_node = @first_param } [ diff --git a/languages/tree-sitter-stack-graphs-python/test/nested_functions.py b/languages/tree-sitter-stack-graphs-python/test/nested_functions.py new file mode 100644 index 000000000..db655e3a3 --- /dev/null +++ b/languages/tree-sitter-stack-graphs-python/test/nested_functions.py @@ -0,0 +1,6 @@ +def outer(a): + def inner(b): + pass + + inner(1) + # ^ defined: 2 diff --git a/languages/tree-sitter-stack-graphs-python/test/not_a_method.py b/languages/tree-sitter-stack-graphs-python/test/not_a_method.py new file mode 100644 index 000000000..f0a207eb0 --- /dev/null +++ b/languages/tree-sitter-stack-graphs-python/test/not_a_method.py @@ -0,0 +1,20 @@ +class Foo: + a = 1 + + def first_method(self): + self.a + # ^ defined: 2 + + def second_method(self): + self.a + # ^ defined: 2 + + # First argument here is not self + def not_a_method(not_self): + return not_self.a + # ^ defined: + + +def function(self): + self.a + # ^ defined: diff --git a/languages/tree-sitter-stack-graphs-python/test/self.py b/languages/tree-sitter-stack-graphs-python/test/self.py new file mode 100644 index 000000000..763a52eb6 --- /dev/null +++ b/languages/tree-sitter-stack-graphs-python/test/self.py @@ -0,0 +1,11 @@ +class Foo: + a = 1 + + def mathod_1(self): + return self.a + # ^ defined: 2 + + # Self can be named anything + def method_2(actually_self): + return actually_self.a + # ^ defined: 2