Class: Deprecool::Scanner

Inherits:
Object
  • Object
show all
Defined in:
lib/deprecool/scanner.rb

Overview

Parses a source file with Prism and runs a set of finders against it

Defined Under Namespace

Classes: FinderDispatcher

Instance Method Summary collapse

Constructor Details

#initialize(finders) ⇒ Scanner

Returns a new instance of Scanner.



6
7
8
# File 'lib/deprecool/scanner.rb', line 6

def initialize(finders)
  @finders = Array(finders)
end

Instance Method Details

#scan_file(path) ⇒ Object



10
11
12
# File 'lib/deprecool/scanner.rb', line 10

def scan_file(path)
  scan_source(File.read(path), path)
end

#scan_source(source, path = '(source)') ⇒ Object

pass Prism's AST over to the dispatcher to gather offending snippets of code into an array



16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/deprecool/scanner.rb', line 16

def scan_source(source, path = '(source)')
  result = Prism.parse(source)
  return [] unless result.success?

  offenses  = []
  # each finder has the path, the whole source of the file, and the offenses list
  # so that they can build the offense warning themselves.
  # we might be able to refactor this to only pass the source because the path
  # and offenses are only needed for the add_offense method, not the actual
  # searching for an offense?
  instances = @finders.map { |finder| finder.new(path, source, offenses) }
  FinderDispatcher.new(instances).visit(result.value)
  offenses
end