Skip to content

Commit

Permalink
[Fix rubocop#10731] Show tip for suggested extensions that are instal…
Browse files Browse the repository at this point in the history
…led but not loaded in .rubocop.yml
  • Loading branch information
nobuyo committed Jul 18, 2022
1 parent 997d66f commit d757b8f
Show file tree
Hide file tree
Showing 5 changed files with 453 additions and 318 deletions.
1 change: 1 addition & 0 deletions .rubocop_todo.yml
Expand Up @@ -29,6 +29,7 @@ Metrics/ModuleLength:
RSpec/AnyInstance:
Exclude:
- 'spec/rubocop/cli_spec.rb'
- 'spec/rubocop/cli/suggest_extensions_spec.rb'
- 'spec/rubocop/cop/lint/duplicate_methods_spec.rb'
- 'spec/rubocop/cop/team_spec.rb'
- 'spec/rubocop/target_finder_spec.rb'
Expand Down
1 change: 1 addition & 0 deletions changelog/change_show_tip_for_suggested_extensions_that.md
@@ -0,0 +1 @@
* [#10731](https://github.com/rubocop/rubocop/issues/10731): Show tip for suggested extensions that are installed but not loaded in .rubocop.yml. ([@nobuyo][])
68 changes: 53 additions & 15 deletions lib/rubocop/cli/command/suggest_extensions.rb
Expand Up @@ -17,20 +17,10 @@ class SuggestExtensions < Base
def run
return if skip? || extensions.none?

puts
puts 'Tip: Based on detected gems, the following ' \
'RuboCop extension libraries might be helpful:'

extensions.sort.each do |extension|
puts " * #{extension} (https://rubygems.org/gems/#{extension})"
end
print_install_suggestions if not_installed_extensions.any?
print_load_suggestions if installed_and_not_loaded_extensions.any?

puts
puts 'You can opt out of this message by adding the following to your config ' \
'(see https://docs.rubocop.org/rubocop/extensions.html#extension-suggestions ' \
'for more options):'
puts ' AllCops:'
puts ' SuggestExtensions: false'
print_opt_out_instruction

puts if @options[:display_time]
end
Expand All @@ -48,15 +38,63 @@ def skip?
!INCLUDED_FORMATTERS.include?(current_formatter)
end

def print_install_suggestions
puts
puts 'Tip: Based on detected gems, the following ' \
'RuboCop extension libraries might be helpful:'

not_installed_extensions.sort.each do |extension|
puts " * #{extension} (https://rubygems.org/gems/#{extension})"
end
end

def print_load_suggestions
puts
puts 'The following RuboCop extension libraries are installed but not loaded in config:'

installed_and_not_loaded_extensions.sort.each do |extension|
puts " * #{extension}"
end
end

def print_opt_out_instruction
puts
puts 'You can opt out of this message by adding the following to your config ' \
'(see https://docs.rubocop.org/rubocop/extensions.html#extension-suggestions ' \
'for more options):'
puts ' AllCops:'
puts ' SuggestExtensions: false'
end

def current_formatter
@options[:format] || @config_store.for_pwd.for_all_cops['DefaultFormatter'] || 'p'
end

def extensions
def all_extensions
return [] unless lockfile.dependencies.any?

extensions = @config_store.for_pwd.for_all_cops['SuggestExtensions'] || {}
extensions.select { |_, v| (Array(v) & dependent_gems).any? }.keys - installed_gems
extensions.select { |_, v| (Array(v) & dependent_gems).any? }.keys
end

def extensions
not_installed_extensions + installed_and_not_loaded_extensions
end

def installed_extensions
all_extensions & installed_gems
end

def not_installed_extensions
all_extensions - installed_gems
end

def loaded_extensions
@config_store.for_pwd.loaded_features.to_a
end

def installed_and_not_loaded_extensions
installed_extensions - loaded_extensions
end

def lockfile
Expand Down

0 comments on commit d757b8f

Please sign in to comment.