class RuboCop::Cop::Bundler::GemFilename

Verifies that a project contains Gemfile or gems.rb file and correct associated lock file based on the configuration.

@example EnforcedStyle: Gemfile (default)

# bad
Project contains gems.rb and gems.locked files

# bad
Project contains Gemfile and gems.locked file

# good
Project contains Gemfile and Gemfile.lock

@example EnforcedStyle: gems.rb

# bad
Project contains Gemfile and Gemfile.lock files

# bad
Project contains gems.rb and Gemfile.lock file

# good
Project contains gems.rb and gems.locked files

Constants

GEMFILE_FILES
GEMS_RB_FILES
MSG_GEMFILE_MISMATCHED
MSG_GEMFILE_REQUIRED
MSG_GEMS_RB_MISMATCHED
MSG_GEMS_RB_REQUIRED

Public Instance Methods

on_new_investigation() click to toggle source
# File lib/rubocop/cop/bundler/gem_filename.rb, line 43
def on_new_investigation
  file_path = processed_source.file_path
  basename = File.basename(file_path)
  return if expected_gemfile?(basename)

  register_offense(file_path, basename)
end

Private Instance Methods

expected_gemfile?(basename) click to toggle source
# File lib/rubocop/cop/bundler/gem_filename.rb, line 88
def expected_gemfile?(basename)
  (gemfile_required? && GEMFILE_FILES.include?(basename)) ||
    (gems_rb_required? && GEMS_RB_FILES.include?(basename))
end
gemfile_offense?(basename) click to toggle source
# File lib/rubocop/cop/bundler/gem_filename.rb, line 80
def gemfile_offense?(basename)
  gemfile_required? && GEMS_RB_FILES.include?(basename)
end
gemfile_required?() click to toggle source
# File lib/rubocop/cop/bundler/gem_filename.rb, line 93
def gemfile_required?
  style == :Gemfile
end
gems_rb_offense?(basename) click to toggle source
# File lib/rubocop/cop/bundler/gem_filename.rb, line 84
def gems_rb_offense?(basename)
  gems_rb_required? && GEMFILE_FILES.include?(basename)
end
gems_rb_required?() click to toggle source
# File lib/rubocop/cop/bundler/gem_filename.rb, line 97
def gems_rb_required?
  style == :'gems.rb'
end
register_gemfile_offense(file_path, basename) click to toggle source
# File lib/rubocop/cop/bundler/gem_filename.rb, line 58
def register_gemfile_offense(file_path, basename)
  message = case basename
            when 'gems.rb'
              MSG_GEMFILE_REQUIRED
            when 'gems.locked'
              MSG_GEMFILE_MISMATCHED
            end

  add_global_offense(format(message, file_path: file_path))
end
register_gems_rb_offense(file_path, basename) click to toggle source
# File lib/rubocop/cop/bundler/gem_filename.rb, line 69
def register_gems_rb_offense(file_path, basename)
  message = case basename
            when 'Gemfile'
              MSG_GEMS_RB_REQUIRED
            when 'Gemfile.lock'
              MSG_GEMS_RB_MISMATCHED
            end

  add_global_offense(format(message, file_path: file_path))
end
register_offense(file_path, basename) click to toggle source
# File lib/rubocop/cop/bundler/gem_filename.rb, line 53
def register_offense(file_path, basename)
  register_gemfile_offense(file_path, basename) if gemfile_offense?(basename)
  register_gems_rb_offense(file_path, basename) if gems_rb_offense?(basename)
end