class Defacer::WhitespaceRemovingVisitor

Public Class Methods

new() click to toggle source
Calls superclass method
# File lib/defacer.rb, line 8
def initialize
  super
  @bound_var_names = {}
  @function_depth = 0
end

Public Instance Methods

bind_var_name(o) click to toggle source

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

# 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) click to toggle source
# File lib/defacer.rb, line 123
def find_bound_name_for_var(o)
  @bound_var_names[o.value]
end
function_params_and_body(o) click to toggle source
# 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_context() click to toggle source
# File lib/defacer.rb, line 134
def in_local_var_context
  @function_depth > 0
end
make_next_var() click to toggle source
# 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) click to toggle source
# File lib/defacer.rb, line 131
def make_var_at_index(x)
end
visit_ArgumentsNode(o) click to toggle source
# File lib/defacer.rb, line 50
def visit_ArgumentsNode(o)
  o.value.map { |x| x.accept(self) }.join(',')
end
visit_ArrayNode(o) click to toggle source
# File lib/defacer.rb, line 38
def visit_ArrayNode(o)
  "[#{o.value.map { |x| x ? x.accept(self) : '' }.join(',')}]"
end
visit_AssignExprNode(o) click to toggle source
# File lib/defacer.rb, line 46
def visit_AssignExprNode(o)
  "=#{o.value.accept(self)}"
end
visit_BlockNode(o) click to toggle source
# File lib/defacer.rb, line 30
def visit_BlockNode(o)
  "{#{o.value.accept(self)}}"
end
visit_ForNode(o) click to toggle source
# 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) click to toggle source
# File lib/defacer.rb, line 26
def visit_FunctionBodyNode(o)
  "{#{o.value.accept(self)}}"
end
visit_IfNode(o) click to toggle source
# 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) click to toggle source
# File lib/defacer.rb, line 34
def visit_ObjectLiteralNode(o)
  '{' + o.value.map { |x| x.accept(self) }.join(',') + '}'
end
visit_OpEqualNode(o) click to toggle source
# File lib/defacer.rb, line 97
def visit_OpEqualNode(o)
  "#{o.left.accept(self)}=#{o.value.accept(self)}"
end
visit_PropertyNode(o) click to toggle source
# File lib/defacer.rb, line 42
def visit_PropertyNode(o)
  "#{o.name}:#{o.value.accept(self)}"
end
visit_ResolveNode(o) click to toggle source
Calls superclass method
# File lib/defacer.rb, line 93
def visit_ResolveNode(o)
  find_bound_name_for_var(o) || super(o)
end
visit_SourceElementsNode(o) click to toggle source
# File lib/defacer.rb, line 14
def visit_SourceElementsNode(o)
  o.value.map { |x| "#{x.accept(self)}" }.join
end
visit_VarDeclNode(o) click to toggle source
# 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) click to toggle source
# File lib/defacer.rb, line 18
def visit_VarStatementNode(o)
  "var #{o.value.map { |x| x.accept(self) }.join(',')};"
end