Sha256: a1db109f3e0ca854ef56f153ffdd6be55a618ede23f62de4a0a09cb9df4e6ffa

Contents?: true

Size: 1.52 KB

Versions: 1

Compression:

Stored size: 1.52 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Gemspec
      # This cop checks that `date =` is not used in gemspec file.
      # It is set automatically when the gem is packaged.
      #
      # @example
      #
      #   # bad
      #   Gem::Specification.new do |spec|
      #     s.name = 'your_cool_gem_name'
      #     spec.date = Time.now.strftime('%Y-%m-%d')
      #   end
      #
      #   # good
      #   Gem::Specification.new do |spec|
      #     s.name = 'your_cool_gem_name'
      #   end
      #
      class DateAssignment < Base
        include RangeHelp
        extend AutoCorrector

        MSG = 'Do not use `date =` in gemspec, it is set automatically when the gem is packaged.'

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

        def on_block(block_node)
          return unless gem_specification(block_node)

          block_parameter = block_node.arguments.first.source

          date_assignment = block_node.descendants.detect do |node|
            node.send_type? && node.receiver&.source == block_parameter && node.method?(:date=)
          end

          return unless date_assignment

          add_offense(date_assignment) do |corrector|
            range = range_by_whole_lines(date_assignment.source_range, include_final_newline: true)

            corrector.remove(range)
          end
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rubocop-1.10.0 lib/rubocop/cop/gemspec/date_assignment.rb