Skip to content

Commit

Permalink
Return empty diagnostics result if there are no syntax errors and
Browse files Browse the repository at this point in the history
rubocop isn't installed
  • Loading branch information
catlee committed Jan 9, 2024
1 parent fd904d3 commit 705361b
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 @@ -2,6 +2,7 @@
# frozen_string_literal: true

require "test_helper"
require "ruby_lsp/requests/support/rubocop_diagnostics_runner"

class DiagnosticsTest < Minitest::Test
def test_empty_diagnostics_for_ignored_file
Expand All @@ -26,4 +27,35 @@ 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

klass = RubyLsp::Requests::Support::RuboCopDiagnosticsRunner
RubyLsp::Requests::Support.send(:remove_const, :RuboCopDiagnosticsRunner)

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

assert_equal(0, diagnostics.length)
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

# Make sure the rubocop runner is loaded
diagnostics = T.must(RubyLsp::Requests::Diagnostics.new(document).run)

assert_operator(diagnostics.length, :>, 0)
end
end

0 comments on commit 705361b

Please sign in to comment.