Class: LanguageServer::Linter::Rubocop

Inherits:
Object
  • Object
show all
Defined in:
lib/language_server/linter/rubocop.rb

Instance Method Summary collapse

Constructor Details

#initialize(source, config_path = "") ⇒ Rubocop

Returns a new instance of Rubocop.



9
10
11
12
# File 'lib/language_server/linter/rubocop.rb', line 9

def initialize(source, config_path="")
  @source = source
  @config_path = config_path
end

Instance Method Details

#callObject



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/language_server/linter/rubocop.rb', line 15

def call
  return [] unless defined? ::RuboCop
  args = []
  args += ["--config", @config_path] if @config_path != ''
  args += ["--format", "json","--stdin", "lsp_buffer.rb"]
  o = nil
  begin
    $stdin = StringIO.new(@source)
    $stdout = StringIO.new
    config_store = ::RuboCop::ConfigStore.new
    options, paths = ::RuboCop::Options.new.parse(args)
    config_store.options_config = options[:config] if options[:config]
    runner = ::RuboCop::Runner.new(options, config_store)
    runner.run(paths)
    o = $stdout.string
  ensure
    $stdin = STDIN
    $stdout = STDOUT
  end
  return [] unless o
  JSON
    .parse(o)['files'].map { |v| v['offenses'] }
    .flatten
    .map { |v| Error.new(line_num: v['location']['line'].to_i - 1, message: v['message'], type: convert_type(v['severity'])) }
end