Class: Transpec::Syntax::OperatorMatcher

Inherits:
Transpec::Syntax show all
Extended by:
AST::Sexp
Includes:
Mixin::Send, Util
Defined in:
lib/transpec/syntax/operator_matcher.rb

Constant Summary collapse

OPERATORS =
[:==, :===, :<, :<=, :>, :>=, :=~].freeze
BE_NODE =
s(:send, nil, :be)

Constants included from Util

Util::LITERAL_TYPES, Util::WHITESPACES

Instance Attribute Summary

Attributes inherited from Transpec::Syntax

#node, #report, #runtime_data, #source_rewriter

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Mixin::Send

#arg_node, #arg_nodes, #arg_range, #args_range, #method_name, #parentheses_range, #range_after_arg, #range_in_between_receiver_and_selector, #range_in_between_selector_and_arg, #receiver_node, #receiver_range, #selector_range

Methods included from Util

beginning_of_line_range, block_node_taken_by_method, const_name, contain_here_document?, each_backward_chained_node, each_forward_chained_node, expand_range_to_adjacent_whitespaces, find_consecutive_whitespace_position, first_block_arg_name, here_document?, in_explicit_parentheses?, indentation_of_line, literal?, proc_literal?

Methods inherited from Transpec::Syntax

conversion_target_node?, #expression_range, #parent_node, snake_case_name, #static_context_inspector

Methods included from Collection

#all_syntaxes, #inherited, #require_all, #standalone_syntaxes

Methods included from DynamicAnalysis

#register_request_for_dynamic_analysis

Constructor Details

#initialize(node, source_rewriter = nil, runtime_data = nil, report = nil) ⇒ OperatorMatcher

Returns a new instance of OperatorMatcher.



35
36
37
38
39
40
41
42
43
# File 'lib/transpec/syntax/operator_matcher.rb', line 35

def initialize(node, source_rewriter = nil, runtime_data = nil, report = nil)
  operator_node = if node == BE_NODE
                    node.parent_node
                  else
                    node
                  end

  super(operator_node, source_rewriter, runtime_data, report)
end

Class Method Details

.standalone?Boolean

Returns:

  • (Boolean)


17
18
19
# File 'lib/transpec/syntax/operator_matcher.rb', line 17

def self.standalone?
  false
end

.target_node?(node, runtime_data = nil) ⇒ Boolean

Returns:

  • (Boolean)


21
22
23
24
25
26
27
# File 'lib/transpec/syntax/operator_matcher.rb', line 21

def self.target_node?(node, runtime_data = nil)
  node = node.parent_node if node == BE_NODE
  receiver_node, method_name, *_ = *node
  return false if receiver_node.nil?
  return false unless OPERATORS.include?(method_name)
  check_target_node_dynamically(node, runtime_data)
end

Instance Method Details

#convert_operator!(parenthesize_arg = true) ⇒ Object



45
46
47
48
49
50
51
52
53
54
# File 'lib/transpec/syntax/operator_matcher.rb', line 45

def convert_operator!(parenthesize_arg = true)
  case method_name
  when :==
    convert_to_eq!(parenthesize_arg)
  when :===, :<, :<=, :>, :>=
    convert_to_be_operator!
  when :=~
    convert_to_match!(parenthesize_arg)
  end
end

#parenthesize!(always = true) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
# File 'lib/transpec/syntax/operator_matcher.rb', line 56

def parenthesize!(always = true)
  return if contain_here_document?(arg_node)

  left_of_arg_source = range_in_between_selector_and_arg.source

  if left_of_arg_source.match(/\A *\Z/)
    parenthesize_single_line!(always)
  elsif left_of_arg_source.match(/\n|\r/)
    parenthesize_multi_line!(Regexp.last_match(0))
  end
end