Sha256: dc4405661d7b73510965f201d10deeeb3e862223446b8c291b26a9c4f597fb2a

Contents?: true

Size: 1.33 KB

Versions: 182

Compression:

Stored size: 1.33 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 >= '3.0'
      #       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 < Base
        include GemspecHelp

        MSG = 'Do not use `RUBY_VERSION` in gemspec file.'

        # @!method ruby_version?(node)
        def_node_matcher :ruby_version?, '(const {cbase nil?} :RUBY_VERSION)'

        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

182 entries across 175 versions & 17 rubygems

Version Path
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/rubocop-1.64.1/lib/rubocop/cop/gemspec/ruby_version_globals_usage.rb
cm-admin-1.5.22 vendor/bundle/ruby/3.3.0/gems/rubocop-1.35.1/lib/rubocop/cop/gemspec/ruby_version_globals_usage.rb
cm-admin-1.5.21 vendor/bundle/ruby/3.3.0/gems/rubocop-1.35.1/lib/rubocop/cop/gemspec/ruby_version_globals_usage.rb
cm-admin-1.5.20 vendor/bundle/ruby/3.3.0/gems/rubocop-1.35.1/lib/rubocop/cop/gemspec/ruby_version_globals_usage.rb
katalyst-govuk-formbuilder-1.9.2 vendor/bundle/ruby/3.3.0/gems/rubocop-1.64.1/lib/rubocop/cop/gemspec/ruby_version_globals_usage.rb
rubocop-1.64.1 lib/rubocop/cop/gemspec/ruby_version_globals_usage.rb
rubocop-1.63.4 lib/rubocop/cop/gemspec/ruby_version_globals_usage.rb
rubocop-1.63.3 lib/rubocop/cop/gemspec/ruby_version_globals_usage.rb
rubocop-1.63.2 lib/rubocop/cop/gemspec/ruby_version_globals_usage.rb
harbr-2.8.1 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/gemspec/ruby_version_globals_usage.rb
rubocop-1.63.1 lib/rubocop/cop/gemspec/ruby_version_globals_usage.rb
rubocop-1.63.0 lib/rubocop/cop/gemspec/ruby_version_globals_usage.rb
bison-0.1.0 vendor/bundle/ruby/3.2.0/gems/rubocop-1.62.1/lib/rubocop/cop/gemspec/ruby_version_globals_usage.rb
rubocop-1.62.1 lib/rubocop/cop/gemspec/ruby_version_globals_usage.rb
rubocop-1.62.0 lib/rubocop/cop/gemspec/ruby_version_globals_usage.rb
rubocop-1.61.0 lib/rubocop/cop/gemspec/ruby_version_globals_usage.rb
mlh-rubocop-config-1.0.3 vendor/bundle/ruby/3.2.0/gems/rubocop-1.60.2/lib/rubocop/cop/gemspec/ruby_version_globals_usage.rb
rubocop-1.60.2 lib/rubocop/cop/gemspec/ruby_version_globals_usage.rb
study_line-0.2.7 vendor/bundle/ruby/3.2.0/gems/rubocop-1.60.0/lib/rubocop/cop/gemspec/ruby_version_globals_usage.rb
study_line-0.2.6 vendor/bundle/ruby/3.2.0/gems/rubocop-1.60.0/lib/rubocop/cop/gemspec/ruby_version_globals_usage.rb