Sha256: d7dd69d431fb7badc298aeacf7ebd73e498b357673c097a188114b8d218e15ef

Contents?: true

Size: 1.62 KB

Versions: 15

Compression:

Stored size: 1.62 KB

Contents

# frozen_string_literal: true

# rubocop:disable Lint/RedundantCopDisableDirective
# rubocop:disable Style/DoubleCopDisableDirective

module RuboCop
  module Cop
    module Style
      # Detects double disable comments on one line. This is mostly to catch
      # automatically generated comments that need to be regenerated.
      #
      # @example
      #   # bad
      #   def f # rubocop:disable Style/For # rubocop:disable Metrics/AbcSize
      #   end
      #
      #   # good
      #   # rubocop:disable Metrics/AbcSize
      #   def f # rubocop:disable Style/For
      #   end
      #   # rubocop:enable Metrics/AbcSize
      #
      #   # if both fit on one line
      #   def f # rubocop:disable Style/For, Metrics/AbcSize
      #   end
      #
      class DoubleCopDisableDirective < Cop
        # rubocop:enable Style/For, Style/DoubleCopDisableDirective
        # rubocop:enable Lint/RedundantCopDisableDirective, Metrics/AbcSize
        MSG = 'More than one disable comment on one line.'

        def investigate(processed_source)
          processed_source.comments.each do |comment|
            next unless comment.text.scan(/# rubocop:(?:disable|todo)/).size > 1

            add_offense(comment)
          end
        end

        def autocorrect(comment)
          prefix = if comment.text.start_with?('# rubocop:disable')
                     '# rubocop:disable'
                   else
                     '# rubocop:todo'
                   end

          lambda do |corrector|
            corrector.replace(comment,
                              comment.text[/#{prefix} \S+/])
          end
        end
      end
    end
  end
end

Version data entries

15 entries across 15 versions & 3 rubygems

Version Path
rubocop-0.89.0 lib/rubocop/cop/style/double_cop_disable_directive.rb
rubocop-0.88.0 lib/rubocop/cop/style/double_cop_disable_directive.rb
rbhint-0.87.1.rc1 lib/rubocop/cop/style/double_cop_disable_directive.rb
rubocop-0.87.1 lib/rubocop/cop/style/double_cop_disable_directive.rb
rubocop-0.87.0 lib/rubocop/cop/style/double_cop_disable_directive.rb
rubocop-0.86.0 lib/rubocop/cop/style/double_cop_disable_directive.rb
files.com-1.0.1 vendor/bundle/ruby/2.5.0/gems/rubocop-0.85.1/lib/rubocop/cop/style/double_cop_disable_directive.rb
rbhint-0.85.1.rc2 lib/rubocop/cop/style/double_cop_disable_directive.rb
rbhint-0.85.1.rc1 lib/rubocop/cop/style/double_cop_disable_directive.rb
rubocop-0.85.1 lib/rubocop/cop/style/double_cop_disable_directive.rb
rbhint-0.8.5.rc1 lib/rubocop/cop/style/double_cop_disable_directive.rb
rubocop-0.85.0 lib/rubocop/cop/style/double_cop_disable_directive.rb
rubocop-0.84.0 lib/rubocop/cop/style/double_cop_disable_directive.rb
rubocop-0.83.0 lib/rubocop/cop/style/double_cop_disable_directive.rb
rubocop-0.82.0 lib/rubocop/cop/style/double_cop_disable_directive.rb