Sha256: e6b7d8ee405be4ff8801d4aa5e09e58c3dd1b27e62b819d81338413052e2e91e

Contents?: true

Size: 1.63 KB

Versions: 9

Compression:

Stored size: 1.63 KB

Contents

# frozen_string_literal: true

# rubocop:disable Lint/UnneededCopDisableDirective
# 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/UnneededCopDisableDirective, 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.loc.expression,
                              comment.text[/#{prefix} \S+/])
          end
        end
      end
    end
  end
end

Version data entries

9 entries across 6 versions & 2 rubygems

Version Path
zuora_connect_ui-0.10.0 vendor/ruby/2.6.0/gems/rubocop-0.75.1/lib/rubocop/cop/style/double_cop_disable_directive.rb
zuora_connect_ui-0.10.0 vendor/ruby/2.6.0/gems/rubocop-0.75.0/lib/rubocop/cop/style/double_cop_disable_directive.rb
zuora_connect_ui-0.9.2 vendor/ruby/2.6.0/gems/rubocop-0.75.0/lib/rubocop/cop/style/double_cop_disable_directive.rb
zuora_connect_ui-0.9.2 vendor/ruby/2.6.0/gems/rubocop-0.75.1/lib/rubocop/cop/style/double_cop_disable_directive.rb
zuora_connect_ui-0.9.1 vendor/ruby/2.6.0/gems/rubocop-0.75.1/lib/rubocop/cop/style/double_cop_disable_directive.rb
zuora_connect_ui-0.9.1 vendor/ruby/2.6.0/gems/rubocop-0.75.0/lib/rubocop/cop/style/double_cop_disable_directive.rb
rubocop-0.75.1 lib/rubocop/cop/style/double_cop_disable_directive.rb
zuora_connect_ui-0.9.0 vendor/ruby/2.6.0/gems/rubocop-0.75.0/lib/rubocop/cop/style/double_cop_disable_directive.rb
rubocop-0.75.0 lib/rubocop/cop/style/double_cop_disable_directive.rb