Sha256: b4957a7bbdfb5c64a90bbb03184f4bf41b03b354120c6cee6c4c49e84d4f50fc

Contents?: true

Size: 1.7 KB

Versions: 34

Compression:

Stored size: 1.7 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Style
      # This cop enforces the use of `$stdout/$stderr/$stdin` instead of `STDOUT/STDERR/STDIN`.
      # `STDOUT/STDERR/STDIN` are constants, and while you can actually
      # reassign (possibly to redirect some stream) constants in Ruby, you'll get
      # an interpreter warning if you do so.
      #
      # @example
      #   # bad
      #   STDOUT.puts('hello')
      #
      #   hash = { out: STDOUT, key: value }
      #
      #   def m(out = STDOUT)
      #     out.puts('hello')
      #   end
      #
      #   # good
      #   $stdout.puts('hello')
      #
      #   hash = { out: $stdout, key: value }
      #
      #   def m(out = $stdout)
      #     out.puts('hello')
      #   end
      #
      class GlobalStdStream < Base
        extend AutoCorrector

        MSG = 'Use `%<gvar_name>s` instead of `%<const_name>s`.'

        STD_STREAMS = %i[STDIN STDOUT STDERR].to_set.freeze

        def_node_matcher :const_to_gvar_assignment?, <<~PATTERN
          (gvasgn %1 (const nil? _))
        PATTERN

        def on_const(node)
          const_name = node.children[1]
          return unless STD_STREAMS.include?(const_name)

          gvar_name = gvar_name(const_name).to_sym
          return if const_to_gvar_assignment?(node.parent, gvar_name)

          add_offense(node, message: message(const_name)) do |corrector|
            corrector.replace(node, gvar_name)
          end
        end

        private

        def message(const_name)
          format(MSG, gvar_name: gvar_name(const_name), const_name: const_name)
        end

        def gvar_name(const_name)
          "$#{const_name.to_s.downcase}"
        end
      end
    end
  end
end

Version data entries

34 entries across 34 versions & 3 rubygems

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