Skip to content

Commit

Permalink
Add RuntimeMacros module
Browse files Browse the repository at this point in the history
RuntimeMacros `.def_node_matcher` and `.def_node_search` accept block
that will be evaluated within cop context to get pattern pattern.
It can be used to pattern match values from config because are available
only during runtime.
  • Loading branch information
sl4vr committed Jul 8, 2020
1 parent 44e7d6e commit 346b685
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/rubocop-rspec.rb
Expand Up @@ -12,6 +12,7 @@
require_relative 'rubocop/rspec/top_level_describe'
require_relative 'rubocop/rspec/wording'
require_relative 'rubocop/rspec/language'
require_relative 'rubocop/rspec/language/runtime_macros'
require_relative 'rubocop/rspec/language/node_pattern'
require_relative 'rubocop/rspec/top_level_group'
require_relative 'rubocop/rspec/concept'
Expand Down
1 change: 1 addition & 0 deletions lib/rubocop/cop/rspec/cop.rb
Expand Up @@ -20,6 +20,7 @@ module RSpec
class Cop < ::RuboCop::Cop::Cop
include RuboCop::RSpec::Language
include RuboCop::RSpec::Language::NodePattern
extend RuboCop::RSpec::Language::RuntimeMacros

DEFAULT_CONFIGURATION =
RuboCop::RSpec::CONFIG.fetch('AllCops').fetch('RSpec')
Expand Down
1 change: 1 addition & 0 deletions lib/rubocop/rspec/language/node_pattern.rb
Expand Up @@ -6,6 +6,7 @@ module Language
# Common node matchers used for matching against the rspec DSL
module NodePattern
extend RuboCop::NodePattern::Macros
extend RuntimeMacros

def_node_matcher :example_group?, ExampleGroups::ALL.block_pattern
def_node_matcher :shared_group?, SharedGroups::ALL.block_pattern
Expand Down
32 changes: 32 additions & 0 deletions lib/rubocop/rspec/language/runtime_macros.rb
@@ -0,0 +1,32 @@
# frozen_string_literal: true

module RuboCop
module RSpec
module Language
# RuntimeMacros `.def_node_matcher` and `.def_node_search` accept block
# that will be evaluated within cop context to get pattern pattern.
# It can be used to pattern match values from config because are available
# only during runtime.
module RuntimeMacros
def def_runtime_node_matcher(method_name, &pattern_block)
define_method method_name do |node, *args, &block|
pattern = RuboCop::NodePattern.new(instance_eval(&pattern_block))
match = pattern.match(node, *args, &block)

method_name.to_s.end_with?('?') ? !match.nil? : match
end
end

def def_runtime_node_search(method_name, &pattern_block)
define_method method_name do |node, *args, &block|
pattern = RuboCop::NodePattern.new(instance_eval(&pattern_block))
search_method = method_name.to_s.end_with?('?') ? :any? : :select

node.each_node.public_method(search_method)
.call(&pattern.public_method(:match, *args, &block))
end
end
end
end
end
end

0 comments on commit 346b685

Please sign in to comment.