Class: Transpec::Syntax::Matcher

Inherits:
Transpec::Syntax show all
Includes:
SendNodeSyntax, Util
Defined in:
lib/transpec/syntax/matcher.rb

Instance Attribute Summary

Attributes inherited from Transpec::Syntax

#ancestor_nodes, #in_example_group_context, #node, #source_rewriter

Class Method Summary collapse

Instance Method Summary collapse

Methods included from SendNodeSyntax

#arg_node, #arg_range, #method_name, #receiver_node, #receiver_range, #selector_range

Methods included from Util

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

Methods inherited from Transpec::Syntax

all, #expression_range, inherited, #parent_node, snake_case_name

Constructor Details

#initialize(node, in_example_group_context, source_rewriter) ⇒ Matcher

Returns a new instance of Matcher.



16
17
18
19
20
# File 'lib/transpec/syntax/matcher.rb', line 16

def initialize(node, in_example_group_context, source_rewriter)
  @node = node
  @in_example_group_context = in_example_group_context
  @source_rewriter = source_rewriter
end

Class Method Details

.target_node?(node) ⇒ Boolean

Returns:

  • (Boolean)


12
13
14
# File 'lib/transpec/syntax/matcher.rb', line 12

def self.target_node?(node)
  false
end

Instance Method Details

#correct_operator!(parenthesize_arg = true) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/transpec/syntax/matcher.rb', line 22

def correct_operator!(parenthesize_arg = true)
  case method_name
  when :==
    replace(selector_range, 'eq')
    parenthesize!(parenthesize_arg)
  when :===, :<, :<=, :>, :>=
    insert_before(selector_range, 'be ')
  when :=~
    if arg_node.type == :array
      replace(selector_range, 'match_array')
    else
      replace(selector_range, 'match')
    end
    parenthesize!(parenthesize_arg)
  end
end

#parenthesize!(always = true) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/transpec/syntax/matcher.rb', line 39

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

  case left_parenthesis_range.source
  when ' '
    if in_parentheses?(arg_node)
      remove(left_parenthesis_range)
    elsif always || arg_node.type == :hash
      replace(left_parenthesis_range, '(')
      insert_after(expression_range, ')')
    end
  when "\n", "\r"
    insert_before(left_parenthesis_range, '(')
    linefeed = left_parenthesis_range.source
    matcher_line_indentation = indentation_of_line(@node)
    right_parenthesis = "#{linefeed}#{matcher_line_indentation})"
    insert_after(expression_range, right_parenthesis)
  end
end