Skip to content

Commit

Permalink
Refactor shared gemspec matcher into a mixin.
Browse files Browse the repository at this point in the history
  • Loading branch information
dvandersluis committed Nov 12, 2021
1 parent c7c498f commit a4ea853
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 30 deletions.
1 change: 1 addition & 0 deletions lib/rubocop.rb
Expand Up @@ -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'
Expand Down
12 changes: 2 additions & 10 deletions lib/rubocop/cop/gemspec/date_assignment.rb
Expand Up @@ -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

Expand Down
11 changes: 1 addition & 10 deletions lib/rubocop/cop/gemspec/duplicated_assignment.rb
Expand Up @@ -36,20 +36,11 @@ module Gemspec
# end
class DuplicatedAssignment < Base
include RangeHelp
include GemspecHelp

MSG = '`%<assignment>s` method calls already given on line '\
'%<line_of_first_occurrence>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
Expand Down
13 changes: 3 additions & 10 deletions lib/rubocop/cop/gemspec/ruby_version_globals_usage.rb
Expand Up @@ -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)

Expand All @@ -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
Expand Down
30 changes: 30 additions & 0 deletions 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

0 comments on commit a4ea853

Please sign in to comment.