Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Fix #10731] Show tip for suggested extensions that are installed but not loaded in .rubocop.yml #10800

Merged
merged 1 commit into from Aug 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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