Sha256: bc2dada1aeca9209a238402bbe4ecb90bf407472af70ad60851cea10d9f3c52c

Contents?: true

Size: 1.46 KB

Versions: 42

Compression:

Stored size: 1.46 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Gemspec
      # Checks that `RUBY_VERSION` constant is not used in gemspec.
      # Using `RUBY_VERSION` is dangerous because value of the
      # constant is determined by `rake release`.
      # It's possible to have dependency based on ruby version used
      # to execute `rake release` and not user's ruby version.
      #
      # @example
      #
      #   # bad
      #   Gem::Specification.new do |spec|
      #     if RUBY_VERSION >= '2.5'
      #       spec.add_runtime_dependency 'gem_a'
      #     else
      #       spec.add_runtime_dependency 'gem_b'
      #     end
      #   end
      #
      #   # good
      #   Gem::Specification.new do |spec|
      #     spec.add_runtime_dependency 'gem_a'
      #   end
      #
      class RubyVersionGlobalsUsage < Cop
        MSG = 'Do not use `RUBY_VERSION` in gemspec file.'

        def_node_matcher :ruby_version?, '(const nil? :RUBY_VERSION)'

        def_node_search :gem_specification?, <<~PATTERN
          (block
            (send
              (const
                (const {cbase nil?} :Gem) :Specification) :new)
            ...)
        PATTERN

        def on_const(node)
          return unless gem_spec_with_ruby_version?(node)

          add_offense(node)
        end

        private

        def gem_spec_with_ruby_version?(node)
          gem_specification?(processed_source.ast) && ruby_version?(node)
        end
      end
    end
  end
end

Version data entries

42 entries across 31 versions & 5 rubygems

Version Path
zuora_connect_ui-0.10.0 vendor/ruby/2.6.0/gems/rubocop-0.75.0/lib/rubocop/cop/gemspec/ruby_version_globals_usage.rb
zuora_connect_ui-0.10.0 vendor/ruby/2.6.0/gems/rubocop-0.75.1/lib/rubocop/cop/gemspec/ruby_version_globals_usage.rb
zuora_connect_ui-0.10.0 vendor/ruby/2.6.0/gems/rubocop-0.76.0/lib/rubocop/cop/gemspec/ruby_version_globals_usage.rb
rubocop-0.77.0 lib/rubocop/cop/gemspec/ruby_version_globals_usage.rb
zuora_connect_ui-0.9.2 vendor/ruby/2.6.0/gems/rubocop-0.74.0/lib/rubocop/cop/gemspec/ruby_version_globals_usage.rb
zuora_connect_ui-0.9.2 vendor/ruby/2.6.0/gems/rubocop-0.76.0/lib/rubocop/cop/gemspec/ruby_version_globals_usage.rb
zuora_connect_ui-0.9.2 vendor/ruby/2.6.0/gems/rubocop-0.75.1/lib/rubocop/cop/gemspec/ruby_version_globals_usage.rb
zuora_connect_ui-0.9.2 vendor/ruby/2.6.0/gems/rubocop-0.75.0/lib/rubocop/cop/gemspec/ruby_version_globals_usage.rb
rubocop-0.76.0 lib/rubocop/cop/gemspec/ruby_version_globals_usage.rb
zuora_connect_ui-0.9.1 vendor/ruby/2.6.0/gems/rubocop-0.75.0/lib/rubocop/cop/gemspec/ruby_version_globals_usage.rb
zuora_connect_ui-0.9.1 vendor/ruby/2.6.0/gems/rubocop-0.74.0/lib/rubocop/cop/gemspec/ruby_version_globals_usage.rb
zuora_connect_ui-0.9.1 vendor/ruby/2.6.0/gems/rubocop-0.75.1/lib/rubocop/cop/gemspec/ruby_version_globals_usage.rb
rubocop-0.75.1 lib/rubocop/cop/gemspec/ruby_version_globals_usage.rb
zuora_connect_ui-0.9.0 vendor/ruby/2.6.0/gems/rubocop-0.74.0/lib/rubocop/cop/gemspec/ruby_version_globals_usage.rb
zuora_connect_ui-0.9.0 vendor/ruby/2.6.0/gems/rubocop-0.75.0/lib/rubocop/cop/gemspec/ruby_version_globals_usage.rb
rubocop-0.75.0 lib/rubocop/cop/gemspec/ruby_version_globals_usage.rb
zuora_connect_ui-0.8.3 vendor/ruby/2.6.0/gems/rubocop-0.74.0/lib/rubocop/cop/gemspec/ruby_version_globals_usage.rb
zuora_connect_ui-0.8.2 vendor/ruby/2.6.0/gems/rubocop-0.74.0/lib/rubocop/cop/gemspec/ruby_version_globals_usage.rb
zuora_connect_ui-0.8.1 vendor/ruby/2.6.0/gems/rubocop-0.74.0/lib/rubocop/cop/gemspec/ruby_version_globals_usage.rb
zuora_connect_ui-0.8.0 vendor/ruby/2.6.0/gems/rubocop-0.74.0/lib/rubocop/cop/gemspec/ruby_version_globals_usage.rb