Class: Audition::Static::Analyzer

Inherits:
Object
  • Object
show all
Defined in:
lib/audition/static/analyzer.rb

Overview

Runs the per-file Prism checks over sources or paths.

Constant Summary collapse

PARALLEL_THRESHOLD =
16

Instance Method Summary collapse

Constructor Details

#initialize(checks: Checks.all) ⇒ Analyzer

Returns a new instance of Analyzer.

Parameters:

  • checks (Array<Class>) (defaults to: Checks.all)

    check classes to run (defaults to Checks.all)



11
12
13
# File 'lib/audition/static/analyzer.rb', line 11

def initialize(checks: Checks.all)
  @checks = checks
end

Instance Method Details

#analyze_path(path) ⇒ Array<Finding>

Parameters:

  • path (String)

    file to read and analyze

Returns:



24
25
26
# File 'lib/audition/static/analyzer.rb', line 24

def analyze_path(path)
  analyze_file(SourceFile.read(path))
end

#analyze_paths(paths, workers: default_workers, threshold: PARALLEL_THRESHOLD) ⇒ Array<Finding>

Scans files across Ractors when there are enough of them to be worth the spawn cost. Checks are plain shareable classes with deeply frozen catalogs, findings copy back through Ractor#value, and anything unexpected falls back to the serial path.

Parameters:

  • paths (Array<String>)

    files to analyze

  • workers (Integer) (defaults to: default_workers)

    Ractor count (defaults to processor count minus one)

  • threshold (Integer) (defaults to: PARALLEL_THRESHOLD)

    minimum file count before Ractors are used at all

Returns:



42
43
44
45
46
47
48
49
50
51
# File 'lib/audition/static/analyzer.rb', line 42

def analyze_paths(paths, workers: default_workers,
  threshold: PARALLEL_THRESHOLD)
  if paths.size < threshold || workers <= 1
    return paths.flat_map { |path| analyze_path(path) }
  end

  parallel_analyze(paths, workers)
rescue Ractor::Error
  paths.flat_map { |path| analyze_path(path) }
end

#analyze_source(source, path:) ⇒ Array<Finding>

Parameters:

  • source (String)

    Ruby source text

  • path (String)

    path used in findings

Returns:



18
19
20
# File 'lib/audition/static/analyzer.rb', line 18

def analyze_source(source, path:)
  analyze_file(SourceFile.new(source: source, path: path))
end