Module: Ruby2JS::Filter::CamelCase

Includes:
SEXP
Defined in:
lib/ruby2js/filter/camelCase.rb

Constant Summary collapse

WHITELIST =
%w{
  attr_accessor
}
CAPS_EXCEPTIONS =
{
  "innerHtml" => "innerHTML",
  "innerHtml=" => "innerHTML=",
  "outerHtml" => "outerHTML",
  "outerHtml=" => "outerHTML=",
  "encodeUri" => "encodeURI",
  "encodeUriComponent" => "encodeURIComponent",
  "decodeUri" => "decodeURI",
  "decodeUriComponent" => "decodeURIComponent"
}

Instance Method Summary collapse

Methods included from SEXP

#S, #s

Instance Method Details

#camelCase(symbol) ⇒ Object



27
28
29
30
31
32
33
34
# File 'lib/ruby2js/filter/camelCase.rb', line 27

def camelCase(symbol)
  should_symbolize = symbol.is_a?(Symbol)
  symbol = symbol
            .to_s
            .gsub(/(?!^)_[a-z0-9]/) {|match| match[1].upcase}
            .gsub(/^(.*)$/) {|match| CAPS_EXCEPTIONS[match] || match }
  should_symbolize ? symbol.to_sym : symbol
end

#handle_generic_node(node, node_type) ⇒ Object



61
62
63
64
65
66
67
68
69
# File 'lib/ruby2js/filter/camelCase.rb', line 61

def handle_generic_node(node, node_type)
  return node if node.type != node_type

  if node.children[0] =~ /_.*\w$/
    S(node_type , camelCase(node.children[0]), *node.children[1..-1])
  else
    node
  end
end

#on_arg(node) ⇒ Object



95
96
97
# File 'lib/ruby2js/filter/camelCase.rb', line 95

def on_arg(node)
  handle_generic_node(super, :arg)
end

#on_attr(node) ⇒ Object



57
58
59
# File 'lib/ruby2js/filter/camelCase.rb', line 57

def on_attr(node)
  on_send(node)
end

#on_csend(node) ⇒ Object



53
54
55
# File 'lib/ruby2js/filter/camelCase.rb', line 53

def on_csend(node)
  on_send(node)
end

#on_cvar(node) ⇒ Object



91
92
93
# File 'lib/ruby2js/filter/camelCase.rb', line 91

def on_cvar(node)
  handle_generic_node(super, :cvar)
end

#on_cvasgn(node) ⇒ Object



107
108
109
# File 'lib/ruby2js/filter/camelCase.rb', line 107

def on_cvasgn(node)
  handle_generic_node(super, :cvasgn)
end

#on_def(node) ⇒ Object



71
72
73
# File 'lib/ruby2js/filter/camelCase.rb', line 71

def on_def(node)
  handle_generic_node(super, :def)
end

#on_defs(node) ⇒ Object



115
116
117
118
119
120
121
122
123
124
125
# File 'lib/ruby2js/filter/camelCase.rb', line 115

def on_defs(node)
  node = super
  return node if node.type != :defs

  if node.children[1] =~ /_.*\w$/
    S(:defs , node.children[0],
      camelCase(node.children[1]), *node.children[2..-1])
  else
    node
  end
end

#on_ivar(node) ⇒ Object



87
88
89
# File 'lib/ruby2js/filter/camelCase.rb', line 87

def on_ivar(node)
  handle_generic_node(super, :ivar)
end

#on_ivasgn(node) ⇒ Object



103
104
105
# File 'lib/ruby2js/filter/camelCase.rb', line 103

def on_ivasgn(node)
  handle_generic_node(super, :ivasgn)
end

#on_kwoptarg(node) ⇒ Object



79
80
81
# File 'lib/ruby2js/filter/camelCase.rb', line 79

def on_kwoptarg(node)
  handle_generic_node(super, :kwoptarg)
end

#on_lvar(node) ⇒ Object



83
84
85
# File 'lib/ruby2js/filter/camelCase.rb', line 83

def on_lvar(node)
  handle_generic_node(super, :lvar)
end

#on_lvasgn(node) ⇒ Object



99
100
101
# File 'lib/ruby2js/filter/camelCase.rb', line 99

def on_lvasgn(node)
  handle_generic_node(super, :lvasgn)
end

#on_optarg(node) ⇒ Object



75
76
77
# File 'lib/ruby2js/filter/camelCase.rb', line 75

def on_optarg(node)
  handle_generic_node(super, :optarg)
end

#on_send(node) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/ruby2js/filter/camelCase.rb', line 36

def on_send(node)
  node = super
  return node unless [:send, :csend, :attr].include? node.type

  if node.children[0] == nil and WHITELIST.include? node.children[1].to_s
    node
  elsif node.children[0] && [:ivar, :cvar].include?(node.children[0].type)
    S(node.type, s(node.children[0].type, camelCase(node.children[0].children[0])),
      camelCase(node.children[1]), *node.children[2..-1])
  elsif node.children[1] =~ /_.*\w[=!?]?$/
    S(node.type, node.children[0],
      camelCase(node.children[1]), *node.children[2..-1])
  else
    node
  end
end

#on_sym(node) ⇒ Object



111
112
113
# File 'lib/ruby2js/filter/camelCase.rb', line 111

def on_sym(node)
  handle_generic_node(super, :sym)
end