Class: RuboCop::Cop::Gemspec::DateAssignment

Inherits:
Base
  • Object
show all
Extended by:
AutoCorrector
Includes:
RangeHelp
Defined in:
lib/rubocop/cop/gemspec/date_assignment.rb

Overview

This cop checks that `date =` is not used in gemspec file. It is set automatically when the gem is packaged.

Examples:


# 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

Constant Summary collapse

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

Constants inherited from Base

Base::RESTRICT_ON_SEND

Instance Attribute Summary

Attributes inherited from Base

#config, #processed_source

Instance Method Summary collapse

Methods included from AutoCorrector

support_autocorrect?

Methods inherited from Base

#add_global_offense, #add_offense, autocorrect_incompatible_with, badge, #callbacks_needed, callbacks_needed, #config_to_allow_offenses, #config_to_allow_offenses=, #cop_config, #cop_name, cop_name, department, documentation_url, exclude_from_registry, #excluded_file?, #external_dependency_checksum, inherited, #initialize, joining_forces, lint?, match?, #message, #offenses, #on_investigation_end, #on_new_investigation, #on_other_file, #parse, #ready, #relevant_file?, support_autocorrect?, support_multiple_source?, #target_rails_version, #target_ruby_version

Methods included from ExcludeLimit

#exclude_limit

Methods included from AutocorrectLogic

#autocorrect?, #autocorrect_enabled?, #autocorrect_requested?, #correctable?, #disable_uncorrectable?, #safe_autocorrect?

Methods included from IgnoredNode

#ignore_node, #ignored_node?, #part_of_ignored_node?

Methods included from Util

silence_warnings

Constructor Details

This class inherits a constructor from RuboCop::Cop::Base

Instance Method Details

#gem_specification(node) ⇒ Object


29
30
31
32
33
34
35
# File 'lib/rubocop/cop/gemspec/date_assignment.rb', line 29

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

#on_block(block_node) ⇒ Object


37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/rubocop/cop/gemspec/date_assignment.rb', line 37

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