Skip to content

Fix other environment cases #177

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

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Only create fresh environment for a MethodAddBlock block
The call part of a MethodAddBlock node occurs in the same environment.
Only the block portion of it occurs in a fresh environment.
  • Loading branch information
vinistock committed Oct 18, 2022
commit 329ce7dc4e5f1af09bd83c0d6345998d0f7b4bb6
6 changes: 5 additions & 1 deletion lib/syntax_tree/visitor/with_environment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,12 @@ def visit_module(node)
with_new_environment { super }
end

# When we find a method invocation with a block, only the code that happens
# inside of the block needs a fresh environment. The method invocation
# itself happens in the same environment
def visit_method_add_block(node)
with_new_environment { super }
visit(node.call)
with_new_environment { visit(node.block) }
end

def visit_def(node)
Expand Down
27 changes: 27 additions & 0 deletions test/visitor_with_environment_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -506,5 +506,32 @@ def test_aref_on_a_method_call_with_arguments
assert_equal(1, variable.definitions[0].start_line)
assert_equal(2, variable.usages[0].start_line)
end

def test_double_aref_on_method_call
tree = SyntaxTree.parse(<<~RUBY)
object = MyObject.new
object["attributes"].find { |a| a["field"] == "expected" }["value"] = "changed"
RUBY

visitor = Collector.new
visitor.visit(tree)

assert_equal(1, visitor.arguments.length)
assert_equal(1, visitor.variables.length)

variable = visitor.variables["object"]
assert_equal(1, variable.definitions.length)
assert_equal(1, variable.usages.length)

assert_equal(1, variable.definitions[0].start_line)
assert_equal(2, variable.usages[0].start_line)

argument = visitor.arguments["a"]
assert_equal(1, argument.definitions.length)
assert_equal(1, argument.usages.length)

assert_equal(2, argument.definitions[0].start_line)
assert_equal(2, argument.usages[0].start_line)
end
end
end