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 =
[: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

FIXME: Kwargs are missing.

[:lvasgn, :arg, :optarg, :restarg].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:

  • (Parser::AST::Node)

Returns:

  • (Set<Symbol>)


52
53
54
55
56
57
# File 'lib/unparser/ast.rb', line 52

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>)


67
68
69
70
71
72
# File 'lib/unparser/ast.rb', line 67

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)


28
29
30
# File 'lib/unparser/ast.rb', line 28

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)


40
41
42
# File 'lib/unparser/ast.rb', line 40

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