Skip to content

Commit

Permalink
[Fix rubocop#9145] Fix issues with SuggestExtensions when bundler is …
Browse files Browse the repository at this point in the history
…not available, or when there is no gemfile.
  • Loading branch information
dvandersluis committed Dec 2, 2020
1 parent 0849d36 commit 1e244c1
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 1 deletion.
1 change: 1 addition & 0 deletions changelog/fix_fix_issues_with_suggestextensions_when.md
@@ -0,0 +1 @@
* [#9145](https://github.com/rubocop-hq/rubocop/issues/9145): Fix issues with SuggestExtensions when bundler is not available, or when there is no gemfile. ([@dvandersluis][])
8 changes: 7 additions & 1 deletion lib/rubocop/cli/command/suggest_extensions.rb
Expand Up @@ -12,8 +12,12 @@ class SuggestExtensions < Base
self.command_name = :suggest_extensions

def self.dependent_gems
return [] unless defined?(Bundler)

# This only includes gems in Gemfile, not in lockfile
Bundler.load.dependencies.map(&:name)
rescue Bundler::GemfileNotFound
[]
end

def run
Expand Down Expand Up @@ -54,6 +58,8 @@ def current_formatter
end

def extensions
return [] unless dependent_gems.any?

@extensions ||= begin
extensions = @config_store.for_pwd.for_all_cops['SuggestExtensions'] || {}
extensions.select { |_, v| (Array(v) & dependent_gems).any? }.keys - dependent_gems
Expand All @@ -66,7 +72,7 @@ def puts(*args)
end

def dependent_gems
self.class.dependent_gems
@dependent_gems ||= self.class.dependent_gems
end
end
end
Expand Down

0 comments on commit 1e244c1

Please sign in to comment.