Module: RuboCop::NodePattern::Macros
- Included in:
- AST::MethodDispatchNode, AST::Node, Cop::Cop, Cop::DefNode, Cop::DocumentationComment, Cop::EachToForCorrector, Cop::EmptyParameter, Cop::ForToEachCorrector, Cop::Layout::EmptyLinesAroundBody, Cop::Lint::UnusedArgument, Cop::MethodComplexity, Cop::NegativeConditional, Cop::Performance::RedundantMerge::EachWithObjectInspector, Cop::SafeAssignment, Cop::Style::ConditionalAssignmentHelper, Cop::Style::ParallelAssignment::AssignmentSorter
- Defined in:
- lib/rubocop/node_pattern.rb
Overview
Helpers for defining methods based on a pattern string
Instance Method Summary collapse
-
#def_node_matcher(method_name, pattern_str) ⇒ Object
Define a method which applies a pattern to an AST node.
-
#def_node_search(method_name, pattern_str) ⇒ Object
Define a method which recurses over the descendants of an AST node, checking whether any of them match the provided pattern.
- #node_search(method_name, compiler, on_match, prelude, called_from) ⇒ Object
- #node_search_all(method_name, compiler, called_from) ⇒ Object
- #node_search_body(method_name, trailing_params, prelude, match_code, on_match) ⇒ Object
- #node_search_first(method_name, compiler, called_from) ⇒ Object
Instance Method Details
#def_node_matcher(method_name, pattern_str) ⇒ Object
Define a method which applies a pattern to an AST node
The new method will return nil if the node does not match If the node matches, and a block is provided, the new method will yield to the block (passing any captures as block arguments). If the node matches, and no block is provided, the new method will return the captures, or true if there were none.
486 487 488 489 490 491 492 493 494 |
# File 'lib/rubocop/node_pattern.rb', line 486 def def_node_matcher(method_name, pattern_str) compiler = Compiler.new(pattern_str, 'node') src = "def #{method_name}(node = self" \ "#{compiler.emit_trailing_params});" \ "#{compiler.emit_method_code};end" location = caller_locations(1, 1).first class_eval(src, location.path, location.lineno) end |
#def_node_search(method_name, pattern_str) ⇒ Object
Define a method which recurses over the descendants of an AST node, checking whether any of them match the provided pattern
If the method name ends with ‘?’, the new method will return true as soon as it finds a descendant which matches. Otherwise, it will yield all descendants which match.
502 503 504 505 506 507 508 509 510 511 |
# File 'lib/rubocop/node_pattern.rb', line 502 def def_node_search(method_name, pattern_str) compiler = Compiler.new(pattern_str, 'node') called_from = caller(1..1).first.split(':') if method_name.to_s.end_with?('?') node_search_first(method_name, compiler, called_from) else node_search_all(method_name, compiler, called_from) end end |
#node_search(method_name, compiler, on_match, prelude, called_from) ⇒ Object
527 528 529 530 531 532 |
# File 'lib/rubocop/node_pattern.rb', line 527 def node_search(method_name, compiler, on_match, prelude, called_from) src = node_search_body(method_name, compiler.emit_trailing_params, prelude, compiler.match_code, on_match) filename, lineno = *called_from class_eval(src, filename, lineno.to_i) end |
#node_search_all(method_name, compiler, called_from) ⇒ Object
517 518 519 520 521 522 523 524 525 |
# File 'lib/rubocop/node_pattern.rb', line 517 def node_search_all(method_name, compiler, called_from) yieldval = compiler.emit_capture_list yieldval = 'node' if yieldval.empty? prelude = "return enum_for(:#{method_name}, node0" \ "#{compiler.emit_trailing_params}) unless block_given?" node_search(method_name, compiler, "yield(#{yieldval})", prelude, called_from) end |
#node_search_body(method_name, trailing_params, prelude, match_code, on_match) ⇒ Object
534 535 536 537 538 539 540 541 542 543 544 545 546 547 |
# File 'lib/rubocop/node_pattern.rb', line 534 def node_search_body(method_name, trailing_params, prelude, match_code, on_match) " def \#{method_name}(node0\#{trailing_params})\n \#{prelude}\n node0.each_node do |node|\n if \#{match_code}\n \#{on_match}\n end\n end\n nil\n end\n RUBY\nend\n" |
#node_search_first(method_name, compiler, called_from) ⇒ Object
513 514 515 |
# File 'lib/rubocop/node_pattern.rb', line 513 def node_search_first(method_name, compiler, called_from) node_search(method_name, compiler, 'return true', '', called_from) end |