Class: Deprecool::Scanner
- Inherits:
-
Object
- Object
- Deprecool::Scanner
- 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
-
#initialize(finders) ⇒ Scanner
constructor
A new instance of Scanner.
- #scan_file(path) ⇒ Object
-
#scan_source(source, path = '(source)') ⇒ Object
Returns an Array
.
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
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 |