Skip to content

Commit 50520da

Browse files
committed
run rubocop on LSP process
1 parent 47169fe commit 50520da

File tree

2 files changed

+25
-10
lines changed

2 files changed

+25
-10
lines changed

language_server.gemspec

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,5 @@ Gem::Specification.new do |spec|
3636
spec.add_development_dependency "m"
3737
spec.add_development_dependency "awesome_print"
3838
spec.add_development_dependency "benchmark-ips"
39+
spec.add_development_dependency "rubocop"
3940
end

lib/language_server/linter/rubocop.rb

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
require "open3"
1+
begin
2+
require 'rubocop'
3+
rescue LoadError
4+
end
25

36
module LanguageServer
47
module Linter
@@ -8,17 +11,28 @@ def initialize(path, config_path="")
811
@config_path = config_path
912
end
1013

14+
1115
def call
12-
Bundler.with_clean_env do
13-
command = "rubocop #{@path} --format json --force-exclusion"
14-
command += " --config #{@config_path}" if @config_path != ""
15-
o, err, _ = Open3.capture3(command)
16-
return [Error.new(line_num: 0, message: err, type: 'error')] if err != ""
17-
JSON
18-
.parse(o)['files'].map { |v| v['offenses'] }
19-
.flatten
20-
.map { |v| Error.new(line_num: v['location']['line'].to_i - 1, message: v['message'], type: convert_type(v['severity'])) }
16+
return [] unless defined? ::RuboCop
17+
args = ["--format", "json", @path]
18+
args += ["--config", @config_path] if @config_path != ''
19+
options, paths = ::RuboCop::Options.new.parse(args)
20+
config_store = ::RuboCop::ConfigStore.new
21+
config_store.options_config = options[:config] if options[:config]
22+
o = nil
23+
begin
24+
$stdout = StringIO.new
25+
runner = ::RuboCop::Runner.new(options, config_store)
26+
runner.run(paths)
27+
o = $stdout.string
28+
ensure
29+
$stdout = STDOUT
2130
end
31+
return [] unless o
32+
JSON
33+
.parse(o)['files'].map { |v| v['offenses'] }
34+
.flatten
35+
.map { |v| Error.new(line_num: v['location']['line'].to_i - 1, message: v['message'], type: convert_type(v['severity'])) }
2236
end
2337

2438
private

0 commit comments

Comments
 (0)