class DeadCodeTerminator::Ast

Attributes

ast[R]
buf[R]
cond[R]
else_branch[R]
env[R]
then_branch[R]

Public Class Methods

new(env:, ast:) click to toggle source
# File lib/dead_code_terminator/ast.rb, line 7
def initialize(env:, ast:)
  @env = env
  @ast = ast
  @buf = @ast.loc.expression.source_buffer
  @cond, @then_branch, @else_branch = @ast.children

  # handle single brackets around: `if (ENV['X'])`
  @cond = @cond.children[0] if (@cond.type == :begin) && (@cond.children.size == 1)
end

Public Instance Methods

process() click to toggle source
# File lib/dead_code_terminator/ast.rb, line 17
def process
  static_if_branch ? [erase_before_args, erase_after_args] : []
end

Private Instance Methods

begin_pos() click to toggle source
# File lib/dead_code_terminator/ast.rb, line 35
def begin_pos
  static_if_branch.loc.expression.begin_pos
end
begin_pos_before_spaces() click to toggle source
# File lib/dead_code_terminator/ast.rb, line 31
def begin_pos_before_spaces
  begin_pos - count_spaces_before_first_line_of_static_if_branch
end
count_spaces_before_first_line_of_static_if_branch() click to toggle source

:(

# File lib/dead_code_terminator/ast.rb, line 72
def count_spaces_before_first_line_of_static_if_branch
  return 0 if total_lines == 1

  (begin_pos - 1).downto(0).take_while do |pos|
    line_for_position_of_static_if_branch(pos) == from_line
  end.size
end
end_pos() click to toggle source
# File lib/dead_code_terminator/ast.rb, line 39
def end_pos
  static_if_branch.loc.expression.end_pos
end
erase_after_args() click to toggle source
# File lib/dead_code_terminator/ast.rb, line 27
def erase_after_args
  [range(end_pos, total_end), "\n" * (total_lines - to_line)]
end
erase_before_args() click to toggle source
# File lib/dead_code_terminator/ast.rb, line 23
def erase_before_args
  [range(total_begin, begin_pos_before_spaces), "\n" * (from_line - total_first_line)]
end
from_line() click to toggle source
# File lib/dead_code_terminator/ast.rb, line 51
def from_line
  @from_line ||= line_for_position_of_static_if_branch(begin_pos)
end
line_for_position_of_static_if_branch(pos) click to toggle source
# File lib/dead_code_terminator/ast.rb, line 80
def line_for_position_of_static_if_branch(pos)
  static_if_branch.loc.expression.source_buffer.line_for_position(pos)
end
range(from, to) click to toggle source
# File lib/dead_code_terminator/ast.rb, line 67
def range(from, to)
  Parser::Source::Range.new(buf, from, to)
end
static_if_branch() click to toggle source
# File lib/dead_code_terminator/ast.rb, line 84
def static_if_branch
  return @static_if_branch if defined? @static_if_branch

  Cond.nodes.each do |klass|
    if (value = klass.new(env: env, ast: cond).value)
      return (@static_if_branch = then_branch) if value == Cond::Base::THEN
      return (@static_if_branch = else_branch) if value == Cond::Base::ELSE
    end
  end

  @static_if_branch = nil
end
to_line() click to toggle source
# File lib/dead_code_terminator/ast.rb, line 55
def to_line
  line_for_position_of_static_if_branch(end_pos)
end
total_begin() click to toggle source
# File lib/dead_code_terminator/ast.rb, line 43
def total_begin
  ast.loc.expression.begin_pos
end
total_end() click to toggle source
# File lib/dead_code_terminator/ast.rb, line 47
def total_end
  ast.loc.expression.end_pos
end
total_first_line() click to toggle source
# File lib/dead_code_terminator/ast.rb, line 63
def total_first_line
  ast.loc.first_line
end
total_lines() click to toggle source
# File lib/dead_code_terminator/ast.rb, line 59
def total_lines
  ast.loc.last_line
end