class Leftovers::AST::Node

Public Class Methods

new(type, children = [], properties = {}) click to toggle source
Calls superclass method
# File lib/leftovers/ast/node.rb, line 8
def initialize(type, children = [], properties = {})
  # ::AST::Node#initialize freezes itself.
  # so can't use normal memoizations
  @memo = {}

  super
end

Public Instance Methods

arguments() click to toggle source
# File lib/leftovers/ast/node.rb, line 81
def arguments
  @memo.fetch(:arguments) do
    @memo[:arguments] = case type
    when :send, :csend then children.drop(2)
    when :casgn then assign_arguments(children[2])
    when :ivasgn, :cvasgn, :gvasgn then assign_arguments(second)
    when :array then children
    when :hash then [self]
    end
  end
end
assign_arguments(arguments_list) click to toggle source
# File lib/leftovers/ast/node.rb, line 93
def assign_arguments(arguments_list)
  arguments_list = arguments_list.unwrap_freeze
  case arguments_list.type
  when :array
    arguments_list.children
  when :hash, :str, :sym
    [arguments_list]
  end
end
first() click to toggle source
# File lib/leftovers/ast/node.rb, line 16
def first
  children.first
end
keep_line=(value) click to toggle source
# File lib/leftovers/ast/node.rb, line 36
def keep_line=(value)
  @memo[:keep_line] = value
end
keep_line?() click to toggle source
# File lib/leftovers/ast/node.rb, line 40
def keep_line?
  @memo[:keep_line]
end
kwargs() click to toggle source
# File lib/leftovers/ast/node.rb, line 115
def kwargs
  @memo.fetch(:kwargs) do
    @memo[:kwargs] = begin
      args = arguments
      next unless args

      last_arg = args[-1]
      last_arg if last_arg && last_arg.type == :hash
    end
  end
end
name() click to toggle source
# File lib/leftovers/ast/node.rb, line 127
def name
  @memo[:name] ||= case type
  when :send, :csend, :casgn, :const
    second
  when :def, :ivasgn, :ivar, :gvar, :cvar, :gvasgn, :cvasgn, :sym
    first
  when :str
    first.to_sym
  when :module, :class, :pair
    first.name
  end
end
path() click to toggle source
# File lib/leftovers/ast/node.rb, line 24
def path
  @memo[:path] ||= loc.expression.source_buffer.name.to_s
end
positional_arguments() click to toggle source
# File lib/leftovers/ast/node.rb, line 103
def positional_arguments
  @memo.fetch(:positional_arguments) do
    @memo[:positional_arguments] = kwargs ? arguments[0...-1] : arguments
  end
end
scalar?() click to toggle source
# File lib/leftovers/ast/node.rb, line 57
def scalar?
  case type
  when :sym, :int, :float, :str, :true, :false, :nil
    true
  else false
  end
end
second() click to toggle source
# File lib/leftovers/ast/node.rb, line 20
def second
  children[1]
end
string_or_symbol?() click to toggle source
# File lib/leftovers/ast/node.rb, line 77
def string_or_symbol?
  type == :str || type == :sym
end
test_line=(value) click to toggle source
# File lib/leftovers/ast/node.rb, line 32
def test_line=(value)
  @memo[:test_line] = value
end
test_line?() click to toggle source
# File lib/leftovers/ast/node.rb, line 28
def test_line?
  @memo[:test_line]
end
to_s() click to toggle source
# File lib/leftovers/ast/node.rb, line 65
def to_s
  @memo[:to_s] ||= name ? name.to_s : to_scalar_value.to_s
end
to_scalar_value() click to toggle source
# File lib/leftovers/ast/node.rb, line 44
def to_scalar_value
  case type
  when :sym, :int, :float, :str
    first
  when :true
    true
  when :false
    false
  when :nil
    nil
  end
end
to_sym() click to toggle source
# File lib/leftovers/ast/node.rb, line 69
def to_sym
  case type
  when :sym then first
  when :nil, :true, :false then type
  else to_s.to_sym
  end
end
unwrap_freeze() click to toggle source
# File lib/leftovers/ast/node.rb, line 109
def unwrap_freeze
  return self unless type == :send && name == :freeze

  first
end