ruby2js 1.0.0 → 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +8 -8
- data/lib/ruby2js.rb +2 -2
- data/lib/ruby2js/converter.rb +68 -474
- data/lib/ruby2js/converter/andasgn.rb +12 -0
- data/lib/ruby2js/converter/arg.rb +14 -0
- data/lib/ruby2js/converter/args.rb +13 -0
- data/lib/ruby2js/converter/array.rb +33 -0
- data/lib/ruby2js/converter/begin.rb +13 -0
- data/lib/ruby2js/converter/block.rb +16 -0
- data/lib/ruby2js/converter/blockpass.rb +11 -0
- data/lib/ruby2js/converter/boolean.rb +11 -0
- data/lib/ruby2js/converter/break.rb +12 -0
- data/lib/ruby2js/converter/case.rb +23 -0
- data/lib/ruby2js/converter/casgn.rb +16 -0
- data/lib/ruby2js/converter/class.rb +75 -0
- data/lib/ruby2js/converter/const.rb +10 -0
- data/lib/ruby2js/converter/def.rb +91 -0
- data/lib/ruby2js/converter/defined.rb +15 -0
- data/lib/ruby2js/converter/dstr.rb +16 -0
- data/lib/ruby2js/converter/for.rb +19 -0
- data/lib/ruby2js/converter/hash.rb +24 -0
- data/lib/ruby2js/converter/if.rb +34 -0
- data/lib/ruby2js/converter/ivar.rb +10 -0
- data/lib/ruby2js/converter/ivasgn.rb +11 -0
- data/lib/ruby2js/converter/kwbegin.rb +44 -0
- data/lib/ruby2js/converter/literal.rb +12 -0
- data/lib/ruby2js/converter/logical.rb +43 -0
- data/lib/ruby2js/converter/masgn.rb +20 -0
- data/lib/ruby2js/converter/next.rb +12 -0
- data/lib/ruby2js/converter/nil.rb +10 -0
- data/lib/ruby2js/converter/opasgn.rb +20 -0
- data/lib/ruby2js/converter/orasgn.rb +12 -0
- data/lib/ruby2js/converter/regexp.rb +21 -0
- data/lib/ruby2js/converter/return.rb +15 -0
- data/lib/ruby2js/converter/self.rb +10 -0
- data/lib/ruby2js/converter/send.rb +98 -0
- data/lib/ruby2js/converter/sym.rb +10 -0
- data/lib/ruby2js/converter/undef.rb +12 -0
- data/lib/ruby2js/converter/until.rb +12 -0
- data/lib/ruby2js/converter/untilpost.rb +13 -0
- data/lib/ruby2js/converter/var.rb +11 -0
- data/lib/ruby2js/converter/vasgn.rb +41 -0
- data/lib/ruby2js/converter/while.rb +12 -0
- data/lib/ruby2js/converter/whilepost.rb +14 -0
- data/lib/ruby2js/converter/xstr.rb +16 -0
- data/lib/ruby2js/filter/angularrb.rb +1 -1
- data/lib/ruby2js/filter/jquery.rb +95 -1
- data/lib/ruby2js/version.rb +1 -1
- data/ruby2js.gemspec +3 -3
- metadata +44 -2
@@ -0,0 +1,34 @@
|
|
1
|
+
module Ruby2JS
|
2
|
+
class Converter
|
3
|
+
|
4
|
+
# (if
|
5
|
+
# (true)
|
6
|
+
# (...)
|
7
|
+
# (...))
|
8
|
+
|
9
|
+
handle :if do |condition, then_block, else_block|
|
10
|
+
if else_block and not then_block
|
11
|
+
return parse(s(:if, s(:send, condition, :!), else_block, nil), @state)
|
12
|
+
end
|
13
|
+
|
14
|
+
if @state == :statement
|
15
|
+
output = "if (#{ parse condition }) {#@nl#{ scope then_block }#@nl}"
|
16
|
+
while else_block and else_block.type == :if
|
17
|
+
condition, then_block, else_block = else_block.children
|
18
|
+
output << " else if (#{ parse condition }) " +
|
19
|
+
"{#@nl#{ scope then_block }#@nl}"
|
20
|
+
end
|
21
|
+
output << " else {#@nl#{ scope else_block }#@nl}" if else_block
|
22
|
+
|
23
|
+
# use short form when appropriate
|
24
|
+
unless output.length > 72 or else_block or then_block.type == :begin
|
25
|
+
output = "if (#{ parse condition }) #{ scope then_block }"
|
26
|
+
end
|
27
|
+
|
28
|
+
output
|
29
|
+
else
|
30
|
+
"(#{ parse condition } ? #{ parse then_block } : #{ parse else_block })"
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,44 @@
|
|
1
|
+
module Ruby2JS
|
2
|
+
class Converter
|
3
|
+
|
4
|
+
# (kwbegin
|
5
|
+
# (ensure
|
6
|
+
# (rescue
|
7
|
+
# (send nil :a)
|
8
|
+
# (resbody nil nil
|
9
|
+
# (send nil :b)) nil)
|
10
|
+
# (send nil :c)))
|
11
|
+
|
12
|
+
handle :kwbegin do |*children|
|
13
|
+
block = children.first
|
14
|
+
if block.type == :ensure
|
15
|
+
block, finally = block.children
|
16
|
+
else
|
17
|
+
finally = nil
|
18
|
+
end
|
19
|
+
|
20
|
+
if block and block.type == :rescue
|
21
|
+
body, recover, otherwise = block.children
|
22
|
+
raise NotImplementedError, "block else" if otherwise
|
23
|
+
exception, name, recovery = recover.children
|
24
|
+
raise NotImplementedError, parse(exception) if exception
|
25
|
+
else
|
26
|
+
body = block
|
27
|
+
end
|
28
|
+
|
29
|
+
output = "try {#@nl#{ parse body }#@nl}"
|
30
|
+
|
31
|
+
if recovery
|
32
|
+
output += " catch (#{ parse name }) {#@nl#{ parse recovery }#@nl}"
|
33
|
+
end
|
34
|
+
|
35
|
+
output += " finally {#@nl#{ parse finally }#@nl}" if finally
|
36
|
+
|
37
|
+
if recovery or finally
|
38
|
+
output
|
39
|
+
else
|
40
|
+
parse s(:begin, *children)
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
@@ -0,0 +1,43 @@
|
|
1
|
+
module Ruby2JS
|
2
|
+
class Converter
|
3
|
+
|
4
|
+
# (and
|
5
|
+
# (...)
|
6
|
+
# (...))
|
7
|
+
|
8
|
+
# (or
|
9
|
+
# (...)
|
10
|
+
# (...))
|
11
|
+
|
12
|
+
# (not
|
13
|
+
# (...))
|
14
|
+
|
15
|
+
handle :and, :or, :not do |left, right=nil|
|
16
|
+
type = @ast.type
|
17
|
+
op_index = operator_index type
|
18
|
+
|
19
|
+
left = left.children.first if left and left.type == :begin
|
20
|
+
lgroup = LOGICAL.include?( left.type ) &&
|
21
|
+
op_index <= operator_index( left.type )
|
22
|
+
left = parse left
|
23
|
+
left = "(#{ left })" if lgroup
|
24
|
+
|
25
|
+
if right
|
26
|
+
right = right.children.first if right.type == :begin
|
27
|
+
rgroup = LOGICAL.include?( right.type ) &&
|
28
|
+
op_index <= operator_index( right.type )
|
29
|
+
right = parse right
|
30
|
+
right = "(#{ right })" if rgroup
|
31
|
+
end
|
32
|
+
|
33
|
+
case type
|
34
|
+
when :and
|
35
|
+
"#{ left } && #{ right }"
|
36
|
+
when :or
|
37
|
+
"#{ left } || #{ right }"
|
38
|
+
else
|
39
|
+
"!#{ left }"
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
module Ruby2JS
|
2
|
+
class Converter
|
3
|
+
|
4
|
+
# (masgn
|
5
|
+
# (mlhs
|
6
|
+
# (lvasgn :a)
|
7
|
+
# (lvasgn :b))
|
8
|
+
# (array
|
9
|
+
# (int 1)
|
10
|
+
# (int 2)))
|
11
|
+
|
12
|
+
handle :masgn do |lhs, rhs|
|
13
|
+
block = []
|
14
|
+
lhs.children.zip rhs.children.zip do |var, val|
|
15
|
+
block << s(var.type, *var.children, *val)
|
16
|
+
end
|
17
|
+
parse s(:begin, *block), @state
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
module Ruby2JS
|
2
|
+
class Converter
|
3
|
+
|
4
|
+
# (op-asgn
|
5
|
+
# (lvasgn :a) :+
|
6
|
+
# (int 1))
|
7
|
+
|
8
|
+
handle :op_asgn do |var, op, value|
|
9
|
+
if [:+, :-].include?(op) and value.type==:int and value.children==[1]
|
10
|
+
if @state == :statement
|
11
|
+
"#{ parse var }#{ op }#{ op }"
|
12
|
+
else
|
13
|
+
"#{ op }#{ op }#{ parse var }"
|
14
|
+
end
|
15
|
+
else
|
16
|
+
"#{ parse var } #{ op }= #{ parse value }"
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
module Ruby2JS
|
2
|
+
class Converter
|
3
|
+
|
4
|
+
# (regexp
|
5
|
+
# (str "x")
|
6
|
+
# (regopt :i))
|
7
|
+
|
8
|
+
handle :regexp do |str, opt|
|
9
|
+
str = str.children.first
|
10
|
+
if str.include? '/'
|
11
|
+
if opt.children.empty?
|
12
|
+
"new RegExp(#{ str.inspect })"
|
13
|
+
else
|
14
|
+
"new RegExp(#{ str.inspect }, #{ opt.children.join.inspect})"
|
15
|
+
end
|
16
|
+
else
|
17
|
+
"/#{ str }/#{ opt.children.join }"
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,98 @@
|
|
1
|
+
module Ruby2JS
|
2
|
+
class Converter
|
3
|
+
|
4
|
+
# (send nil :puts
|
5
|
+
# (int 1))
|
6
|
+
|
7
|
+
# (attr nil :puts
|
8
|
+
# (int 1))
|
9
|
+
|
10
|
+
handle :send, :attr do |receiver, method, *args|
|
11
|
+
ast = @ast
|
12
|
+
|
13
|
+
if method =~ /\w[!?]$/
|
14
|
+
raise NotImplementedError, "invalid method name #{ method }"
|
15
|
+
end
|
16
|
+
|
17
|
+
if method == :new and receiver and receiver.children == [nil, :Proc]
|
18
|
+
return parse args.first
|
19
|
+
elsif not receiver and [:lambda, :proc].include? method
|
20
|
+
return parse args.first
|
21
|
+
end
|
22
|
+
|
23
|
+
op_index = operator_index method
|
24
|
+
if op_index != -1
|
25
|
+
target = args.first
|
26
|
+
target = target.children.first if target and target.type == :begin
|
27
|
+
receiver = receiver.children.first if receiver.type == :begin
|
28
|
+
end
|
29
|
+
|
30
|
+
group_receiver = receiver.type == :send && op_index <= operator_index( receiver.children[1] ) if receiver
|
31
|
+
group_target = target.type == :send && op_index <= operator_index( target.children[1] ) if target
|
32
|
+
|
33
|
+
if method == :!
|
34
|
+
if receiver.type == :defined?
|
35
|
+
parse s(:undefined?, *receiver.children)
|
36
|
+
else
|
37
|
+
group_receiver ||= (receiver.type != :send && receiver.children.length > 1)
|
38
|
+
"!#{ group_receiver ? group(receiver) : parse(receiver) }"
|
39
|
+
end
|
40
|
+
|
41
|
+
elsif method == :[]
|
42
|
+
"#{ parse receiver }[#{ args.map {|arg| parse arg}.join(', ') }]"
|
43
|
+
|
44
|
+
elsif method == :[]=
|
45
|
+
"#{ parse receiver }[#{ args[0..-2].map {|arg| parse arg}.join(', ') }] = #{ parse args[-1] }"
|
46
|
+
|
47
|
+
elsif [:-@, :+@, :~].include? method
|
48
|
+
"#{ method.to_s[0] }#{ parse receiver }"
|
49
|
+
|
50
|
+
elsif method == :=~
|
51
|
+
"#{ parse args.first }.test(#{ parse receiver })"
|
52
|
+
|
53
|
+
elsif method == :!~
|
54
|
+
"!#{ parse args.first }.test(#{ parse receiver })"
|
55
|
+
|
56
|
+
elsif OPERATORS.flatten.include? method
|
57
|
+
"#{ group_receiver ? group(receiver) : parse(receiver) } #{ method } #{ group_target ? group(target) : parse(target) }"
|
58
|
+
|
59
|
+
elsif method =~ /=$/
|
60
|
+
"#{ parse receiver }#{ '.' if receiver }#{ method.to_s.sub(/=$/, ' =') } #{ parse args.first }"
|
61
|
+
|
62
|
+
elsif method == :new and receiver
|
63
|
+
args = args.map {|a| parse a}.join(', ')
|
64
|
+
if args.length > 0 or is_method?(ast)
|
65
|
+
"new #{ parse receiver }(#{ args })"
|
66
|
+
else
|
67
|
+
"new #{ parse receiver }"
|
68
|
+
end
|
69
|
+
|
70
|
+
elsif method == :raise and receiver == nil
|
71
|
+
if args.length == 1
|
72
|
+
"throw #{ parse args.first }"
|
73
|
+
else
|
74
|
+
"throw new #{ parse args.first }(#{ parse args[1] })"
|
75
|
+
end
|
76
|
+
|
77
|
+
elsif method == :typeof and receiver == nil
|
78
|
+
"typeof #{ parse args.first }"
|
79
|
+
|
80
|
+
else
|
81
|
+
if args.length == 0 and not is_method?(ast)
|
82
|
+
if receiver
|
83
|
+
"#{ parse receiver }.#{ method }"
|
84
|
+
else
|
85
|
+
parse s(:lvasgn, method), @state
|
86
|
+
end
|
87
|
+
elsif args.length > 0 and args.last.type == :splat
|
88
|
+
parse s(:send, s(:attr, receiver, method), :apply, receiver,
|
89
|
+
s(:send, s(:array, *args[0..-2]), :concat,
|
90
|
+
args[-1].children.first))
|
91
|
+
else
|
92
|
+
args = args.map {|a| parse a}.join(', ')
|
93
|
+
"#{ parse receiver }#{ '.' if receiver }#{ method }(#{ args })"
|
94
|
+
end
|
95
|
+
end
|
96
|
+
end
|
97
|
+
end
|
98
|
+
end
|