From 12e09935faf8af7729c2562c97e71c05aef2bdc4 Mon Sep 17 00:00:00 2001 From: Daniel Vandersluis Date: Fri, 12 Nov 2021 13:48:24 -0500 Subject: [PATCH] Refactor shared gemspec matcher into a mixin. --- lib/rubocop.rb | 1 + lib/rubocop/cop/gemspec/date_assignment.rb | 12 ++------ .../cop/gemspec/duplicated_assignment.rb | 11 +------ .../cop/gemspec/ruby_version_globals_usage.rb | 13 ++------ lib/rubocop/cop/mixin/gemspec_help.rb | 30 +++++++++++++++++++ 5 files changed, 37 insertions(+), 30 deletions(-) create mode 100644 lib/rubocop/cop/mixin/gemspec_help.rb diff --git a/lib/rubocop.rb b/lib/rubocop.rb index 54363c1a8f2..d353cca8478 100644 --- a/lib/rubocop.rb +++ b/lib/rubocop.rb @@ -83,6 +83,7 @@ require_relative 'rubocop/cop/mixin/first_element_line_break' require_relative 'rubocop/cop/mixin/frozen_string_literal' require_relative 'rubocop/cop/mixin/gem_declaration' +require_relative 'rubocop/cop/mixin/gemspec_help' require_relative 'rubocop/cop/mixin/hash_alignment_styles' require_relative 'rubocop/cop/mixin/hash_transform_method' require_relative 'rubocop/cop/mixin/ignored_pattern' diff --git a/lib/rubocop/cop/gemspec/date_assignment.rb b/lib/rubocop/cop/gemspec/date_assignment.rb index 857a99aa3b8..b8a0645bbc6 100644 --- a/lib/rubocop/cop/gemspec/date_assignment.rb +++ b/lib/rubocop/cop/gemspec/date_assignment.rb @@ -21,21 +21,13 @@ module Gemspec # class DateAssignment < Base include RangeHelp + include GemspecHelp extend AutoCorrector MSG = 'Do not use `date =` in gemspec, it is set automatically when the gem is packaged.' - # @!method gem_specification(node) - 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) + return unless gem_specification?(block_node) block_parameter = block_node.arguments.first.source diff --git a/lib/rubocop/cop/gemspec/duplicated_assignment.rb b/lib/rubocop/cop/gemspec/duplicated_assignment.rb index b832db4f476..8bd10fe0157 100644 --- a/lib/rubocop/cop/gemspec/duplicated_assignment.rb +++ b/lib/rubocop/cop/gemspec/duplicated_assignment.rb @@ -36,20 +36,11 @@ module Gemspec # end class DuplicatedAssignment < Base include RangeHelp + include GemspecHelp MSG = '`%s` method calls already given on line '\ '%d of the gemspec.' - # @!method gem_specification(node) - def_node_search :gem_specification, <<~PATTERN - (block - (send - (const - (const {cbase nil?} :Gem) :Specification) :new) - (args - (arg $_)) ...) - PATTERN - # @!method assignment_method_declarations(node) def_node_search :assignment_method_declarations, <<~PATTERN (send diff --git a/lib/rubocop/cop/gemspec/ruby_version_globals_usage.rb b/lib/rubocop/cop/gemspec/ruby_version_globals_usage.rb index 0f9a867d51f..73ccb290d90 100644 --- a/lib/rubocop/cop/gemspec/ruby_version_globals_usage.rb +++ b/lib/rubocop/cop/gemspec/ruby_version_globals_usage.rb @@ -26,20 +26,13 @@ module Gemspec # 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)' - # @!method gem_specification?(node) - def_node_search :gem_specification?, <<~PATTERN - (block - (send - (const - (const {cbase nil?} :Gem) :Specification) :new) - ...) - PATTERN - def on_const(node) return unless gem_spec_with_ruby_version?(node) @@ -49,7 +42,7 @@ def on_const(node) private def gem_spec_with_ruby_version?(node) - gem_specification?(processed_source.ast) && ruby_version?(node) + gem_specification(processed_source.ast) && ruby_version?(node) end end end diff --git a/lib/rubocop/cop/mixin/gemspec_help.rb b/lib/rubocop/cop/mixin/gemspec_help.rb new file mode 100644 index 00000000000..19d58d51ffc --- /dev/null +++ b/lib/rubocop/cop/mixin/gemspec_help.rb @@ -0,0 +1,30 @@ +# frozen_string_literal: true + +module RuboCop + module Cop + # Common functionality for checking gem declarations. + module GemspecHelp + extend NodePattern::Macros + + # @!method gem_specification?(node) + def_node_matcher :gem_specification?, <<~PATTERN + (block + (send + (const + (const {cbase nil?} :Gem) :Specification) :new) + (args + (arg $_)) ...) + PATTERN + + # @!method gem_specification(node) + def_node_search :gem_specification, <<~PATTERN + (block + (send + (const + (const {cbase nil?} :Gem) :Specification) :new) + (args + (arg $_)) ...) + PATTERN + end + end +end