From 8aed2b58b0182a028a36d1beacf7db08a9e87545 Mon Sep 17 00:00:00 2001 From: Koichi ITO Date: Sun, 31 Dec 2023 16:01:17 +0900 Subject: [PATCH] [Fix #11033] Change warning message for `Lint/Syntax` when using LSP 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. --- lib/rubocop/cop/base.rb | 6 ++++++ lib/rubocop/cop/lint/syntax.rb | 9 ++++++--- lib/rubocop/rspec/shared_contexts.rb | 6 ++++++ lib/rubocop/rspec/support.rb | 1 + spec/rubocop/cop/lint/syntax_spec.rb | 6 ++++++ 5 files changed, 25 insertions(+), 3 deletions(-) diff --git a/lib/rubocop/cop/base.rb b/lib/rubocop/cop/base.rb index 39cea5913e4e..d0c5dae9930b 100644 --- a/lib/rubocop/cop/base.rb +++ b/lib/rubocop/cop/base.rb @@ -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 diff --git a/lib/rubocop/cop/lint/syntax.rb b/lib/rubocop/cop/lint/syntax.rb index 457fac1c6e4f..f77c20fbe5e3 100644 --- a/lib/rubocop/cop/lint/syntax.rb +++ b/lib/rubocop/cop/lint/syntax.rb @@ -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 diff --git a/lib/rubocop/rspec/shared_contexts.rb b/lib/rubocop/rspec/shared_contexts.rb index 562d5f5588f6..19aa69890210 100644 --- a/lib/rubocop/rspec/shared_contexts.rb +++ b/lib/rubocop/rspec/shared_contexts.rb @@ -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 diff --git a/lib/rubocop/rspec/support.rb b/lib/rubocop/rspec/support.rb index 7c8777f261e7..acad83989417 100644 --- a/lib/rubocop/rspec/support.rb +++ b/lib/rubocop/rspec/support.rb @@ -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 diff --git a/spec/rubocop/cop/lint/syntax_spec.rb b/spec/rubocop/cop/lint/syntax_spec.rb index a3cc58b5316a..ca3e034fe6e0 100644 --- a/spec/rubocop/cop/lint/syntax_spec.rb +++ b/spec/rubocop/cop/lint/syntax_spec.rb @@ -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 + expect(offenses.first.message).to eq('unexpected token $end') + end + end end context 'with a parser error' do