Class: Rewrite::Prelude::Andand
- Inherits:
-
SexpProcessor
- Object
- SexpProcessor
- Rewrite::Prelude::Andand
- Defined in:
- lib/rewrite/prelude/andand.rb
Overview
Adds a guarded method invocation to Ruby:
with(andand) do
...
@phone = Location.find(:first, ...elided... ).andand.phone
...
end
It also works with parameters, blocks, and procs passed as blocks.
Works by rewriting expressions like:
numbers.andand.inject(&:+)
Into:
lambda { |__1234567890__|
if __1234567890__.nil?
nil
else
__1234567890__.inject(&:+)
end
}.call(numbers)
See also: Please, Try
Instance Method Summary collapse
Instance Method Details
#process_block_pass(exp) ⇒ Object
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 |
# File 'lib/rewrite/prelude/andand.rb', line 125 def process_block_pass(exp) orig = lambda { |exp_dup| lambda { Ruby2Ruby.new.process(exp_dup) } }.call(exp) # [:block_pass, [:lit, :+], [:call, [:call, [:vcall, :foo], :andand], :bar]] # [:block_pass, [:lit, :blitz], [:call, [:call, [:vcall, :foo], :andand], :bar, [:array, [:lit, :bash]]]] exp.shift # [[:lit, :+], [:call, [:call, [:vcall, :foo], :andand], :bar]] # [[:lit, :blitz], [:call, [:call, [:vcall, :foo], :andand], :bar, [:array, [:lit, :bash]]]] block_exp = process_inner_expr(exp.shift) call_exp = exp.shift raise "expected block_pass to have a receiver and a call form near #{orig.call}" unless exp.empty? # [:call, [:call, [:vcall, :foo], :andand], :bar] # [:call, [:call, [:vcall, :foo], :andand], :bar, [:array, [:lit, :bash]]] raise 'confused' unless call_exp.shift == :call # [[:call, [:vcall, :foo], :andand], :bar] # [[:call, [:vcall, :foo], :andand], :bar, [:array, [:lit, :bash]]] receiver_sexp = call_exp.first if matches_andand_invocation(receiver_sexp) # [:call, [:vcall, :foo], :andand] call_exp.shift # [:bar] # [:bar, [:array, [:lit, :bash]]] mono_parameter = Rewrite.gensym() s(:call, s(:iter, s(:fcall, :lambda), s(:dasgn_curr, mono_parameter), s(:if, s(:call, s(:dvar, mono_parameter), :nil?), s(:nil), s(:block_pass, block_exp, s(:call, s(:dvar, mono_parameter), *(call_exp.map { |inner| process_inner_expr inner }) ) ) ) ), :call, s(:array, process_inner_expr(receiver_sexp[1]) # s(:lit, :foo) ) ) else s(:block_pass, block_exp, s(:call, *(call_exp.map { |inner| process_inner_expr inner }) ) ) end end |
#process_call(exp) ⇒ Object
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/rewrite/prelude/andand.rb', line 82 def process_call(exp) # s(:call, s(:call, s(:lit, :foo), :andand), :bar) exp.shift # s(s(:call, s(:lit, :foo), :andand), :bar) receiver_sexp = exp.first if matches_andand_invocation(receiver_sexp) # s(:call, s(:lit, :foo), :andand) exp.shift # s( :bar ) mono_parameter = Rewrite.gensym() s(:call, s(:iter, s(:fcall, :lambda), s(:dasgn_curr, mono_parameter), s(:if, s(:call, s(:dvar, mono_parameter), :nil?), s(:nil), begin s(:call, s(:dvar, mono_parameter), *(exp.map { |inner| process_inner_expr inner }) ) ensure exp.clear end ) ), :call, s(:array, process_inner_expr(receiver_sexp[1]) # s(:lit, :foo) ) ) else # pass through begin s(:call, *(exp.map { |inner| process_inner_expr inner }) ) ensure exp.clear end end end |
#process_iter(exp) ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/rewrite/prelude/andand.rb', line 40 def process_iter(exp) exp.shift receiver_sexp = exp.first #[:call, [:call, [:lit, 1..10], :andand], :inject] if receiver_sexp[0] == :call && matches_andand_invocation(receiver_sexp[1]) exp.shift mono_parameter = Rewrite.gensym() s(:call, s(:iter, s(:fcall, :lambda), s(:dasgn_curr, mono_parameter), s(:if, s(:call, s(:dvar, mono_parameter), :nil?), s(:nil), begin s(:iter, s(:call, s(:dvar, mono_parameter), *(receiver_sexp[2..-1].map { |inner| process_inner_expr inner }) ), *(exp.map { |inner| process_inner_expr inner }) ) ensure exp.clear end ) ), :call, s(:array, process_inner_expr(receiver_sexp[1][1]) # s(:lit, 1..10) ) ) else begin s(:iter, *(exp.map { |inner| process_inner_expr inner }) ) ensure exp.clear end end end |