Skip to content

Commit

Permalink
[Fix rubocop#10245] Remove Gemspec/DateAssignment
Browse files Browse the repository at this point in the history
Gemspec/DateAssignment functionality is
integrated into Gemspec/DeprecatedAttributeAssignment
  • Loading branch information
kaitithoma committed Jun 15, 2022
1 parent 75b3169 commit 0bcd760
Show file tree
Hide file tree
Showing 10 changed files with 116 additions and 232 deletions.
1 change: 1 addition & 0 deletions changelog/change_integrate_date_assignment_cop.md
@@ -0,0 +1 @@
* [#10245](https://github.com/rubocop/rubocop/pull/10245): **(Breaking)** integrate `Gemspec/DateAssignment` into `Gemspec/DeprecatedAttributeAssignment`. ([@kaitielth][])
7 changes: 0 additions & 7 deletions config/default.yml
Expand Up @@ -235,13 +235,6 @@ Bundler/OrderedGems:

#################### Gemspec ###############################

Gemspec/DateAssignment:
Description: 'Checks that `date =` is not used in gemspec file, it is set automatically when the gem is packaged.'
Enabled: pending
VersionAdded: '1.10'
Include:
- '**/*.gemspec'

Gemspec/DependencyVersion:
Description: 'Requires or forbids specifying gem dependency versions.'
Enabled: false
Expand Down
2 changes: 2 additions & 0 deletions config/obsoletion.yml
Expand Up @@ -55,6 +55,8 @@ renamed:

# Cops that were removed
removed:
Gemspec/DateAssignment:
alternatives: Gemspec/DeprecatedAttributeAssignment
Layout/SpaceAfterControlKeyword:
alternatives: Layout/SpaceAroundKeyword
Layout/SpaceBeforeModifierKeyword:
Expand Down
1 change: 0 additions & 1 deletion docs/modules/ROOT/pages/cops.adoc
Expand Up @@ -82,7 +82,6 @@ In the following section you find all available cops:

=== Department xref:cops_gemspec.adoc[Gemspec]

* xref:cops_gemspec.adoc#gemspecdateassignment[Gemspec/DateAssignment]
* xref:cops_gemspec.adoc#gemspecdependencyversion[Gemspec/DependencyVersion]
* xref:cops_gemspec.adoc#gemspecdeprecatedattributeassignment[Gemspec/DeprecatedAttributeAssignment]
* xref:cops_gemspec.adoc#gemspecduplicatedassignment[Gemspec/DuplicatedAssignment]
Expand Down
31 changes: 0 additions & 31 deletions docs/modules/ROOT/pages/cops_gemspec.adoc
@@ -1,36 +1,5 @@
= Gemspec

== Gemspec/DateAssignment

|===
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed

| Pending
| Yes
| Yes
| 1.10
| -
|===

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

=== Examples

[source,ruby]
----
# bad
Gem::Specification.new do |spec|
spec.name = 'your_cool_gem_name'
spec.date = Time.now.strftime('%Y-%m-%d')
end
# good
Gem::Specification.new do |spec|
spec.name = 'your_cool_gem_name'
end
----

=== Configurable attributes

|===
Expand Down
1 change: 0 additions & 1 deletion lib/rubocop.rb
Expand Up @@ -160,7 +160,6 @@
require_relative 'rubocop/cop/bundler/insecure_protocol_source'
require_relative 'rubocop/cop/bundler/ordered_gems'

require_relative 'rubocop/cop/gemspec/date_assignment'
require_relative 'rubocop/cop/gemspec/dependency_version'
require_relative 'rubocop/cop/gemspec/deprecated_attribute_assignment'
require_relative 'rubocop/cop/gemspec/duplicated_assignment'
Expand Down
49 changes: 0 additions & 49 deletions lib/rubocop/cop/gemspec/date_assignment.rb

This file was deleted.

47 changes: 31 additions & 16 deletions lib/rubocop/cop/gemspec/deprecated_attribute_assignment.rb
Expand Up @@ -4,7 +4,7 @@ module RuboCop
module Cop
module Gemspec
# Checks that deprecated attribute attributes are not set in a gemspec file.
# Removing `test_files` allows the user to receive smaller packed gems.
# Removing deprecated attributes allows the user to receive smaller packed gems.
#
# @example
#
Expand All @@ -29,7 +29,7 @@ class DeprecatedAttributeAssignment < Base
include RangeHelp
extend AutoCorrector

MSG = 'Do not set `test_files` in gemspec.'
MSG = 'Do not set `%<attribute>s` in gemspec.'

# @!method gem_specification(node)
def_node_matcher :gem_specification, <<~PATTERN
Expand All @@ -45,31 +45,46 @@ def on_block(block_node)

block_parameter = block_node.arguments.first.source

date_assignment = block_node.descendants.detect do |node|
use_test_files?(node, block_parameter)
assignment = block_node.descendants.detect do |node|
use_deprecated_attributes?(node, block_parameter)
end
return unless assignment

return unless date_assignment

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

corrector.remove(range)
end
end

private

def use_test_files?(node, block_parameter)
node, method_name = if node.op_asgn_type?
lhs, _op, _rhs = *node
def node_and_method_name(node, attribute)
if node.op_asgn_type?
lhs, _op, _rhs = *node
[lhs, attribute]
else
[node, "#{attribute}=".to_sym]
end
end

def use_deprecated_attributes?(node, block_parameter)
%i[test_files date].each do |attribute|
node, method_name = node_and_method_name(node, attribute)
unless node.send_type? && node.receiver&.source == block_parameter &&
node.method?(method_name)
next
end

[lhs, :test_files]
else
[node, :test_files=]
end
@attribute = attribute.to_s
return true
end
false
end

node.send_type? && node.receiver&.source == block_parameter && node.method?(method_name)
def format_message_from
format(MSG, attribute: @attribute)
end
end
end
Expand Down
53 changes: 0 additions & 53 deletions spec/rubocop/cop/gemspec/date_assignment_spec.rb

This file was deleted.

0 comments on commit 0bcd760

Please sign in to comment.