Module: Ruby2JS::Filter::CamelCase

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

Constant Summary collapse

ALLOWLIST =
%w{
  attr_accessor
  attr_reader
  attr_writer
  method_missing
  is_a?
  kind_of?
  instance_of?
}
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



33
34
35
36
37
38
39
40
41
42
# File 'lib/ruby2js/filter/camelCase.rb', line 33

def camelCase(symbol)
  return symbol if ALLOWLIST.include?(symbol.to_s)

  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



69
70
71
72
73
74
75
76
77
# File 'lib/ruby2js/filter/camelCase.rb', line 69

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

  if node.children[0].to_s =~ /_.*[?!\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



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

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

#on_assign(node) ⇒ Object



135
136
137
# File 'lib/ruby2js/filter/camelCase.rb', line 135

def on_assign(node)
  S(:assign , node.children[0], *node.children[1..-1].map{ process _1 })
end

#on_attr(node) ⇒ Object



65
66
67
# File 'lib/ruby2js/filter/camelCase.rb', line 65

def on_attr(node)
  on_send(node)
end

#on_csend(node) ⇒ Object



61
62
63
# File 'lib/ruby2js/filter/camelCase.rb', line 61

def on_csend(node)
  on_send(node)
end

#on_cvar(node) ⇒ Object



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

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

#on_cvasgn(node) ⇒ Object



119
120
121
# File 'lib/ruby2js/filter/camelCase.rb', line 119

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

#on_def(node) ⇒ Object



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

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

#on_defs(node) ⇒ Object



139
140
141
142
143
144
145
146
147
148
149
# File 'lib/ruby2js/filter/camelCase.rb', line 139

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



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

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

#on_ivasgn(node) ⇒ Object



115
116
117
# File 'lib/ruby2js/filter/camelCase.rb', line 115

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

#on_kwarg(node) ⇒ Object



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

def on_kwarg(node)
  handle_generic_node(super, :kwarg)
end

#on_kwoptarg(node) ⇒ Object



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

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

#on_lvar(node) ⇒ Object



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

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

#on_lvasgn(node) ⇒ Object



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

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

#on_match_pattern(node) ⇒ Object



123
124
125
# File 'lib/ruby2js/filter/camelCase.rb', line 123

def on_match_pattern(node)
  handle_generic_node(super, :match_pattern)
end

#on_match_var(node) ⇒ Object



127
128
129
# File 'lib/ruby2js/filter/camelCase.rb', line 127

def on_match_var(node)
  handle_generic_node(super, :match_var)
end

#on_optarg(node) ⇒ Object



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

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

#on_send(node) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/ruby2js/filter/camelCase.rb', line 44

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



131
132
133
# File 'lib/ruby2js/filter/camelCase.rb', line 131

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