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.



68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/ruby_lsp/requests/support/rubocop_runner.rb', line 68

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.



47
48
49
# File 'lib/ruby_lsp/requests/support/rubocop_runner.rb', line 47

def offenses
  @offenses
end

Class Method Details

.find_cop_by_name(cop_name) ⇒ Object



108
109
110
# File 'lib/ruby_lsp/requests/support/rubocop_runner.rb', line 108

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

Instance Method Details

#formatted_sourceObject



100
101
102
# File 'lib/ruby_lsp/requests/support/rubocop_runner.rb', line 100

def formatted_source
  @options[:stdin]
end

#run(path, contents) ⇒ Object



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

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 StandardError => error
  raise InternalRuboCopError, error
end