Add Scope#yields?

This commit is contained in:
Sijawusz Pur Rahnama 2022-12-12 18:34:13 +01:00
parent 4b1378aa33
commit 6ffb635dcc
2 changed files with 16 additions and 0 deletions

View file

@ -4,6 +4,9 @@ module Ameba::AST
# Represents a context of the local variable visibility.
# This is where the local variables belong to.
class Scope
# Whether the scope yields.
setter yields = false
# Link to local variables
getter variables = [] of Variable
@ -143,6 +146,14 @@ module Ameba::AST
end || variable.used_in_macro?
end
# Returns `true` if current scope (or any of inner scopes) yields,
# `false` otherwise.
def yields?(check_inner_scopes = true)
return true if @yields
return inner_scopes.any?(&.yields?) if check_inner_scopes
false
end
# Returns `true` if current scope is a def, `false` otherwise.
def def?
node.is_a?(Crystal::Def)

View file

@ -64,6 +64,11 @@ module Ameba::AST
end
{% end %}
# :nodoc:
def visit(node : Crystal::Yield)
@current_scope.yields = true
end
# :nodoc:
def visit(node : Crystal::Def)
node.name == "->" || on_scope_enter(node)