Class: Scanny::Runner

Inherits:
Object
  • Object
show all
Defined in:
lib/scanny/runner.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*checks) ⇒ Runner

Returns a new instance of Runner.



9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/scanny/runner.rb', line 9

def initialize(*checks)
  options = checks.last.is_a?(Hash) ? checks.pop : {}

  if checks.empty?
    @checks = check_classes
  else
    @checks = checks
  end

  @checks_data = []
  @parser = options[:parser] || Rubinius::Melbourne19
end

Instance Attribute Details

#checksObject (readonly)

Returns the value of attribute checks.



7
8
9
# File 'lib/scanny/runner.rb', line 7

def checks
  @checks
end

#checks_dataObject (readonly)

Returns the value of attribute checks_data.



7
8
9
# File 'lib/scanny/runner.rb', line 7

def checks_data
  @checks_data
end

#fileObject (readonly)

Returns the value of attribute file.



7
8
9
# File 'lib/scanny/runner.rb', line 7

def file
  @file
end

#parserObject (readonly)

Returns the value of attribute parser.



7
8
9
# File 'lib/scanny/runner.rb', line 7

def parser
  @parser
end

Instance Method Details

#check(file, input) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/scanny/runner.rb', line 22

def check(file, input)
  ast               = parser.new("(eval)", 1).parse_string(input)
  ignored_lines     = extract_ignored_lines(input)
  checks_performed  = 0
  nodes_inspected   = 0
  issues            = []

  @checks.each do |check|
    nodes_to_inspect = Machete.find(ast, check.compiled_pattern)
    checks_performed += 1 unless nodes_to_inspect.empty?
    nodes_inspected  += nodes_to_inspect.size

    nodes_to_inspect.each do |node|
      issues += check.visit(file, node)
    end
    issues.reject! { |i| ignored_lines.include?(i.line) }
  end

  {
    :issues             => issues,
    :checks_performed   => checks_performed,
    :nodes_inspected    => nodes_inspected,
    :file               => file
  }
end

#check_file(file) ⇒ Object



48
49
50
51
# File 'lib/scanny/runner.rb', line 48

def check_file(file)
  @file = file
  @checks_data << check(file, File.read(file))
end

#check_files(*files) ⇒ Object Also known as: run



53
54
55
# File 'lib/scanny/runner.rb', line 53

def check_files(*files)
  files.each { |f| check_file(f) }
end