Class: Transpec::Syntax::Operator

Inherits:
Transpec::Syntax show all
Extended by:
AST::Sexp
Includes:
Mixin::OwnedMatcher, Mixin::Send, Util
Defined in:
lib/transpec/syntax/operator.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, #project, #report, #runtime_data, #source_rewriter

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 Mixin::Send::TargetDetection

#conversion_target?

Methods included from Util

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

Methods inherited from Transpec::Syntax

#conversion_target?, #dependent_syntaxes, #expression_range, #inspect, #parent_node, #rspec_version, snake_case_name, standalone?, #static_context_inspector

Methods included from Collection

#all_syntaxes, #inherited, #mixins, #require_all, #standalone_syntaxes

Methods included from DynamicAnalysis

#register_dynamic_analysis_request

Constructor Details

#initialize(*args) ⇒ Operator

Returns a new instance of Operator.



28
29
30
31
32
33
34
35
36
37
38
# 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

Instance Method Details

#convert_operator!(parenthesize_arg = true) ⇒ Object



44
45
46
47
48
49
50
51
52
53
# 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?Boolean

Returns:

  • (Boolean)


40
41
42
# File 'lib/transpec/syntax/operator.rb', line 40

def dynamic_analysis_target?
  super && receiver_node && OPERATORS.include?(method_name)
end

#parenthesize!(always = true) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
# 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