Skip to content

Commit

Permalink
(maint) - Rubocop Fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
jordanbreen28 committed Sep 21, 2023
1 parent dbcfbe3 commit 9f65a88
Show file tree
Hide file tree
Showing 18 changed files with 104 additions and 117 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/nightly.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
1 change: 1 addition & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
inherit_from: .rubocop_todo.yml

AllCops:
TargetRubyVersion: 2.7
Include:
- 'lib/**/*.rb'
- 'puppet-languageserver'
Expand Down
23 changes: 22 additions & 1 deletion .rubocop_todo.yml
Original file line number Diff line number Diff line change
@@ -1 +1,22 @@
# Empty
# 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'
6 changes: 3 additions & 3 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 3 additions & 5 deletions lib/dsp/dsp.rb
Original file line number Diff line number Diff line change
@@ -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
8 changes: 3 additions & 5 deletions lib/lsp/lsp.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
10 changes: 4 additions & 6 deletions lib/puppet-debugserver/hooks.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 4 additions & 6 deletions lib/puppet-languageserver/global_queues/single_instance_queue.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
50 changes: 23 additions & 27 deletions lib/puppet-languageserver/manifest/validation_provider.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
8 changes: 3 additions & 5 deletions lib/puppet-languageserver/providers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
12 changes: 5 additions & 7 deletions lib/puppet-languageserver/puppet_monkey_patches.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 1 addition & 2 deletions lib/puppet-languageserver/server_capabilities.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
8 changes: 3 additions & 5 deletions lib/puppet_debugserver.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/puppet_editor_services/protocol/json_rpc.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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}\"}}"

Expand Down
20 changes: 8 additions & 12 deletions lib/puppet_editor_services/server/tcp.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
16 changes: 6 additions & 10 deletions lib/puppet_languageserver.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
32 changes: 12 additions & 20 deletions lib/puppet_languageserver_sidecar.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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')
Expand All @@ -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')
Expand Down

0 comments on commit 9f65a88

Please sign in to comment.