Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
Showing
4 changed files
with
35 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |