Sha256: adbec5bf620f5fad9dce5decb0e0bd33f7f564a1faaf066b4d462ebbda11cea8

Contents?: true

Size: 1.33 KB

Versions: 15

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 >= '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 < 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

15 entries across 15 versions & 4 rubygems

Version Path
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/rubocop-1.26.0/lib/rubocop/cop/gemspec/ruby_version_globals_usage.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/rubocop-1.26.0/lib/rubocop/cop/gemspec/ruby_version_globals_usage.rb
rubocop-1.28.2 lib/rubocop/cop/gemspec/ruby_version_globals_usage.rb
rubocop-1.28.1 lib/rubocop/cop/gemspec/ruby_version_globals_usage.rb
rubocop-1.28.0 lib/rubocop/cop/gemspec/ruby_version_globals_usage.rb
rubocop-1.27.0 lib/rubocop/cop/gemspec/ruby_version_globals_usage.rb
rubocop-1.26.1 lib/rubocop/cop/gemspec/ruby_version_globals_usage.rb
op_connect-0.1.2 vendor/bundle/ruby/3.1.0/gems/rubocop-1.26.0/lib/rubocop/cop/gemspec/ruby_version_globals_usage.rb
rubocop-1.26.0 lib/rubocop/cop/gemspec/ruby_version_globals_usage.rb
rubocop-1.25.1 lib/rubocop/cop/gemspec/ruby_version_globals_usage.rb
rubocop-1.25.0 lib/rubocop/cop/gemspec/ruby_version_globals_usage.rb
phillipug-foodie-0.1.0 .vendor/ruby/3.0.0/gems/rubocop-1.24.0/lib/rubocop/cop/gemspec/ruby_version_globals_usage.rb
rubocop-1.24.1 lib/rubocop/cop/gemspec/ruby_version_globals_usage.rb
rubocop-1.24.0 lib/rubocop/cop/gemspec/ruby_version_globals_usage.rb
rubocop-1.23.0 lib/rubocop/cop/gemspec/ruby_version_globals_usage.rb