Skip to content

Commit 2ccb4c4

Browse files
committed
run rubocop lint via stdin
1 parent c80b282 commit 2ccb4c4

File tree

3 files changed

+16
-29
lines changed

3 files changed

+16
-29
lines changed

lib/language_server.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,11 +91,10 @@ def on(method, &callback)
9191

9292
on :"textDocument/didChange" do |request:, notifier:, file_store:, project:|
9393
uri = request[:params][:textDocument][:uri]
94-
filePath = uri.match(/^file:\/\/(.*\.rb)/)[1]
9594
text = request[:params][:contentChanges][0][:text]
9695
file_store.cache(uri, text)
9796
project.recalculate_result(uri)
98-
diagnostics = (Linter::Rubocop.new(filePath).call + Linter::RubyWC.new(text).call).flatten
97+
diagnostics = (Linter::Rubocop.new(text).call + Linter::RubyWC.new(text).call).flatten
9998

10099
diagnostics = diagnostics.map do |error|
101100
Protocol::Interface::Diagnostic.new(

lib/language_server/linter/rubocop.rb

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,26 +6,29 @@
66
module LanguageServer
77
module Linter
88
class Rubocop
9-
def initialize(path, config_path="")
10-
@path = path
9+
def initialize(source, config_path="")
10+
@source = source
1111
@config_path = config_path
1212
end
1313

1414

1515
def call
1616
return [] unless defined? ::RuboCop
17-
args = ["--format", "json", @path]
17+
args = []
1818
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]
19+
args += ["--format", "json","--stdin", "lsp_buffer.rb"]
2220
o = nil
2321
begin
22+
$stdin = StringIO.new(@source)
2423
$stdout = StringIO.new
24+
config_store = ::RuboCop::ConfigStore.new
25+
options, paths = ::RuboCop::Options.new.parse(args)
26+
config_store.options_config = options[:config] if options[:config]
2527
runner = ::RuboCop::Runner.new(options, config_store)
2628
runner.run(paths)
2729
o = $stdout.string
2830
ensure
31+
$stdin = STDIN
2932
$stdout = STDOUT
3033
end
3134
return [] unless o

test/language_server/linter/rubocop_test.rb

Lines changed: 6 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,8 @@ def setup
77
end
88

99
def test_fatal_error
10-
file = Tempfile.open(['bar_', '.rb']) do |v|
11-
v.puts 'require "foo'
12-
v
13-
end
14-
15-
actual = Rubocop.new(file.path, @config_path).call
16-
10+
source = 'require "foo'
11+
actual = Rubocop.new(source, @config_path).call
1712
assert do
1813
actual == [
1914
Error.new(
@@ -26,26 +21,16 @@ def test_fatal_error
2621
end
2722

2823
def test_warning_error
29-
file = Tempfile.open(['bar_', '.rb']) do |v|
30-
v.puts "a = 'a'"
31-
v
32-
end
33-
34-
actual = Rubocop.new(file.path, @config_path).call
35-
24+
source = "a = 'a'"
25+
actual = Rubocop.new(source, @config_path).call
3626
assert do
3727
actual.first == Error.new(line_num: 0, message: "Useless assignment to variable - `a`.", type: 'warning')
3828
end
3929
end
4030

4131
def test_convention_error
42-
file = Tempfile.open(['bar_', '.rb']) do |v|
43-
v.puts 'require "foo"'
44-
v
45-
end
46-
47-
actual = Rubocop.new(file.path, @config_path).call
48-
32+
source = 'require "foo"'
33+
actual = Rubocop.new(source, @config_path).call
4934
assert do
5035
actual.first == Error.new(line_num: 0, message: "Prefer single-quoted strings when you don't need string interpolation or special symbols.", type: 'warning')
5136
end

0 commit comments

Comments
 (0)