Module: Ruby2JS::Filter::CamelCase

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

Constant Summary collapse

ALLOWLIST =
%w{
  attr_accessor
  method_missing
}
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



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

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



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

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

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

#on_arg(node) ⇒ Object



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

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

#on_attr(node) ⇒ Object



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

def on_attr(node)
  on_send(node)
end

#on_csend(node) ⇒ Object



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

def on_csend(node)
  on_send(node)
end

#on_cvar(node) ⇒ Object



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

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

#on_cvasgn(node) ⇒ Object



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

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

#on_def(node) ⇒ Object



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

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

#on_defs(node) ⇒ Object



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

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



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

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

#on_ivasgn(node) ⇒ Object



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

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

#on_kwoptarg(node) ⇒ Object



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

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

#on_lvar(node) ⇒ Object



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

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

#on_lvasgn(node) ⇒ Object



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

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

#on_optarg(node) ⇒ Object



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

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

#on_send(node) ⇒ Object



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

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

  if node.children[0] == nil and ALLOWLIST.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



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

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