From 9f65a88dec70e6c8c1b535c4b7bcc52361cb0720 Mon Sep 17 00:00:00 2001 From: jordanbreen28 Date: Thu, 21 Sep 2023 14:52:36 +0100 Subject: [PATCH] (maint) - Rubocop Fixes --- .github/workflows/ci.yml | 2 +- .github/workflows/nightly.yml | 2 +- .rubocop.yml | 1 + .rubocop_todo.yml | 23 ++++++++- Gemfile | 6 +-- lib/dsp/dsp.rb | 8 ++- lib/lsp/lsp.rb | 8 ++- lib/puppet-debugserver/hooks.rb | 10 ++-- .../global_queues/single_instance_queue.rb | 10 ++-- .../manifest/validation_provider.rb | 50 +++++++++---------- lib/puppet-languageserver/providers.rb | 8 ++- .../puppet_monkey_patches.rb | 12 ++--- .../server_capabilities.rb | 3 +- lib/puppet_debugserver.rb | 8 ++- .../protocol/json_rpc.rb | 2 +- lib/puppet_editor_services/server/tcp.rb | 20 +++----- lib/puppet_languageserver.rb | 16 +++--- lib/puppet_languageserver_sidecar.rb | 32 +++++------- 18 files changed, 104 insertions(+), 117 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 33527c95..41a98da0 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -43,7 +43,7 @@ jobs: - ruby_version: '3.2' puppet_version: '~> 8.0' - name: "Acceptance : ${{ matrix.runs_on }} Ruby ${{ matrix.ruby_version }} Puppet ${{ matrix.puppet_version }}}" + name: "Acceptance : ${{ matrix.runs_on }} Ruby ${{ matrix.ruby_version }} Puppet ${{ matrix.puppet_version }}" uses: "puppetlabs/cat-github-actions/.github/workflows/gem_acceptance.yml@main" secrets: "inherit" with: diff --git a/.github/workflows/nightly.yml b/.github/workflows/nightly.yml index 14516c0a..b1fbbfa7 100644 --- a/.github/workflows/nightly.yml +++ b/.github/workflows/nightly.yml @@ -42,7 +42,7 @@ jobs: - ruby_version: '3.2' puppet_version: '~> 8.0' - name: "Acceptance : ${{ matrix.runs_on }} Ruby ${{ matrix.ruby_version }} Puppet ${{ matrix.puppet_version }}}" + name: "Acceptance : ${{ matrix.runs_on }} Ruby ${{ matrix.ruby_version }} Puppet ${{ matrix.puppet_version }}" uses: "puppetlabs/cat-github-actions/.github/workflows/gem_acceptance.yml@main" secrets: "inherit" with: diff --git a/.rubocop.yml b/.rubocop.yml index 8f495485..64479997 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -1,6 +1,7 @@ inherit_from: .rubocop_todo.yml AllCops: + TargetRubyVersion: 2.7 Include: - 'lib/**/*.rb' - 'puppet-languageserver' diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index b1cfe267..ba6b69e8 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -1 +1,22 @@ -# Empty \ No newline at end of file +# This configuration was generated by +# `rubocop --auto-gen-config` +# on 2023-09-21 13:50:42 UTC using RuboCop version 1.48.1. +# The point is for the user to remove these configuration records +# one by one as the offenses are removed from the code base. +# Note that changes in the inspected code, or installation of new +# versions of RuboCop, may require this file to be generated again. + +# Offense count: 2 +# This cop supports unsafe autocorrection (--autocorrect-all). +Style/SlicingWithRange: + Exclude: + - 'lib/puppet-languageserver/manifest/hover_provider.rb' + - 'lib/puppet-languageserver/puppet_lexer_helper.rb' + +# Offense count: 3 +# This cop supports unsafe autocorrection (--autocorrect-all). +Style/ZeroLengthPredicate: + Exclude: + - 'lib/puppet-debugserver/debug_session/hook_handlers.rb' + - 'lib/puppet-languageserver/message_handler.rb' + - 'lib/puppet_editor_services/server/tcp.rb' diff --git a/Gemfile b/Gemfile index 6ae97cd0..f475e3e4 100644 --- a/Gemfile +++ b/Gemfile @@ -15,9 +15,9 @@ group :development do gem 'puppetfile-resolver', '~> 0.6.2', :require => false gem 'yard', '~> 0.9.28', :require => false - gem "rubocop", '= 1.6.1', require: false - gem "rubocop-performance", '= 1.9.1', require: false - gem "rubocop-rspec", '= 2.0.1', require: false + gem "rubocop", '~> 1.48.1', require: false + gem "rubocop-performance", '~> 1.16', require: false + gem "rubocop-rspec", '~> 2.19', require: false if ENV['PUPPET_GEM_VERSION'] gem 'puppet', ENV['PUPPET_GEM_VERSION'], :require => false diff --git a/lib/dsp/dsp.rb b/lib/dsp/dsp.rb index 962aa488..52a20a30 100644 --- a/lib/dsp/dsp.rb +++ b/lib/dsp/dsp.rb @@ -1,9 +1,7 @@ # frozen_string_literal: true %w[dsp_base dsp_protocol].each do |lib| - begin - require "dsp/#{lib}" - rescue LoadError - require File.expand_path(File.join(__dir__, lib)) - end + require "dsp/#{lib}" +rescue LoadError + require File.expand_path(File.join(__dir__, lib)) end diff --git a/lib/lsp/lsp.rb b/lib/lsp/lsp.rb index 5224e485..36682f88 100644 --- a/lib/lsp/lsp.rb +++ b/lib/lsp/lsp.rb @@ -4,9 +4,7 @@ # See tools/lsp_introspect/index.js %w[lsp_base lsp_custom lsp_types lsp_enums lsp_protocol_callhierarchy.proposed lsp_protocol_colorprovider lsp_protocol_configuration lsp_protocol lsp_protocol_declaration lsp_protocol_foldingrange lsp_protocol_implementation lsp_protocol_progress lsp_protocol_selectionrange lsp_protocol_sematictokens.proposed lsp_protocol_typedefinition lsp_protocol_workspacefolders].each do |lib| - begin - require "lsp/#{lib}" - rescue LoadError - require File.expand_path(File.join(File.dirname(__FILE__), lib)) - end + require "lsp/#{lib}" +rescue LoadError + require File.expand_path(File.join(File.dirname(__FILE__), lib)) end diff --git a/lib/puppet-debugserver/hooks.rb b/lib/puppet-debugserver/hooks.rb index ac316d7c..d33c08fd 100644 --- a/lib/puppet-debugserver/hooks.rb +++ b/lib/puppet-debugserver/hooks.rb @@ -60,12 +60,10 @@ def add_hook(event_name, hook_name, callable = nil, &block) def exec_hook(event_name, *args, &block) PuppetDebugServer.log_message(:debug, "Starting to execute hook #{event_name}") unless event_name == :hook_log_message @hooks[event_name.to_s].map do |_hook_name, callable| - begin - callable.call(*args, &block) - rescue ::RuntimeError => e - errors << e - e - end + callable.call(*args, &block) + rescue ::RuntimeError => e + errors << e + e end.last PuppetDebugServer.log_message(:debug, "Finished executing hook #{event_name}") unless event_name == :hook_log_message end diff --git a/lib/puppet-languageserver/global_queues/single_instance_queue.rb b/lib/puppet-languageserver/global_queues/single_instance_queue.rb index 49d9fcb0..62358145 100644 --- a/lib/puppet-languageserver/global_queues/single_instance_queue.rb +++ b/lib/puppet-languageserver/global_queues/single_instance_queue.rb @@ -59,12 +59,10 @@ def enqueue_job(job_object) # Append a new thread if we have space if @queue_threads.count < max_queue_threads @queue_threads << Thread.new do - begin - thread_worker - rescue => e # rubocop:disable Style/RescueStandardError - PuppetLanguageServer.log_message(:error, "Error in #{self.class} Thread: #{e}") - raise - end + thread_worker + rescue => e # rubocop:disable Style/RescueStandardError + PuppetLanguageServer.log_message(:error, "Error in #{self.class} Thread: #{e}") + raise end end end diff --git a/lib/puppet-languageserver/manifest/validation_provider.rb b/lib/puppet-languageserver/manifest/validation_provider.rb index 0f2476e4..6022b85d 100644 --- a/lib/puppet-languageserver/manifest/validation_provider.rb +++ b/lib/puppet-languageserver/manifest/validation_provider.rb @@ -77,33 +77,29 @@ def self.validate(session_state, content, options = {}) env = Puppet.lookup(:current_environment) loaders = Puppet::Pops::Loaders.new(env) Puppet.override({ loaders: loaders }, 'For puppet parser validate') do - begin - validation_environment = env - $PuppetParserMutex.synchronize do # rubocop:disable Style/GlobalVars - begin - original_taskmode = Puppet[:tasks] if Puppet.tasks_supported? - Puppet[:tasks] = options[:tasks_mode] if Puppet.tasks_supported? - validation_environment.check_for_reparse - validation_environment.known_resource_types.clear - ensure - Puppet[:tasks] = original_taskmode if Puppet.tasks_supported? - end - end - rescue StandardError => e - # Sometimes the error is in the cause not the root object itself - e = e.cause if !e.respond_to?(:line) && e.respond_to?(:cause) - ex_line = e.respond_to?(:line) && !e.line.nil? ? e.line - 1 : nil # Line numbers from puppet exceptions are base 1 - ex_pos = e.respond_to?(:pos) && !e.pos.nil? ? e.pos : nil # Pos numbers from puppet are base 1 - - message = e.respond_to?(:message) ? e.message : nil - message = e.basic_message if message.nil? && e.respond_to?(:basic_message) - - unless ex_line.nil? || ex_pos.nil? || message.nil? - result << LSP::Diagnostic.new('severity' => LSP::DiagnosticSeverity::ERROR, - 'range' => LSP.create_range(ex_line, ex_pos, ex_line, ex_pos + 1), - 'source' => 'Puppet', - 'message' => message) - end + validation_environment = env + $PuppetParserMutex.synchronize do # rubocop:disable Style/GlobalVars + original_taskmode = Puppet[:tasks] if Puppet.tasks_supported? + Puppet[:tasks] = options[:tasks_mode] if Puppet.tasks_supported? + validation_environment.check_for_reparse + validation_environment.known_resource_types.clear + ensure + Puppet[:tasks] = original_taskmode if Puppet.tasks_supported? + end + rescue StandardError => e + # Sometimes the error is in the cause not the root object itself + e = e.cause if !e.respond_to?(:line) && e.respond_to?(:cause) + ex_line = e.respond_to?(:line) && !e.line.nil? ? e.line - 1 : nil # Line numbers from puppet exceptions are base 1 + ex_pos = e.respond_to?(:pos) && !e.pos.nil? ? e.pos : nil # Pos numbers from puppet are base 1 + + message = e.respond_to?(:message) ? e.message : nil + message = e.basic_message if message.nil? && e.respond_to?(:basic_message) + + unless ex_line.nil? || ex_pos.nil? || message.nil? + result << LSP::Diagnostic.new('severity' => LSP::DiagnosticSeverity::ERROR, + 'range' => LSP.create_range(ex_line, ex_pos, ex_line, ex_pos + 1), + 'source' => 'Puppet', + 'message' => message) end end diff --git a/lib/puppet-languageserver/providers.rb b/lib/puppet-languageserver/providers.rb index f7e6185e..566a9419 100644 --- a/lib/puppet-languageserver/providers.rb +++ b/lib/puppet-languageserver/providers.rb @@ -12,9 +12,7 @@ manifest/hover_provider puppetfile/validation_provider ].each do |lib| - begin - require "puppet-languageserver/#{lib}" - rescue LoadError - require File.expand_path(File.join(File.dirname(__FILE__), lib)) - end + require "puppet-languageserver/#{lib}" +rescue LoadError + require File.expand_path(File.join(File.dirname(__FILE__), lib)) end diff --git a/lib/puppet-languageserver/puppet_monkey_patches.rb b/lib/puppet-languageserver/puppet_monkey_patches.rb index 58c1e687..2641a2cd 100644 --- a/lib/puppet-languageserver/puppet_monkey_patches.rb +++ b/lib/puppet-languageserver/puppet_monkey_patches.rb @@ -11,13 +11,11 @@ module Parser class Parser def singleton_parse_string(code, task_mode = false, path = nil) $PuppetParserMutex.synchronize do # rubocop:disable Style/GlobalVars - begin - original_taskmode = Puppet[:tasks] if Puppet.tasks_supported? - Puppet[:tasks] = task_mode if Puppet.tasks_supported? - return parse_string(code, path) - ensure - Puppet[:tasks] = original_taskmode if Puppet.tasks_supported? - end + original_taskmode = Puppet[:tasks] if Puppet.tasks_supported? + Puppet[:tasks] = task_mode if Puppet.tasks_supported? + return parse_string(code, path) + ensure + Puppet[:tasks] = original_taskmode if Puppet.tasks_supported? end end end diff --git a/lib/puppet-languageserver/server_capabilities.rb b/lib/puppet-languageserver/server_capabilities.rb index cdf0e73b..73c1fbab 100644 --- a/lib/puppet-languageserver/server_capabilities.rb +++ b/lib/puppet-languageserver/server_capabilities.rb @@ -42,8 +42,7 @@ def self.folding_range_provider_options def self.no_capabilities # Any empty hash denotes no capabilities at all - { - } + {} end end end diff --git a/lib/puppet_debugserver.rb b/lib/puppet_debugserver.rb index ef81e265..d487e415 100644 --- a/lib/puppet_debugserver.rb +++ b/lib/puppet_debugserver.rb @@ -40,11 +40,9 @@ def self.require_gems(options) debug_session/puppet_session_state puppet_monkey_patches ].each do |lib| - begin - require "puppet-debugserver/#{lib}" - rescue LoadError - require File.expand_path(File.join(File.dirname(__FILE__), 'puppet-debugserver', lib)) - end + require "puppet-debugserver/#{lib}" + rescue LoadError + require File.expand_path(File.join(File.dirname(__FILE__), 'puppet-debugserver', lib)) end ensure $VERBOSE = original_verbose diff --git a/lib/puppet_editor_services/protocol/json_rpc.rb b/lib/puppet_editor_services/protocol/json_rpc.rb index 4e8a5579..0b6de68b 100644 --- a/lib/puppet_editor_services/protocol/json_rpc.rb +++ b/lib/puppet_editor_services/protocol/json_rpc.rb @@ -26,7 +26,7 @@ class JsonRPC < ::PuppetEditorServices::Protocol::Base CODE_INTERNAL_ERROR = -32603 MSG_INTERNAL_ERROR = 'internal error' - PARSING_ERROR_RESPONSE = '{"jsonrpc":"2.0","id":null,"error":{' \ + PARSING_ERROR_RESPONSE = '{"jsonrpc":"2.0","id":null,"error":{' \ "\"code\":#{CODE_INVALID_JSON}," \ "\"message\":\"#{MSG_INVALID_JSON}\"}}" diff --git a/lib/puppet_editor_services/server/tcp.rb b/lib/puppet_editor_services/server/tcp.rb index cc3eee26..f5f8afd5 100644 --- a/lib/puppet_editor_services/server/tcp.rb +++ b/lib/puppet_editor_services/server/tcp.rb @@ -208,12 +208,10 @@ def io_review end end io_r[2].each do |io| - begin - (remove_connection(io) || self.class.services.delete(io)).close - rescue # rubocop:disable Style/RescueStandardError - # Swallow all errors - true - end + (remove_connection(io) || self.class.services.delete(io)).close + rescue # rubocop:disable Style/RescueStandardError + # Swallow all errors + true end end end @@ -274,12 +272,10 @@ def remove_connection_async(io) def stop_connections self.class.c_locker.synchronize do self.class.io_connection_dic.each_key do |io| - begin - io.close - rescue # rubocop:disable Style/RescueStandardError - # Swallow all errors - true - end + io.close + rescue # rubocop:disable Style/RescueStandardError + # Swallow all errors + true end self.class.io_connection_dic.clear end diff --git a/lib/puppet_languageserver.rb b/lib/puppet_languageserver.rb index 2a1cf1fd..65c8f2de 100644 --- a/lib/puppet_languageserver.rb +++ b/lib/puppet_languageserver.rb @@ -62,11 +62,9 @@ def self.require_gems(options) message_handler server_capabilities ].each do |lib| - begin - require "puppet-languageserver/#{lib}" - rescue LoadError - require File.expand_path(File.join(File.dirname(__FILE__), 'puppet-languageserver', lib)) - end + require "puppet-languageserver/#{lib}" + rescue LoadError + require File.expand_path(File.join(File.dirname(__FILE__), 'puppet-languageserver', lib)) end begin @@ -90,11 +88,9 @@ def self.require_gems(options) puppet_monkey_patches providers ].each do |lib| - begin - require "puppet-languageserver/#{lib}" - rescue LoadError - require File.expand_path(File.join(File.dirname(__FILE__), 'puppet-languageserver', lib)) - end + require "puppet-languageserver/#{lib}" + rescue LoadError + require File.expand_path(File.join(File.dirname(__FILE__), 'puppet-languageserver', lib)) end # Validate the feature flags diff --git a/lib/puppet_languageserver_sidecar.rb b/lib/puppet_languageserver_sidecar.rb index 507c493f..df84a9ac 100644 --- a/lib/puppet_languageserver_sidecar.rb +++ b/lib/puppet_languageserver_sidecar.rb @@ -12,11 +12,9 @@ %w[ sidecar_protocol ].each do |lib| - begin - require "puppet-languageserver/#{lib}" - rescue LoadError - require File.expand_path(File.join(File.dirname(__FILE__), 'puppet-languageserver', lib)) - end + require "puppet-languageserver/#{lib}" + rescue LoadError + require File.expand_path(File.join(File.dirname(__FILE__), 'puppet-languageserver', lib)) end ensure $VERBOSE = original_verbose @@ -76,11 +74,9 @@ def self.require_gems(options) ] require_list.each do |lib| - begin - require "puppet-languageserver-sidecar/#{lib}" - rescue LoadError - require File.expand_path(File.join(File.dirname(__FILE__), 'puppet-languageserver-sidecar', lib)) - end + require "puppet-languageserver-sidecar/#{lib}" + rescue LoadError + require File.expand_path(File.join(File.dirname(__FILE__), 'puppet-languageserver-sidecar', lib)) end ensure $VERBOSE = original_verbose @@ -213,11 +209,9 @@ def self.inject_workspace_as_module return false unless PuppetLanguageServerSidecar::Workspace.has_module_metadata? %w[puppet_modulepath_monkey_patches].each do |lib| - begin - require "puppet-languageserver-sidecar/#{lib}" - rescue LoadError - require File.expand_path(File.join(File.dirname(__FILE__), 'puppet-languageserver-sidecar', lib)) - end + require "puppet-languageserver-sidecar/#{lib}" + rescue LoadError + require File.expand_path(File.join(File.dirname(__FILE__), 'puppet-languageserver-sidecar', lib)) end log_message(:debug, 'Injected the workspace into the module loader') @@ -230,11 +224,9 @@ def self.inject_workspace_as_environment Puppet.settings[:environment] = PuppetLanguageServerSidecar::PuppetHelper::SIDECAR_PUPPET_ENVIRONMENT %w[puppet_environment_monkey_patches].each do |lib| - begin - require "puppet-languageserver-sidecar/#{lib}" - rescue LoadError - require File.expand_path(File.join(File.dirname(__FILE__), 'puppet-languageserver-sidecar', lib)) - end + require "puppet-languageserver-sidecar/#{lib}" + rescue LoadError + require File.expand_path(File.join(File.dirname(__FILE__), 'puppet-languageserver-sidecar', lib)) end log_message(:debug, 'Injected the workspace into the environment loader')