Sha256: 15dcf48380f3d770445922a661e33445fa273cc67713d87bd631e4978ba575a5

Contents?: true

Size: 1.24 KB

Versions: 7

Compression:

Stored size: 1.24 KB

Contents

# frozen_string_literal: true

# rubocop:disable Lint/RedundantCopDisableDirective

module RuboCop
  module Cop
    module Style
      # 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.
      #
      # @example
      #   # bad
      #   # rubocop:disable Metrics/AbcSize
      #   def f
      #   end
      #   # rubocop:enable Metrics/AbcSize
      #
      #   # good
      #   def fixed_method_name_and_no_rubocop_comments
      #   end
      #
      class DisableCopsWithinSourceCodeDirective < Cop
        # rubocop:enable Lint/RedundantCopDisableDirective
        MSG = 'Comment to disable/enable RuboCop.'

        def investigate(processed_source)
          processed_source.comments.each do |comment|
            next unless rubocop_directive_comment?(comment)

            add_offense(comment)
          end
        end

        def autocorrect(comment)
          lambda do |corrector|
            corrector.replace(comment, '')
          end
        end

        private

        def rubocop_directive_comment?(comment)
          CommentConfig::COMMENT_DIRECTIVE_REGEXP.match?(comment.text)
        end
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 2 rubygems

Version Path
rubocop-0.89.0 lib/rubocop/cop/style/disable_cops_within_source_code_directive.rb
rubocop-0.88.0 lib/rubocop/cop/style/disable_cops_within_source_code_directive.rb
rbhint-0.87.1.rc1 lib/rubocop/cop/style/disable_cops_within_source_code_directive.rb
rubocop-0.87.1 lib/rubocop/cop/style/disable_cops_within_source_code_directive.rb
rubocop-0.87.0 lib/rubocop/cop/style/disable_cops_within_source_code_directive.rb
rubocop-0.86.0 lib/rubocop/cop/style/disable_cops_within_source_code_directive.rb
rbhint-0.85.1.rc2 lib/rubocop/cop/style/disable_cops_within_source_code_directive.rb