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