Class: Lumin::Worker

Inherits:
Object
  • Object
show all
Defined in:
lib/lumin/worker.rb

Defined Under Namespace

Classes: FileResult

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config:, registry: Registry) ⇒ Worker

Returns a new instance of Worker.



16
17
18
19
20
21
# File 'lib/lumin/worker.rb', line 16

def initialize(config:, registry: Registry)
  @config = config
  @registry = registry
  @rules = build_rules
  @dispatcher = build_dispatcher
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



14
15
16
# File 'lib/lumin/worker.rb', line 14

def config
  @config
end

Instance Method Details

#call(path, fix: false, unsafe: false, dry_run: false) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/lumin/worker.rb', line 23

def call(path, fix: false, unsafe: false, dry_run: false)
  if fix
    require_relative "autocorrector"
    result = Autocorrector.new(self).call(path, unsafe: unsafe, dry_run: dry_run)
    return FileResult.new(
      path: path,
      offenses: result.offenses,
      corrected: result.corrected,
      diff: result.diff,
      warnings: result.warnings,
      error: nil,
      content_digest: result.content_digest,
      fingerprint: result.fingerprint
    )
  end

  snapshot = SourceSnapshot.read(path)
  source = Astel::SourceFile.from_string(
    snapshot.source, path: path, version: @config.target_ruby
  )
  FileResult.new(
    path: path,
    offenses: lint_source(source),
    corrected: false,
    diff: nil,
    warnings: [],
    error: nil,
    content_digest: Digest::SHA256.hexdigest(snapshot.source),
    fingerprint: snapshot.fingerprint
  )
rescue StandardError => error
  FileResult.new(
    path: path,
    offenses: [],
    corrected: false,
    diff: nil,
    warnings: [],
    error: "#{error.class}: #{error.message}",
    content_digest: nil,
    fingerprint: nil
  )
end

#lint_source(source) ⇒ Object



66
67
68
69
70
71
72
# File 'lib/lumin/worker.rb', line 66

def lint_source(source)
  return syntax_offenses(source) unless source.valid?

  @rules.each { |rule| rule.begin_source(source) }
  @dispatcher.run(source.ast)
  @rules.flat_map(&:offenses)
end