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
  ],
  T::Array[String],
)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ RuboCopRunner

Returns a new instance of RuboCopRunner.



63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/ruby_lsp/requests/support/rubocop_runner.rb', line 63

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.



42
43
44
# File 'lib/ruby_lsp/requests/support/rubocop_runner.rb', line 42

def offenses
  @offenses
end

Instance Method Details

#formatted_sourceObject



95
96
97
# File 'lib/ruby_lsp/requests/support/rubocop_runner.rb', line 95

def formatted_source
  @options[:stdin]
end

#run(path, contents) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/ruby_lsp/requests/support/rubocop_runner.rb', line 77

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
rescue RuboCop::ErrorWithAnalyzedFileLocation => error
  raise InternalRuboCopError, error
end