class Transpec::Syntax::Operator

Constants

BE_NODE
OPERATORS

Public Class Methods

new(*args) click to toggle source
Calls superclass method Transpec::Syntax::new
# File lib/transpec/syntax/operator.rb, line 28
def initialize(*args)
  node = args.shift

  operator_node = if node == BE_NODE
                    node.parent
                  else
                    node
                  end

  super(operator_node, *args)
end

Public Instance Methods

convert_operator!(parenthesize_arg = true) click to toggle source
# File lib/transpec/syntax/operator.rb, line 44
def convert_operator!(parenthesize_arg = true)
  case method_name
  when :==
    convert_to_eq!(parenthesize_arg)
  when :=~
    convert_to_match!(parenthesize_arg)
  else
    convert_to_be_operator!
  end
end
dynamic_analysis_target?() click to toggle source
# File lib/transpec/syntax/operator.rb, line 40
def dynamic_analysis_target?
  super && receiver_node && OPERATORS.include?(method_name)
end
parenthesize!(always = true) click to toggle source
# File lib/transpec/syntax/operator.rb, line 55
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

Private Instance Methods

add_record(old_syntax, new_syntax, accurate = true) click to toggle source
Calls superclass method Transpec::Syntax#add_record
# File lib/transpec/syntax/operator.rb, line 166
def add_record(old_syntax, new_syntax, accurate = true)
  old_syntax ||= "#{method_name} expected"
  annotation = AccuracyAnnotation.new(matcher_range) unless accurate
  super(old_syntax, new_syntax, { annotation: annotation })
end
be_node() click to toggle source
# File lib/transpec/syntax/operator.rb, line 150
def be_node
  if receiver_node == BE_NODE
    receiver_node
  else
    nil
  end
end
convert_to_be_operator!() click to toggle source
# File lib/transpec/syntax/operator.rb, line 76
def convert_to_be_operator!
  return if prefixed_with_be?
  insert_before(selector_range, 'be ')
  add_record(nil, "be #{method_name} expected")
end
convert_to_eq!(parenthesize_arg) click to toggle source
# File lib/transpec/syntax/operator.rb, line 69
def convert_to_eq!(parenthesize_arg)
  handle_anterior_of_operator!
  replace(selector_range, 'eq')
  parenthesize!(parenthesize_arg)
  add_record(nil, 'eq(expected)')
end
convert_to_match!(parenthesize_arg) click to toggle source
# File lib/transpec/syntax/operator.rb, line 82
def convert_to_match!(parenthesize_arg)
  handle_anterior_of_operator!

  if using_match_array?
    replace(selector_range, 'match_array')
  else
    replace(selector_range, 'match')
  end

  parenthesize!(parenthesize_arg)

  accurate = !using_match_array?.nil?

  # Need to register record after all source rewrites are done
  # to avoid false record when failed with overlapped rewrite.
  if using_match_array?
    add_record('=~ [1, 2]', 'match_array([1, 2])', accurate)
  else
    add_record('=~ /pattern/', 'match(/pattern/)', accurate)
  end
end
handle_anterior_of_operator!() click to toggle source
# File lib/transpec/syntax/operator.rb, line 104
def handle_anterior_of_operator!
  if prefixed_with_be?
    remove_be!
  elsif range_in_between_receiver_and_selector.source.empty?
    insert_before(selector_range, ' ')
  end
end
matcher_range() click to toggle source
# File lib/transpec/syntax/operator.rb, line 158
def matcher_range
  if be_node
    expression_range
  else
    selector_range.join(expression_range.end)
  end
end
parenthesize_multi_line!(linefeed) click to toggle source
# File lib/transpec/syntax/operator.rb, line 134
def parenthesize_multi_line!(linefeed)
  insert_before(range_in_between_selector_and_arg, '(')
  matcher_line_indentation = indentation_of_line(node)
  right_parenthesis = "#{linefeed}#{matcher_line_indentation})"
  insert_after(expression_range, right_parenthesis)
end
parenthesize_single_line!(always) click to toggle source
# File lib/transpec/syntax/operator.rb, line 123
def parenthesize_single_line!(always)
  if in_explicit_parentheses?(arg_node)
    remove(range_in_between_selector_and_arg)
  elsif always || arg_node.hash_type?
    replace(range_in_between_selector_and_arg, '(')
    insert_after(expression_range, ')')
  elsif range_in_between_selector_and_arg.source.empty?
    insert_after(selector_range, ' ')
  end
end
prefixed_with_be?() click to toggle source
# File lib/transpec/syntax/operator.rb, line 141
def prefixed_with_be?
  be_node
end
remove_be!() click to toggle source
# File lib/transpec/syntax/operator.rb, line 145
def remove_be!
  be_range = be_node.loc.expression.join(selector_range.begin)
  remove(be_range)
end
using_match_array?() click to toggle source
# File lib/transpec/syntax/operator.rb, line 112
def using_match_array?
  case arg_node.type
  when :array
    true
  when :regexp
    false
  else
    runtime_data[arg_node, :using_match_array?]
  end
end