Module: Reek::Source::SexpNode

Defined in:
lib/reek/source/sexp_node.rb

Overview

Extensions to Sexp to allow CodeParser to navigate the abstract syntax tree more easily.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.format(expr) ⇒ Object


8
9
10
11
12
13
# File 'lib/reek/source/sexp_node.rb', line 8

def self.format(expr)
  case expr
  when AST::Node then expr.format_ruby
  else expr.to_s
  end
end

Instance Method Details

#contains_nested_node?(target_type) ⇒ Boolean

Returns:

  • (Boolean)

69
70
71
72
# File 'lib/reek/source/sexp_node.rb', line 69

def contains_nested_node?(target_type)
  look_for(target_type) { |_elem| return true }
  false
end

#each_node(type, ignoring = [], &blk) ⇒ Object


19
20
21
22
23
24
25
26
27
# File 'lib/reek/source/sexp_node.rb', line 19

def each_node(type, ignoring = [], &blk)
  if block_given?
    look_for(type, ignoring, &blk)
  else
    result = []
    look_for(type, ignoring) { |exp| result << exp }
    result
  end
end

#each_sexpObject


35
36
37
# File 'lib/reek/source/sexp_node.rb', line 35

def each_sexp
  children.each { |elem| yield elem if elem.is_a? AST::Node }
end

#find_nodes(types, ignoring = []) ⇒ Object


29
30
31
32
33
# File 'lib/reek/source/sexp_node.rb', line 29

def find_nodes(types, ignoring = [])
  result = []
  look_for_alt(types, ignoring) { |exp| result << exp }
  result
end

#format_rubyObject


74
75
76
# File 'lib/reek/source/sexp_node.rb', line 74

def format_ruby
  SexpFormatter.format(self)
end

#hashObject


15
16
17
# File 'lib/reek/source/sexp_node.rb', line 15

def hash
  inspect.hash
end

#look_for(target_type, ignoring = [], &blk) ⇒ Object

Carries out a depth-first traversal of this syntax tree, yielding every Sexp of type target_type. The traversal ignores any node whose type is listed in the Array ignoring.


44
45
46
47
48
49
# File 'lib/reek/source/sexp_node.rb', line 44

def look_for(target_type, ignoring = [], &blk)
  each_sexp do |elem|
    elem.look_for(target_type, ignoring, &blk) unless ignoring.include?(elem.type)
  end
  blk.call(self) if type == target_type
end

#look_for_alt(target_types, ignoring = [], &blk) ⇒ Object

Carries out a depth-first traversal of this syntax tree, yielding every Sexp of type target_type. The traversal ignores any node whose type is listed in the Array ignoring, includeing the top node.

Also, doesn't nest


58
59
60
61
62
63
64
65
66
67
# File 'lib/reek/source/sexp_node.rb', line 58

def look_for_alt(target_types, ignoring = [], &blk)
  return if ignoring.include?(type)
  if target_types.include? type
    blk.call(self)
  else
    each_sexp do |elem|
      elem.look_for_alt(target_types, ignoring, &blk)
    end
  end
end