-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathcamelCase.rb
154 lines (124 loc) · 3.75 KB
/
camelCase.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
require 'ruby2js'
# Note care is taken to run all the filters first before camelCasing.
# This ensures that Ruby methods like each_pair can be mapped to
# JavaScript before camelcasing.
module Ruby2JS
module Filter
module CamelCase
include SEXP
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"
}
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
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
def on_csend(node)
on_send(node)
end
def on_attr(node)
on_send(node)
end
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
def on_def(node)
handle_generic_node(super, :def)
end
def on_optarg(node)
handle_generic_node(super, :optarg)
end
def on_kwoptarg(node)
handle_generic_node(super, :kwoptarg)
end
def on_lvar(node)
handle_generic_node(super, :lvar)
end
def on_ivar(node)
handle_generic_node(super, :ivar)
end
def on_cvar(node)
handle_generic_node(super, :cvar)
end
def on_arg(node)
handle_generic_node(super, :arg)
end
def on_kwarg(node)
handle_generic_node(super, :kwarg)
end
def on_lvasgn(node)
handle_generic_node(super, :lvasgn)
end
def on_ivasgn(node)
handle_generic_node(super, :ivasgn)
end
def on_cvasgn(node)
handle_generic_node(super, :cvasgn)
end
def on_match_pattern(node)
handle_generic_node(super, :match_pattern)
end
def on_match_var(node)
handle_generic_node(super, :match_var)
end
def on_sym(node)
handle_generic_node(super, :sym)
end
def on_assign(node)
S(:assign , node.children[0], *node.children[1..-1].map{ process _1 })
end
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
end
DEFAULTS.push CamelCase
end
end