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 in a single AST traversal.

Defined Under Namespace

Classes: DispatchVisitor

Instance Method Summary collapse

Constructor Details

#initialize(finders) ⇒ Scanner

Returns a new instance of Scanner.



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

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

Instance Method Details

#scan_file(path) ⇒ Object



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

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

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

Returns an Array. Files that fail to parse yield no offenses; parse errors are surfaced separately via #parse_errors.



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

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) }
  DispatchVisitor.new(instances).visit(result.value)
  offenses
end