Class: Transpec::Syntax::OperatorMatcher

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

Constant Summary collapse

OPERATORS =
[:==, :===, :<, :<=, :>, :>=, :=~].freeze

Instance Attribute Summary

Attributes inherited from Transpec::Syntax

#ancestor_nodes, #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, included, #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

const_name, contain_here_document?, here_document?, in_parentheses?, indentation_of_line, proc_literal?

Methods inherited from Transpec::Syntax

all_syntaxes, #expression_range, inherited, #parent_node, register_request_for_dynamic_analysis, snake_case_name, standalone_syntaxes, #static_context_inspector, target_node?

Constructor Details

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

Returns a new instance of OperatorMatcher.



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

def initialize(node, source_rewriter = nil, runtime_data = nil, report = nil)
  @node = node
  @source_rewriter = source_rewriter
  @runtime_data = runtime_data
  @report = report || Report.new
end

Class Method Details

.standalone?Boolean

Returns:

  • (Boolean)


14
15
16
# File 'lib/transpec/syntax/operator_matcher.rb', line 14

def self.standalone?
  false
end

.target_method?(receiver_node, method_name) ⇒ Boolean

Returns:

  • (Boolean)


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

def self.target_method?(receiver_node, method_name)
  !receiver_node.nil? && OPERATORS.include?(method_name)
end

Instance Method Details

#correct_operator!(parenthesize_arg = true) ⇒ Object



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

def correct_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



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

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

#register_request_for_dynamic_analysis(rewriter) ⇒ Object



29
30
31
32
# File 'lib/transpec/syntax/operator_matcher.rb', line 29

def register_request_for_dynamic_analysis(rewriter)
  return unless method_name == :=~
  rewriter.register_request(arg_node, :arg_is_enumerable?, 'is_a?(Enumerable)')
end