#Contains a couple shared methods for Processors.
module Brakeman::ProcessorHelper
  def process_all exp
    exp.each_sexp do |e|
      process e
    end
    exp
  end

  def process_all! exp
    exp.each_with_index do |e, i|
      if sexp? e
        exp[i] = e
      end
    end

    exp
  end

  #Sets the current module.
  def process_module exp
    module_name = class_name(exp.class_name).to_s
    prev_module = @current_module

    if prev_module
      @current_module = "#{prev_module}::#{module_name}"
    else
      @current_module = module_name
    end

    process_all exp.body

    @current_module = prev_module

    exp
  end

  #Returns a class name as a Symbol.
  def class_name exp
    case exp
    when Sexp
      case exp.node_type
      when :const
        exp.value
      when :lvar
        exp.value.to_sym
      when :colon2
        "#{class_name(exp.lhs)}::#{exp.rhs}".to_sym
      when :colon3
        "::#{exp.value}".to_sym
      when :call
        process exp
      when :self
        @current_class || @current_module || nil
      else
        raise "Error: Cannot get class name from #{exp}"
      end
    when Symbol
      exp
    when nil
      nil
    else
      raise "Error: Cannot get class name from #{exp}"
    end
  end
end

#Contains a couple shared methods for Processors.
module Brakeman::ProcessorHelper
  def process_all exp
    exp.each_sexp do |e|
      process e
    end
    exp
  end

  def process_all! exp
    exp.each_with_index do |e, i|
      if sexp? e
        exp[i] = process e
      end
    end

    exp
  end

  #Sets the current module.
  def process_module exp
    module_name = class_name(exp.class_name).to_s
    prev_module = @current_module

    if prev_module
      @current_module = "#{prev_module}::#{module_name}"
    else
      @current_module = module_name
    end

    process_all exp.body

    @current_module = prev_module

    exp
  end

  #Returns a class name as a Symbol.
  def class_name exp
    case exp
    when Sexp
      case exp.node_type
      when :const
        exp.value
      when :lvar
        exp.value.to_sym
      when :colon2
        "#{class_name(exp.lhs)}::#{exp.rhs}".to_sym
      when :colon3
        "::#{exp.value}".to_sym
      when :call
        process exp
      when :self
        @current_class || @current_module || nil
      else
        raise "Error: Cannot get class name from #{exp}"
      end
    when Symbol
      exp
    when nil
      nil
    else
      raise "Error: Cannot get class name from #{exp}"
    end
  end
end

Fix horrible negligence in process_all!
Fix horrible negligence in process_all!
