Skip to content

Commit

Permalink
Return empty diagnostics result if there are no syntax errors and rub…
Browse files Browse the repository at this point in the history
…ocop isn't installed (#1290)

* Return empty diagnostics result if there are no syntax errors and
rubocop isn't installed

* Address review comments
  • Loading branch information
catlee authored Jan 10, 2024
1 parent fd904d3 commit c37838b
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/ruby_lsp/requests/diagnostics.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def initialize(document)
def response
# Running RuboCop is slow, so to avoid excessive runs we only do so if the file is syntactically valid
return syntax_error_diagnostics if @document.syntax_error?
return unless defined?(Support::RuboCopDiagnosticsRunner)
return [] unless defined?(Support::RuboCopDiagnosticsRunner)

Support::RuboCopDiagnosticsRunner.instance.run(@uri, @document).map!(&:to_lsp_diagnostic)
end
Expand Down
32 changes: 32 additions & 0 deletions test/requests/diagnostics_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,36 @@ def foo
assert_equal(2, diagnostics.length)
assert_equal("expected an `end` to close the `def` statement", T.must(diagnostics.last).message)
end

def test_empty_diagnostics_without_rubocop
document = RubyLsp::RubyDocument.new(source: <<~RUBY, version: 1, uri: URI("file:///fake/file.rb"))
def foo
"Hello, world!"
end
RUBY

# We want to unload the rubocop runner for this test; first make sure that it's loaded
require "ruby_lsp/requests/support/rubocop_diagnostics_runner"
klass = RubyLsp::Requests::Support::RuboCopDiagnosticsRunner
RubyLsp::Requests::Support.send(:remove_const, :RuboCopDiagnosticsRunner)

diagnostics = T.must(RubyLsp::Requests::Diagnostics.new(document).response)

assert_empty(diagnostics)
ensure
# Restore the class
RubyLsp::Requests::Support.const_set(:RuboCopDiagnosticsRunner, klass)
end

def test_empty_diagnostics_with_rubocop
document = RubyLsp::RubyDocument.new(source: <<~RUBY, version: 1, uri: URI("file:///fake/file.rb"))
def foo
"Hello, world!"
end
RUBY

diagnostics = T.must(RubyLsp::Requests::Diagnostics.new(document).response)

refute_empty(diagnostics)
end
end

0 comments on commit c37838b

Please sign in to comment.