class RuboCop::Cop::Style::DisableCopsWithinSourceCodeDirective

Detects comments to enable/disable RuboCop. This is useful if want to make sure that every RuboCop error gets fixed and not quickly disabled with a comment.

Specific cops can be allowed with the ‘AllowedCops` configuration. Note that if this configuration is set, `rubocop:disable all` is still disallowed.

@example

# bad
# rubocop:disable Metrics/AbcSize
def foo
end
# rubocop:enable Metrics/AbcSize

# good
def foo
end

@example AllowedCops: [Metrics/AbcSize]

# good
# rubocop:disable Metrics/AbcSize
def foo
end
# rubocop:enable Metrics/AbcSize

Constants

MSG

rubocop:enable Lint/RedundantCopDisableDirective

MSG_FOR_COPS

Public Instance Methods

on_new_investigation() click to toggle source
# File lib/rubocop/cop/style/disable_cops_within_source_code_directive.rb, line 40
def on_new_investigation
  processed_source.comments.each do |comment|
    directive_cops = directive_cops(comment)
    disallowed_cops = directive_cops - allowed_cops

    next unless disallowed_cops.any?

    register_offense(comment, directive_cops, disallowed_cops)
  end
end

Private Instance Methods

allowed_cops() click to toggle source
# File lib/rubocop/cop/style/disable_cops_within_source_code_directive.rb, line 77
def allowed_cops
  Array(cop_config['AllowedCops'])
end
any_cops_allowed?() click to toggle source
# File lib/rubocop/cop/style/disable_cops_within_source_code_directive.rb, line 81
def any_cops_allowed?
  allowed_cops.any?
end
directive_cops(comment) click to toggle source
# File lib/rubocop/cop/style/disable_cops_within_source_code_directive.rb, line 72
def directive_cops(comment)
  match_captures = DirectiveComment.new(comment).match_captures
  match_captures && match_captures[1] ? match_captures[1].split(',').map(&:strip) : []
end
register_offense(comment, directive_cops, disallowed_cops) click to toggle source
# File lib/rubocop/cop/style/disable_cops_within_source_code_directive.rb, line 53
def register_offense(comment, directive_cops, disallowed_cops)
  message = if any_cops_allowed?
              format(MSG_FOR_COPS, cops: "`#{disallowed_cops.join('`, `')}`")
            else
              MSG
            end

  add_offense(comment, message: message) do |corrector|
    replacement = ''

    if directive_cops.length != disallowed_cops.length
      replacement = comment.text.sub(/#{Regexp.union(disallowed_cops)},?\s*/, '')
                           .sub(/,\s*$/, '')
    end

    corrector.replace(comment, replacement)
  end
end