Class: CodeAnalyzer::CheckingVisitor::Default

Inherits:
Base
  • Object
show all
Defined in:
lib/code_analyzer/checking_visitor/default.rb

Overview

This is the default checking visitor to check ruby sexp nodes.

Instance Method Summary collapse

Methods inherited from Base

#warnings

Constructor Details

#initialize(options = {}) ⇒ Default

Returns a new instance of Default.



5
6
7
8
9
10
11
12
13
14
15
# File 'lib/code_analyzer/checking_visitor/default.rb', line 5

def initialize(options={})
   super
   @checks = {}
   @checkers.each do |checker|
     checker.interesting_nodes.each do |node|
       @checks[node] ||= []
       @checks[node] << checker
       @checks[node].uniq!
     end
   end
end

Instance Method Details

#after_checkObject

trigger all after_check callbacks defined in all checkers.



28
29
30
31
32
33
34
35
# File 'lib/code_analyzer/checking_visitor/default.rb', line 28

def after_check
  @checkers.each do |checker|
    after_check_callbacks = checker.class.get_callbacks(:after_check)
    after_check_callbacks.each do |block|
      checker.instance_exec &block
    end
  end
end

#check(filename, content) ⇒ Object

check the ruby sexp nodes for the ruby file.

Parameters:

  • filename (String)

    is the filename of ruby code.

  • content (String)

    is the content of ruby file.



21
22
23
24
25
# File 'lib/code_analyzer/checking_visitor/default.rb', line 21

def check(filename, content)
  node = parse(filename, content)
  node.file = filename
  check_node(node)
end

#check_node(node) ⇒ Object

recursively check ruby sexp node.

  1. it triggers the interesting checkers’ start callbacks.

  2. recursively check the sexp children.

  3. it triggers the interesting checkers’ end callbacks.



52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/code_analyzer/checking_visitor/default.rb', line 52

def check_node(node)
  checkers = @checks[node.sexp_type]
  if checkers
    checkers.each { |checker| checker.node_start(node) if checker.parse_file?(node.file) }
  end
  node.children.each { |child_node|
    child_node.file = node.file
    child_node.check(self)
  }
  if checkers
    checkers.each { |checker| checker.node_end(node) if checker.parse_file?(node.file) }
  end
end

#parse(filename, content) ⇒ Object

parse ruby code.

Parameters:

  • filename (String)

    is the filename of ruby code.

  • content (String)

    is the content of ruby file.



41
42
43
44
45
# File 'lib/code_analyzer/checking_visitor/default.rb', line 41

def parse(filename, content)
  Sexp.from_array(Ripper::SexpBuilder.new(content).parse)
rescue Exception
  raise AnalyzerException.new("#{filename} looks like it's not a valid Ruby file.  Skipping...")
end