Class: RubyLsp::Requests::Support::RuboCopRunner

Inherits:
RuboCop::Runner
  • Object
show all
Extended by:
T::Sig
Defined in:
lib/ruby_lsp/requests/support/rubocop_runner.rb

Overview

:nodoc:

Defined Under Namespace

Classes: ConfigurationError

Constant Summary collapse

DEFAULT_ARGS =
T.let([
  "--stderr", # Print any output to stderr so that our stdout does not get polluted
  "--force-exclusion",
  "--format",
  "RuboCop::Formatter::BaseFormatter", # Suppress any output by using the base formatter
].freeze, T::Array[String])

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ RuboCopRunner

Returns a new instance of RuboCopRunner.



36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/ruby_lsp/requests/support/rubocop_runner.rb', line 36

def initialize(*args)
  @options = T.let({}, T::Hash[Symbol, T.untyped])
  @offenses = T.let([], T::Array[RuboCop::Cop::Offense])
  @errors = T.let([], T::Array[String])
  @warnings = T.let([], T::Array[String])

  args += DEFAULT_ARGS
  rubocop_options = ::RuboCop::Options.new.parse(args).first
  config_store = ::RuboCop::ConfigStore.new

  super(rubocop_options, config_store)
end

Instance Attribute Details

#offensesObject (readonly)

Returns the value of attribute offenses.



26
27
28
# File 'lib/ruby_lsp/requests/support/rubocop_runner.rb', line 26

def offenses
  @offenses
end

Instance Method Details

#formatted_sourceObject



66
67
68
# File 'lib/ruby_lsp/requests/support/rubocop_runner.rb', line 66

def formatted_source
  @options[:stdin]
end

#run(path, contents) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/ruby_lsp/requests/support/rubocop_runner.rb', line 50

def run(path, contents)
  # Clear Runner state between runs since we get a single instance of this class
  # on every use site.
  @errors = []
  @warnings = []
  @offenses = []
  @options[:stdin] = contents

  super([path])
rescue RuboCop::Runner::InfiniteCorrectionLoop => error
  raise Formatting::Error, error.message
rescue RuboCop::ValidationError => error
  raise ConfigurationError, error.message
end