Skip to content

Commit

Permalink
[Fix #11033] Change warning message for Lint/Syntax when using LSP
Browse files Browse the repository at this point in the history
Fixes #11033.

## Summary

This PR changes warning message for `Lint/Syntax` between LSP and the command line.

The redundancy of the offense message probably depends on the context.
The context in which the issue has been reported is LSP.

In LSP, source code being typed may still be evaluated as invalid syntax.
On the other hand, when executed via the `rubocop` command line,
the input is usually complete and evaluated as valid syntax in most cases.
This means that suggesting a possible misconfiguration in .rubocop.yml would be
sufficient only when using the command line.

So, this is because the difference in evaluation timing of source code.
Therefore, it is implemented as a distinction based on the execution context,
rather than as a configuration option.

## Additional Information

To resolve this, it seems reasonable to switch the context of the message between
LSP and the `rubocop` command line.

As for implementation, while the issue in Solargraph won't be directly resolved,
considering a switch to RuboCop's built-in LSP could be an option.

This PR adds the `RuboCop::Base#lsp_mode?` method, which is intended to be used for
the LSP mode expansion I'm currently considering. The LSP still can be carved,
and I think there is potential to make RuboCop's LSP more user-friendly and improve it.
  • Loading branch information
koic committed Dec 31, 2023
1 parent 01928d5 commit 8aed2b5
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 3 deletions.
6 changes: 6 additions & 0 deletions lib/rubocop/cop/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -481,6 +481,12 @@ def range_for_original(range)
range.end_pos + @current_offset
)
end

# This experimental feature has been under consideration for a while.
# @api private
def lsp_mode?
ARGV.include?('--lsp')
end
end
end
end
9 changes: 6 additions & 3 deletions lib/rubocop/cop/lint/syntax.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,12 @@ def on_other_file
private

def add_offense_from_diagnostic(diagnostic, ruby_version)
message =
"#{diagnostic.message}\n(Using Ruby #{ruby_version} parser; " \
'configure using `TargetRubyVersion` parameter, under `AllCops`)'
message = if lsp_mode?
diagnostic.message
else
"#{diagnostic.message}\n(Using Ruby #{ruby_version} parser; " \
'configure using `TargetRubyVersion` parameter, under `AllCops`)'
end
add_offense(diagnostic.location, message: message, severity: diagnostic.level)
end

Expand Down
6 changes: 6 additions & 0 deletions lib/rubocop/rspec/shared_contexts.rb
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,12 @@ def source_range(range, buffer: source_buffer)
end
end

RSpec.shared_context 'lsp mode' do
before do
allow(cop).to receive(:lsp_mode?).and_return(true)
end
end

RSpec.shared_context 'ruby 2.0' do
let(:ruby_version) { 2.0 }
end
Expand Down
1 change: 1 addition & 0 deletions lib/rubocop/rspec/support.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
config.include HostEnvironmentSimulatorHelper
config.include_context 'config', :config
config.include_context 'isolated environment', :isolated_environment
config.include_context 'lsp mode', :lsp_mode
config.include_context 'maintain registry', :restore_registry
config.include_context 'ruby 2.0', :ruby20
config.include_context 'ruby 2.1', :ruby21
Expand Down
6 changes: 6 additions & 0 deletions spec/rubocop/cop/lint/syntax_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,12 @@
expect(offense.severity).to eq(:fatal)
end
end

context 'with `--lsp` option', :lsp_mode do
it 'does not include a configration information in the offense message' do

Check failure on line 55 in spec/rubocop/cop/lint/syntax_spec.rb

View workflow job for this annotation

GitHub Actions / Check spelling of all files with codespell

configration ==> configuration
expect(offenses.first.message).to eq('unexpected token $end')
end
end
end

context 'with a parser error' do
Expand Down

0 comments on commit 8aed2b5

Please sign in to comment.