Module: Unparser::AST

Defined in:
lib/unparser/ast.rb,
lib/unparser/ast/local_variable_scope.rb

Overview

Namespace for AST processing tools

Defined Under Namespace

Classes: Enumerator, LocalVariableScope, LocalVariableScopeEnumerator, Walker

Constant Summary collapse

FIRST_CHILD =
->(node) { node.children.first }.freeze
TAUTOLOGY =
->(_node) { true }.freeze
RESET_NODES =
%i[module class sclass def defs].freeze
INHERIT_NODES =
[:block].freeze
CLOSE_NODES =
(RESET_NODES + INHERIT_NODES).freeze
ASSIGN_NODES =

Nodes that assign a local variable

%i[
  arg
  kwarg
  kwoptarg
  lvasgn
  optarg
  procarg0
  restarg
].to_set.freeze

Class Method Summary collapse

Class Method Details

.local_variable_assignments(node) ⇒ Set<Symbol>

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Return local variables that get assigned in scope

Parameters:

  • node (Parser::AST::Node)

Returns:

  • (Set<Symbol>)


57
58
59
60
61
62
# File 'lib/unparser/ast.rb', line 57

def self.local_variable_assignments(node)
  Enumerator.new(
    node,
    method(:not_reset_scope?)
  ).types(ASSIGN_NODES)
end

.local_variable_reads(node) ⇒ Set<Symbol>

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Return local variables read

Parameters:

  • node (Parser::AST::Node)

Returns:

  • (Set<Symbol>)


72
73
74
75
76
77
# File 'lib/unparser/ast.rb', line 72

def self.local_variable_reads(node)
  Enumerator.new(
    node,
    method(:not_close_scope?)
  ).type(:lvar).map(&FIRST_CHILD).to_set
end

.not_close_scope?(node) ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Test for local variable inherited scope reset

Parameters:

  • node (Parser::AST::Node)

Returns:

  • (Boolean)


33
34
35
# File 'lib/unparser/ast.rb', line 33

def self.not_close_scope?(node)
  !CLOSE_NODES.include?(node.type)
end

.not_reset_scope?(node) ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Test for local variable scope reset

Parameters:

  • node (Parser::AST::Node)

Returns:

  • (Boolean)


45
46
47
# File 'lib/unparser/ast.rb', line 45

def self.not_reset_scope?(node)
  !RESET_NODES.include?(node.type)
end