Skip to content

Commit 346b685

Browse files
committed
Add RuntimeMacros module
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.
1 parent 44e7d6e commit 346b685

File tree

4 files changed

+35
-0
lines changed

4 files changed

+35
-0
lines changed

lib/rubocop-rspec.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
require_relative 'rubocop/rspec/top_level_describe'
1313
require_relative 'rubocop/rspec/wording'
1414
require_relative 'rubocop/rspec/language'
15+
require_relative 'rubocop/rspec/language/runtime_macros'
1516
require_relative 'rubocop/rspec/language/node_pattern'
1617
require_relative 'rubocop/rspec/top_level_group'
1718
require_relative 'rubocop/rspec/concept'

lib/rubocop/cop/rspec/cop.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ module RSpec
2020
class Cop < ::RuboCop::Cop::Cop
2121
include RuboCop::RSpec::Language
2222
include RuboCop::RSpec::Language::NodePattern
23+
extend RuboCop::RSpec::Language::RuntimeMacros
2324

2425
DEFAULT_CONFIGURATION =
2526
RuboCop::RSpec::CONFIG.fetch('AllCops').fetch('RSpec')

lib/rubocop/rspec/language/node_pattern.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ module Language
66
# Common node matchers used for matching against the rspec DSL
77
module NodePattern
88
extend RuboCop::NodePattern::Macros
9+
extend RuntimeMacros
910

1011
def_node_matcher :example_group?, ExampleGroups::ALL.block_pattern
1112
def_node_matcher :shared_group?, SharedGroups::ALL.block_pattern
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# frozen_string_literal: true
2+
3+
module RuboCop
4+
module RSpec
5+
module Language
6+
# RuntimeMacros `.def_node_matcher` and `.def_node_search` accept block
7+
# that will be evaluated within cop context to get pattern pattern.
8+
# It can be used to pattern match values from config because are available
9+
# only during runtime.
10+
module RuntimeMacros
11+
def def_runtime_node_matcher(method_name, &pattern_block)
12+
define_method method_name do |node, *args, &block|
13+
pattern = RuboCop::NodePattern.new(instance_eval(&pattern_block))
14+
match = pattern.match(node, *args, &block)
15+
16+
method_name.to_s.end_with?('?') ? !match.nil? : match
17+
end
18+
end
19+
20+
def def_runtime_node_search(method_name, &pattern_block)
21+
define_method method_name do |node, *args, &block|
22+
pattern = RuboCop::NodePattern.new(instance_eval(&pattern_block))
23+
search_method = method_name.to_s.end_with?('?') ? :any? : :select
24+
25+
node.each_node.public_method(search_method)
26+
.call(&pattern.public_method(:match, *args, &block))
27+
end
28+
end
29+
end
30+
end
31+
end
32+
end

0 commit comments

Comments
 (0)