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

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

Overview

:nodoc:

Defined Under Namespace

Classes: ConfigurationError

Constant Summary collapse

DEFAULT_ARGS =
[
  "--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
]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ RuboCopRunner

: (*String args) -> void



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

def initialize(*args)
  @options = {} #: Hash[Symbol, untyped]
  @offenses = [] #: Array[::RuboCop::Cop::Offense]
  @errors = [] #: Array[String]
  @warnings = [] #: Array[String]
  # @prism_result = nil #: Prism::ParseLexResult?

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

  config_store = ::RuboCop::ConfigStore.new
  config_store.options_config = rubocop_options[:config] if rubocop_options[:config]
  @config_for_working_directory = config_store.for_pwd #: ::RuboCop::Config

  super(rubocop_options, config_store)
end

Instance Attribute Details

#config_for_working_directoryObject (readonly)

: ::RuboCop::Config



68
69
70
# File 'lib/ruby_lsp/requests/support/rubocop_runner.rb', line 68

def config_for_working_directory
  @config_for_working_directory
end

#offensesObject (readonly)

: Array



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

def offenses
  @offenses
end

Class Method Details

.find_cop_by_name(cop_name) ⇒ Object

: (String cop_name) -> singleton(::RuboCop::Cop::Base)?



134
135
136
# File 'lib/ruby_lsp/requests/support/rubocop_runner.rb', line 134

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

Instance Method Details

#formatted_sourceObject

: -> String



128
129
130
# File 'lib/ruby_lsp/requests/support/rubocop_runner.rb', line 128

def formatted_source
  @options[:stdin]
end

#run(path, contents, prism_result) ⇒ Object

: (String, String, Prism::ParseLexResult) -> void



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/ruby_lsp/requests/support/rubocop_runner.rb', line 97

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

  # Setting the Prism result before running the RuboCop runner makes it reuse the existing AST and avoids
  # double-parsing. Unfortunately, this leads to a bunch of cops failing to execute properly under LSP mode.
  # Uncomment this once reusing the Prism result is more stable
  # @prism_result = prism_result

  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
  # Maintain the original backtrace so that debugging cops that are breaking is easier, but re-raise as a
  # different error class
  internal_error = InternalRuboCopError.new(error)
  internal_error.set_backtrace(error.backtrace)
  raise internal_error
end