Class: HamlLint::Linter::RuboCop::Runner

Inherits:
RuboCop::Runner
  • Object
show all
Defined in:
lib/haml_lint/linter/rubocop.rb

Overview

Processes a ruby file and reports RuboCop offenses

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#offensesObject (readonly)

Returns the value of attribute offenses.



21
22
23
# File 'lib/haml_lint/linter/rubocop.rb', line 21

def offenses
  @offenses
end

Instance Method Details

#cached_run?Boolean

RuboCop caches results by taking a hash of the file contents & path, among other things. It disables its cache when working on file-content from stdin. Unfortunately we always use RuboCop’s stdin, even when we’re linting a file on-disk. So, override RuboCop::Runner#cached_run? so that it’ll allow caching results, so long as haml-lint itself isn’t being invoked with files on stdin.

Returns:

  • (Boolean)


49
50
51
52
53
54
55
56
# File 'lib/haml_lint/linter/rubocop.rb', line 49

def cached_run?
  return false unless @allow_cache

  @cached_run ||=
    (@options[:cache] == 'true' ||
    (@options[:cache] != 'false' && @config_store.for_pwd.for_all_cops['UseCache'])) &&
    !@options[:auto_gen_config]
end

#corrected_codeObject



31
32
33
# File 'lib/haml_lint/linter/rubocop.rb', line 31

def corrected_code
  @options[:stdin]
end

#file_finished(_file, offenses) ⇒ Object

Executed when a file has been scanned by RuboCop, adding the reported offenses to our collection.

Parameters:

  • _file (String)
  • offenses (Array<RuboCop::Cop::Offense>)


40
41
42
# File 'lib/haml_lint/linter/rubocop.rb', line 40

def file_finished(_file, offenses)
  @offenses = offenses
end

#run(haml_path, ruby_code, config:, allow_cache: false) ⇒ Object



23
24
25
26
27
28
29
# File 'lib/haml_lint/linter/rubocop.rb', line 23

def run(haml_path, ruby_code, config:, allow_cache: false)
  @allow_cache = allow_cache
  @offenses = []
  @config_store.instance_variable_set(:@options_config, config)
  @options[:stdin] = ruby_code
  super([haml_path])
end