Sha256: 1b87376a10d5c1f050840af8490e3208352f1cec648d7247d6fdf05054311f97

Contents?: true

Size: 1.41 KB

Versions: 42

Compression:

Stored size: 1.41 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Style
      # This cop identifies places where `$stderr.puts` can be replaced by
      # `warn`. The latter has the advantage of easily being disabled by,
      # the `-W0` interpreter flag or setting `$VERBOSE` to `nil`.
      #
      # @example
      #   # bad
      #   $stderr.puts('hello')
      #
      #   # good
      #   warn('hello')
      #
      class StderrPuts < Cop
        include RangeHelp

        MSG =
          'Use `warn` instead of `%<bad>s` to allow such output to be disabled.'

        def_node_matcher :stderr_puts?, <<~PATTERN
          (send
            {(gvar #stderr_gvar?) (const nil? :STDERR)}
            :puts $_
            ...)
        PATTERN

        def on_send(node)
          return unless stderr_puts?(node)

          add_offense(node, location: stderr_puts_range(node))
        end

        def autocorrect(node)
          lambda do |corrector|
            corrector.replace(stderr_puts_range(node), 'warn')
          end
        end

        private

        def message(node)
          format(MSG, bad: "#{node.receiver.source}.#{node.method_name}")
        end

        def stderr_gvar?(sym)
          sym == :$stderr
        end

        def stderr_puts_range(send)
          range_between(
            send.loc.expression.begin_pos,
            send.loc.selector.end_pos
          )
        end
      end
    end
  end
end

Version data entries

42 entries across 31 versions & 5 rubygems

Version Path
grape-extra_validators-2.0.0 vendor/bundle/ruby/2.6.0/gems/rubocop-0.79.0/lib/rubocop/cop/style/stderr_puts.rb
rubocop-0.86.0 lib/rubocop/cop/style/stderr_puts.rb
files.com-1.0.1 vendor/bundle/ruby/2.5.0/gems/rubocop-0.85.1/lib/rubocop/cop/style/stderr_puts.rb
rbhint-0.85.1.rc2 lib/rubocop/cop/style/stderr_puts.rb
rbhint-0.85.1.rc1 lib/rubocop/cop/style/stderr_puts.rb
rubocop-0.85.1 lib/rubocop/cop/style/stderr_puts.rb
rbhint-0.8.5.rc1 lib/rubocop/cop/style/stderr_puts.rb
rubocop-0.85.0 lib/rubocop/cop/style/stderr_puts.rb
rubocop-0.84.0 lib/rubocop/cop/style/stderr_puts.rb
rubocop-0.83.0 lib/rubocop/cop/style/stderr_puts.rb
rubocop-0.82.0 lib/rubocop/cop/style/stderr_puts.rb
rubocop-0.81.0 lib/rubocop/cop/style/stderr_puts.rb
rubocop-0.80.1 lib/rubocop/cop/style/stderr_puts.rb
rubocop-0.80.0 lib/rubocop/cop/style/stderr_puts.rb
grape-extra_validators-1.0.0 vendor/bundle/ruby/2.6.0/gems/rubocop-0.79.0/lib/rubocop/cop/style/stderr_puts.rb
grape-extra_validators-1.0.0 vendor/bundle/ruby/2.4.0/gems/rubocop-0.79.0/lib/rubocop/cop/style/stderr_puts.rb
rubocop-0.79.0 lib/rubocop/cop/style/stderr_puts.rb
rubocop-0.78.0 lib/rubocop/cop/style/stderr_puts.rb
zuora_connect_ui-0.10.0 vendor/ruby/2.6.0/gems/rubocop-0.77.0/lib/rubocop/cop/style/stderr_puts.rb
zuora_connect_ui-0.10.0 vendor/ruby/2.6.0/gems/rubocop-0.75.1/lib/rubocop/cop/style/stderr_puts.rb