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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ RuboCopRunner

Returns a new instance of RuboCopRunner.



75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/ruby_lsp/requests/support/rubocop_runner.rb', line 75

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
  @config_for_working_directory = T.let(config_store.for_pwd, ::RuboCop::Config)

  super(rubocop_options, config_store)
end

Instance Attribute Details

#config_for_working_directoryObject (readonly)

Returns the value of attribute config_for_working_directory.



54
55
56
# File 'lib/ruby_lsp/requests/support/rubocop_runner.rb', line 54

def config_for_working_directory
  @config_for_working_directory
end

#offensesObject (readonly)

Returns the value of attribute offenses.



51
52
53
# File 'lib/ruby_lsp/requests/support/rubocop_runner.rb', line 51

def offenses
  @offenses
end

Class Method Details

.find_cop_by_name(cop_name) ⇒ Object



120
121
122
# File 'lib/ruby_lsp/requests/support/rubocop_runner.rb', line 120

def find_cop_by_name(cop_name)
  cop_registry[cop_name]&.first
end

Instance Method Details

#formatted_sourceObject



112
113
114
# File 'lib/ruby_lsp/requests/support/rubocop_runner.rb', line 112

def formatted_source
  @options[:stdin]
end

#run(path, contents) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/ruby_lsp/requests/support/rubocop_runner.rb', line 90

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])

  # RuboCop rescues interrupts and then sets the `@aborting` variable to true. We don't want them to be rescued,
  # so here we re-raise in case RuboCop received an interrupt.
  raise Interrupt if aborting?
rescue RuboCop::Runner::InfiniteCorrectionLoop => error
  raise Formatting::Error, error.message
rescue RuboCop::ValidationError => error
  raise ConfigurationError, error.message
rescue StandardError => error
  raise InternalRuboCopError, error
end