class Solargraph::Diagnostics::Rubocop
This reporter provides linting through RuboCop.
def diagnose source, _api_map
-
(Array-)
Parameters:
-
_api_map(Solargraph::ApiMap) -- -
source(Solargraph::Source) --
def diagnose source, _api_map options, paths = generate_options(source.filename, source.code) runner = RuboCop::Runner.new(options, RuboCop::ConfigStore.new) result = redirect_stdout{ runner.run(paths) } make_array JSON.parse(result) rescue RuboCop::ValidationError, RuboCop::ConfigNotFoundError => e raise DiagnosticsError, "Error in RuboCop configuration: #{e.message}" rescue JSON::ParserError raise DiagnosticsError, 'RuboCop returned invalid data' end
def make_array resp
-
(Array-)
Parameters:
-
resp(Hash) --
def make_array resp diagnostics = [] resp['files'].each do |file| file['offenses'].each do |off| diagnostics.push offense_to_diagnostic(off) end end diagnostics end
def offense_ending_position off
-
(Position)-
Parameters:
-
off(Hash) --
def offense_ending_position off if off['location']['start_line'] != off['location']['last_line'] Position.new(off['location']['start_line'], 0) else Position.new( off['location']['start_line'] - 1, off['location']['last_column'] ) end end
def offense_range off
-
(Range)-
Parameters:
-
off(Hash) --
def offense_range off Range.new(offense_start_position(off), offense_ending_position(off)) end
def offense_start_position off
-
(Position)-
Parameters:
-
off(Hash) --
def offense_start_position off Position.new(off['location']['start_line'] - 1, off['location']['start_column'] - 1) end
def offense_to_diagnostic off
-
(Hash)- LSP diagnostic
Parameters:
-
off(Hash) -- Offense received from Rubocop
def offense_to_diagnostic off { range: offense_range(off).to_hash, # 1 = Error, 2 = Warning, 3 = Information, 4 = Hint severity: SEVERITIES[off['severity']], source: off['cop_name'], message: off['message'].gsub(/^#{off['cop_name']}\:/, '') } end
def redirect_stdout
-
(String)-
Other tags:
- Todo: - This is a smelly way to redirect output, but the RuboCop specs do
def redirect_stdout redir = StringIO.new $stdout = redir yield if block_given? $stdout = STDOUT redir.string end