class RuboCop::Cop::Migration::DepartmentName

Check that cop names in rubocop:disable comments are given with department name.

Constants

DISABLE_COMMENT_FORMAT
DISABLING_COPS_CONTENT_TOKEN

The token that makes up a disable comment. The allowed specification for comments after ‘# rubocop: disable` is `DepartmentName/CopName` or` all`.

MSG

Public Instance Methods

on_new_investigation() click to toggle source
# File lib/rubocop/cop/migration/department_name.rb, line 21
def on_new_investigation
  processed_source.comments.each do |comment|
    next if comment.text !~ DISABLE_COMMENT_FORMAT

    offset = Regexp.last_match(1).length

    Regexp.last_match(4).scan(/[^,]+|\W+/) do |name|
      trimmed_name = name.strip

      unless valid_content_token?(trimmed_name)
        check_cop_name(trimmed_name, comment, offset)
      end

      break if contain_unexpected_character_for_department_name?(name)

      offset += name.length
    end
  end
end

Private Instance Methods

check_cop_name(name, comment, offset) click to toggle source
# File lib/rubocop/cop/migration/department_name.rb, line 47
def check_cop_name(name, comment, offset)
  start = comment.location.expression.begin_pos + offset
  range = range_between(start, start + name.length)

  add_offense(range) do |corrector|
    cop_name = range.source
    qualified_cop_name = Registry.global.qualified_cop_name(cop_name, nil, warn: false)

    unless qualified_cop_name.include?('/')
      qualified_cop_name = qualified_legacy_cop_name(cop_name)
    end

    corrector.replace(range, qualified_cop_name)
  end
end
contain_unexpected_character_for_department_name?(name) click to toggle source
# File lib/rubocop/cop/migration/department_name.rb, line 69
def contain_unexpected_character_for_department_name?(name)
  name.match?(%r{[^A-z/, ]})
end
disable_comment_offset() click to toggle source
# File lib/rubocop/cop/migration/department_name.rb, line 43
def disable_comment_offset
  Regexp.last_match(1).length
end
qualified_legacy_cop_name(cop_name) click to toggle source
# File lib/rubocop/cop/migration/department_name.rb, line 73
def qualified_legacy_cop_name(cop_name)
  legacy_cop_names = RuboCop::ConfigObsoletion.legacy_cop_names

  legacy_cop_names.detect { |legacy_cop_name| legacy_cop_name.split('/')[1] == cop_name }
end
valid_content_token?(content_token) click to toggle source
# File lib/rubocop/cop/migration/department_name.rb, line 63
def valid_content_token?(content_token)
  /\W+/.match?(content_token) ||
    DISABLING_COPS_CONTENT_TOKEN.match?(content_token) ||
    Registry.global.department?(content_token)
end