Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 2 commits into from
Jan 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading