Class: RubyNext::Language::Rewriters::SafeNavigation

Inherits:
Base
  • Object
show all
Defined in:
lib/ruby-next/language/rewriters/2.3/safe_navigation.rb

Constant Summary collapse

NAME =
"safe-navigation"
SYNTAX_PROBE =
"nil&.x&.nil?"
MIN_SUPPORTED_VERSION =
Gem::Version.new("2.3.0")
SAFE_LVAR =
:__safe_lvar__

Instance Attribute Summary

Attributes inherited from Base

#locals

Instance Method Summary collapse

Methods inherited from Base

ast?, #initialize, #s

Methods inherited from Abstract

ast?, #initialize, text?, unsupported_syntax?, unsupported_version?

Constructor Details

This class inherits a constructor from RubyNext::Language::Rewriters::Base

Instance Method Details

#on_block(node) ⇒ Object



34
35
36
37
38
39
40
# File 'lib/ruby-next/language/rewriters/2.3/safe_navigation.rb', line 34

def on_block(node)
  return super(node) unless node.children[0].type == :csend

  context.track!(self)

  super(decsendize(node))
end

#on_csend(node) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/ruby-next/language/rewriters/2.3/safe_navigation.rb', line 13

def on_csend(node)
  node = super(node)

  context.track! self

  receiver, *args = *node

  new_node = s(:begin,
    node.updated(
      :and,
      [
        process(safe_navigation(receiver)),
        s(:send, safe_lvar, *args)
      ]
    ))

  replace(node.loc.expression, new_node)

  new_node
end

#on_numblock(node) ⇒ Object



42
43
44
45
46
47
48
# File 'lib/ruby-next/language/rewriters/2.3/safe_navigation.rb', line 42

def on_numblock(node)
  return super(node) unless node.children[0].type == :csend

  context.track!(self)

  super(decsendize(node))
end

#on_op_asgn(node) ⇒ Object



50
51
52
53
54
55
56
# File 'lib/ruby-next/language/rewriters/2.3/safe_navigation.rb', line 50

def on_op_asgn(node)
  return super(node) unless node.children[0].type == :csend

  context.track!(self)

  super(decsendize(node))
end