Class: Rewrite::ByExample::Unhygienic

Inherits:
SexpProcessor
  • Object
show all
Defined in:
lib/rewrite/by_example/unhygienic.rb

Overview

TODO: ‘mixed modes’ such that we can write string_to_proc as an unhygienic TODO: composable unhygienics (entity matchers are already composable) TODO: how would we rename variable references safely with unhygienics?

CSS model (heterogeneous patterns)? Combinatoral?

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#bind_argumentsObject

Returns the value of attribute bind_arguments.



18
19
20
# File 'lib/rewrite/by_example/unhygienic.rb', line 18

def bind_arguments
  @bind_arguments
end

#from_unfolderObject

Returns the value of attribute from_unfolder.



18
19
20
# File 'lib/rewrite/by_example/unhygienic.rb', line 18

def from_unfolder
  @from_unfolder
end

#to_folderObject

Returns the value of attribute to_folder.



18
19
20
# File 'lib/rewrite/by_example/unhygienic.rb', line 18

def to_folder
  @to_folder
end

Class Method Details

.from(*bind_arguments, &proc) ⇒ Object



31
32
33
# File 'lib/rewrite/by_example/unhygienic.rb', line 31

def self.from(*bind_arguments, &proc)
  self.new.from(*bind_arguments, &proc)
end

Instance Method Details

#from(*bind_arguments, &proc) ⇒ Object



20
21
22
23
24
# File 'lib/rewrite/by_example/unhygienic.rb', line 20

def from(*bind_arguments, &proc)
  self.bind_arguments = bind_arguments
  self.from_unfolder = ObjectToMatcher.binding(*self.bind_arguments).from_example(&proc)
  self
end

#process(exp) ⇒ Object



35
36
37
38
39
# File 'lib/rewrite/by_example/unhygienic.rb', line 35

def process(exp)
  raise "Need a from unfolder" unless from_unfolder
  raise "Need a to folder" unless to_folder
  Rewrite.recursive_s(process_inner(exp))
end

#process_inner(exp) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
# File 'lib/rewrite/by_example/unhygienic.rb', line 41

def process_inner(exp)
  if exp.is_a? Array
    exp = exp.map { |sub_exp| process_inner(sub_exp) }
  end
  if unfolded = from_unfolder.unfold(exp) 
    if refolded = to_folder.fold(unfolded)
      exp = refolded
    end
  end
  exp
end

#to(&proc) ⇒ Object



26
27
28
29
# File 'lib/rewrite/by_example/unhygienic.rb', line 26

def to(&proc)
  self.to_folder = ObjectToMatcher.binding(*self.bind_arguments).from_example(&proc)
  self
end