File tree Expand file tree Collapse file tree 4 files changed +35
-0
lines changed Expand file tree Collapse file tree 4 files changed +35
-0
lines changed Original file line number Diff line number Diff line change 1212require_relative 'rubocop/rspec/top_level_describe'
1313require_relative 'rubocop/rspec/wording'
1414require_relative 'rubocop/rspec/language'
15+ require_relative 'rubocop/rspec/language/runtime_macros'
1516require_relative 'rubocop/rspec/language/node_pattern'
1617require_relative 'rubocop/rspec/top_level_group'
1718require_relative 'rubocop/rspec/concept'
Original file line number Diff line number Diff 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' )
Original file line number Diff line number Diff 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
Original file line number Diff line number Diff line change 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
You can’t perform that action at this time.
0 commit comments