Class: Defacer::WhitespaceRemovingVisitor

Inherits:
RKelly::Visitors::ECMAVisitor
  • Object
show all
Defined in:
lib/defacer.rb

Instance Method Summary collapse

Constructor Details

#initializeWhitespaceRemovingVisitor

Returns a new instance of WhitespaceRemovingVisitor.



8
9
10
11
12
# File 'lib/defacer.rb', line 8

def initialize
  super
  @bound_var_names = {}
  @function_depth = 0
end

Instance Method Details

#bind_var_name(o) ⇒ Object

We’ve hit a new variable declaration, bind it to a shorter name



115
116
117
118
119
120
121
# File 'lib/defacer.rb', line 115

def bind_var_name(o)
  if in_local_var_context
    @bound_var_names[o] = make_next_var
  else
    o
  end
end

#find_bound_name_for_var(o) ⇒ Object



123
124
125
# File 'lib/defacer.rb', line 123

def find_bound_name_for_var(o)
  @bound_var_names[o.value]
end

#function_params_and_body(o) ⇒ Object



138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/defacer.rb', line 138

def function_params_and_body(o)
  # Track depth to determine if we are at global scope
  @function_depth += 1

  # Save the current binding
  saved_bound_var_names = @bound_var_names.dup

  "(#{o.arguments.map { |x| bind_var_name(x.value) }.join(',')})" +
    "#{o.function_body.accept(self)}"

ensure
  # Restore the binding
  @bound_var_names = saved_bound_var_names

  # Restore the depth
  @function_depth -= 1
end

#in_local_var_contextObject



134
135
136
# File 'lib/defacer.rb', line 134

def in_local_var_context
  @function_depth > 0
end

#make_next_varObject



127
128
129
# File 'lib/defacer.rb', line 127

def make_next_var
  Defacer::Namer.name_var_at_index(@bound_var_names.size)
end

#make_var_at_index(x) ⇒ Object



131
132
# File 'lib/defacer.rb', line 131

def make_var_at_index(x)
end

#visit_ArgumentsNode(o) ⇒ Object



50
51
52
# File 'lib/defacer.rb', line 50

def visit_ArgumentsNode(o)
  o.value.map { |x| x.accept(self) }.join(',')
end

#visit_ArrayNode(o) ⇒ Object



38
39
40
# File 'lib/defacer.rb', line 38

def visit_ArrayNode(o)
  "[#{o.value.map { |x| x ? x.accept(self) : '' }.join(',')}]"
end

#visit_AssignExprNode(o) ⇒ Object



46
47
48
# File 'lib/defacer.rb', line 46

def visit_AssignExprNode(o)
  "=#{o.value.accept(self)}"
end

#visit_BlockNode(o) ⇒ Object



30
31
32
# File 'lib/defacer.rb', line 30

def visit_BlockNode(o)
  "{#{o.value.accept(self)}}"
end

#visit_ForNode(o) ⇒ Object



106
107
108
109
110
111
112
# File 'lib/defacer.rb', line 106

def visit_ForNode(o)
  init    = o.init ? o.init.accept(self) : ';'
  init    << ';' unless init.end_with? ';' # make sure it has a ;
  test    = o.test ? o.test.accept(self) : ''
  counter = o.counter ? o.counter.accept(self) : ''
  "for(#{init}#{test};#{counter})#{o.value.accept(self)}"
end

#visit_FunctionBodyNode(o) ⇒ Object



26
27
28
# File 'lib/defacer.rb', line 26

def visit_FunctionBodyNode(o)
  "{#{o.value.accept(self)}}"
end

#visit_IfNode(o) ⇒ Object



101
102
103
104
# File 'lib/defacer.rb', line 101

def visit_IfNode(o)
  "if(#{o.conditions.accept(self)})#{o.value.accept(self)}" +
    (o.else ? "else #{o.else.accept(self)}" : '')
end

#visit_ObjectLiteralNode(o) ⇒ Object



34
35
36
# File 'lib/defacer.rb', line 34

def visit_ObjectLiteralNode(o)
  '{' + o.value.map { |x| x.accept(self) }.join(',') + '}'
end

#visit_OpEqualNode(o) ⇒ Object



97
98
99
# File 'lib/defacer.rb', line 97

def visit_OpEqualNode(o)
  "#{o.left.accept(self)}=#{o.value.accept(self)}"
end

#visit_PropertyNode(o) ⇒ Object



42
43
44
# File 'lib/defacer.rb', line 42

def visit_PropertyNode(o)
  "#{o.name}:#{o.value.accept(self)}"
end

#visit_ResolveNode(o) ⇒ Object



93
94
95
# File 'lib/defacer.rb', line 93

def visit_ResolveNode(o)
  find_bound_name_for_var(o) || super(o)
end

#visit_SourceElementsNode(o) ⇒ Object



14
15
16
# File 'lib/defacer.rb', line 14

def visit_SourceElementsNode(o)
  o.value.map { |x| "#{x.accept(self)}" }.join
end

#visit_VarDeclNode(o) ⇒ Object



22
23
24
# File 'lib/defacer.rb', line 22

def visit_VarDeclNode(o)
  "#{bind_var_name(o.name)}#{o.value ? o.value.accept(self) : nil}"
end

#visit_VarStatementNode(o) ⇒ Object



18
19
20
# File 'lib/defacer.rb', line 18

def visit_VarStatementNode(o)
  "var #{o.value.map { |x| x.accept(self) }.join(',')};"
end