Sha256: 26796dbf03bf49657e1c482793432327e8e0e584f3c521a562736c84d8a38f91

Contents?: true

Size: 1.46 KB

Versions: 30

Compression:

Stored size: 1.46 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 < Base
        include RangeHelp
        extend AutoCorrector

        MSG =
          'Use `warn` instead of `%<bad>s` to allow such output to be disabled.'
        RESTRICT_ON_SEND = %i[puts].freeze

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

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

          message = message(node)
          add_offense(stderr_puts_range(node), message: message) 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

30 entries across 30 versions & 2 rubygems

Version Path
plaid-14.13.0 vendor/bundle/ruby/3.0.0/gems/rubocop-0.91.1/lib/rubocop/cop/style/stderr_puts.rb
plaid-14.12.1 vendor/bundle/ruby/3.0.0/gems/rubocop-0.91.1/lib/rubocop/cop/style/stderr_puts.rb
plaid-14.12.0 vendor/bundle/ruby/2.6.0/gems/rubocop-0.91.1/lib/rubocop/cop/style/stderr_puts.rb
plaid-14.11.1 vendor/bundle/ruby/2.6.0/gems/rubocop-0.91.1/lib/rubocop/cop/style/stderr_puts.rb
plaid-14.10.0 vendor/bundle/ruby/2.6.0/gems/rubocop-0.91.1/lib/rubocop/cop/style/stderr_puts.rb
plaid-14.7.0 vendor/bundle/ruby/2.6.0/gems/rubocop-0.91.1/lib/rubocop/cop/style/stderr_puts.rb
rubocop-1.10.0 lib/rubocop/cop/style/stderr_puts.rb
rubocop-1.9.1 lib/rubocop/cop/style/stderr_puts.rb
rubocop-1.9.0 lib/rubocop/cop/style/stderr_puts.rb
rubocop-1.8.1 lib/rubocop/cop/style/stderr_puts.rb
rubocop-1.8.0 lib/rubocop/cop/style/stderr_puts.rb
rubocop-1.7.0 lib/rubocop/cop/style/stderr_puts.rb
rubocop-1.6.1 lib/rubocop/cop/style/stderr_puts.rb
rubocop-1.6.0 lib/rubocop/cop/style/stderr_puts.rb
rubocop-1.5.2 lib/rubocop/cop/style/stderr_puts.rb
rubocop-1.5.1 lib/rubocop/cop/style/stderr_puts.rb
rubocop-1.5.0 lib/rubocop/cop/style/stderr_puts.rb
rubocop-1.4.2 lib/rubocop/cop/style/stderr_puts.rb
rubocop-1.4.1 lib/rubocop/cop/style/stderr_puts.rb
rubocop-1.4.0 lib/rubocop/cop/style/stderr_puts.rb