module Opal::Nodes::Helpers

def current_indent

def current_indent
  compiler.parser_indent
end

def empty_line

def empty_line
  push "\n"
end

def indent(&block)

def indent(&block)
  compiler.indent(&block)
end

def ivar(name)

def ivar(name)
  valid_ivar_name?(name.to_s) ? name : "#{name}$"
end

def js_falsy(sexp)

def js_falsy(sexp)
  if sexp.type == :call
    mid = sexp[2]
    if mid == :block_given?
      scope.uses_block!
      return "#{scope.block_name} === nil"
    end
  end
  with_temp do |tmp|
    [fragment("((#{tmp} = "), expr(sexp), fragment(") === nil || #{tmp} == null || (#{tmp}.$$is_boolean && #{tmp} == false))")]
  end
end

def js_truthy(sexp)

def js_truthy(sexp)
  if optimize = js_truthy_optimize(sexp)
    return optimize
  end
  with_temp do |tmp|
    [fragment("((#{tmp} = "), expr(sexp), fragment(") !== nil && #{tmp} != null && (!#{tmp}.$$is_boolean || #{tmp} == true))")]
  end
end

def js_truthy_optimize(sexp)

def js_truthy_optimize(sexp)
  if sexp.type == :call
    mid = sexp[2]
    receiver_handler_class = (receiver = sexp[1]) && compiler.handlers[receiver.type]
    # Only operator calls on the truthy_optimize? node classes should be optimized.
    # Monkey patch method calls might return 'self'/aka a bridged instance and need
    # the nil check - see discussion at https://github.com/opal/opal/pull/1097
    allow_optimization_on_type = Compiler::COMPARE.include?(mid.to_s) &&
      receiver_handler_class &&
      receiver_handler_class.truthy_optimize?
    if allow_optimization_on_type ||
      mid == :block_given? ||
      mid == :"=="
      expr(sexp)
    end
  elsif [:lvar, :self].include? sexp.type
    [expr(sexp.dup), fragment(" !== false && "), expr(sexp.dup), fragment(" !== nil && "), expr(sexp.dup), fragment(" != null")]
  end
end

def line(*strs)

def line(*strs)
  push "\n#{current_indent}"
  push(*strs)
end

def lvar_to_js(var)

varibales
are valid in javascript. A $ suffix is added to non-valid names.
Converts a ruby lvar/arg name to a js identifier. Not all ruby names
def lvar_to_js(var)
  var = "#{var}$" unless valid_name? var.to_s
  var.to_sym
end

def mid_to_jsid(mid)

wrapped in brackets to use reference notation calling.
have a '.' prefix (for dot-calling), otherwise it will be
a '$', and if the name is a valid javascript identifier, it will
a method/function call. All ruby method names get prefixed with
Converts a ruby method name into its javascript equivalent for
def mid_to_jsid(mid)
  if /\=|\+|\-|\*|\/|\!|\?|<|\>|\&|\||\^|\%|\~|\[/ =~ mid.to_s
    "['$#{mid}']"
  else
    '.$' + mid
  end
end

def property(name)

def property(name)
  valid_name?(name) ? ".#{name}" : "[#{name.inspect}]"
end

def valid_ivar_name?(name)

def valid_ivar_name?(name)
  not (PROTO_SPECIAL_PROPS =~ name or PROTO_SPECIAL_METHODS =~ name)
end

def valid_name?(name)

def valid_name?(name)
  BASIC_IDENTIFIER_RULES =~ name and not(
    ES51_RESERVED_WORD          =~ name or
    ES3_RESERVED_WORD_EXCLUSIVE =~ name or
    IMMUTABLE_PROPS             =~ name
  )
end

def variable(name)

def variable(name)
  valid_name?(name.to_s) ? name : "#{name}$"
end