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

fix/python nested function declaration #434

Merged
Merged
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
22 changes: 14 additions & 8 deletions languages/tree-sitter-stack-graphs-python/src/stack-graphs.tsg
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

[
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
def outer(a):
def inner(b):
pass

inner(1)
# ^ defined: 2
20 changes: 20 additions & 0 deletions languages/tree-sitter-stack-graphs-python/test/not_a_method.py
Original file line number Diff line number Diff line change
@@ -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:
11 changes: 11 additions & 0 deletions languages/tree-sitter-stack-graphs-python/test/self.py
Original file line number Diff line number Diff line change
@@ -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
Loading