ruby2js 1.5.0 → 1.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +8 -8
- data/lib/ruby2js.rb +11 -4
- data/lib/ruby2js/converter.rb +1 -0
- data/lib/ruby2js/converter/class.rb +56 -28
- data/lib/ruby2js/converter/return.rb +1 -1
- data/lib/ruby2js/converter/super.rb +33 -0
- data/lib/ruby2js/filter/angularrb.rb +43 -23
- data/lib/ruby2js/version.rb +1 -1
- data/ruby2js.gemspec +3 -3
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
---
|
2
2
|
!binary "U0hBMQ==":
|
3
3
|
metadata.gz: !binary |-
|
4
|
-
|
4
|
+
NzY5MTI2YzczYzk3NTUwMjczNzk1MzBiOTQ2Yjg4NTE1OGFhN2VjNQ==
|
5
5
|
data.tar.gz: !binary |-
|
6
|
-
|
6
|
+
YjVlMWM3ZWI3ODZkNTdkNmE2OTQ0NzhkNzJjMGI4NmQwNzZhOTcxYQ==
|
7
7
|
!binary "U0hBNTEy":
|
8
8
|
metadata.gz: !binary |-
|
9
|
-
|
10
|
-
|
11
|
-
|
9
|
+
NmJhNDY0ZmNlMjAwNzk4NDUxMTViYjJjNzViMjRhYzQxZTI4YTI4NTg3MWMw
|
10
|
+
MzUxOGU0NTVmZGI5NDcwN2VlMTQ4MmNlOGQzM2QwZmExNmUxYTliOWI2YTdi
|
11
|
+
MzlkOTgxNDM1YmFhNDRkMDAzZjdkNThiZjc4NzQ5NDFmODlkOWI=
|
12
12
|
data.tar.gz: !binary |-
|
13
|
-
|
14
|
-
|
15
|
-
|
13
|
+
OGJlNzI1NTBjYzM0ODkwMjAxZTY0NmIwMGYzYTMxMzJhYjA3NjcyMTQ0MjU1
|
14
|
+
MzdiOWMzNDQxOGZlNmRjMmIyYWYzZmU5YjFhZDY0NGQzMzI0MjU3NjIwYzMx
|
15
|
+
OTNlODVkZWM0YmVmNzhiMWFkZWU3NGY4NmZiNGFjNmNmNGQ3YWM=
|
data/lib/ruby2js.rb
CHANGED
@@ -15,10 +15,17 @@ module Ruby2JS
|
|
15
15
|
class Processor < Parser::AST::Processor
|
16
16
|
BINARY_OPERATORS = Converter::OPERATORS[2..-1].flatten
|
17
17
|
|
18
|
-
|
19
|
-
|
20
|
-
end
|
21
|
-
|
18
|
+
# handle all of the 'invented' ast types
|
19
|
+
def on_attr(node); on_send(node); end
|
20
|
+
def on_autoreturn(node); on_return(node); end
|
21
|
+
def on_constructor(node); on_def(node); end
|
22
|
+
def on_method(node); on_send(node); end
|
23
|
+
def on_prop(node); on_array(node); end
|
24
|
+
def on_prototype(node); on_begin(node); end
|
25
|
+
def on_sendw(node); on_send(node); end
|
26
|
+
def on_undefined?(node); on_defined?(node); end
|
27
|
+
|
28
|
+
# convert map(&:symbol) to a block
|
22
29
|
def on_send(node)
|
23
30
|
if node.children.length > 2 and node.children.last.type == :block_pass
|
24
31
|
method = node.children.last.children.first.children.last
|
data/lib/ruby2js/converter.rb
CHANGED
@@ -148,6 +148,7 @@ require 'ruby2js/converter/regexp'
|
|
148
148
|
require 'ruby2js/converter/return'
|
149
149
|
require 'ruby2js/converter/self'
|
150
150
|
require 'ruby2js/converter/send'
|
151
|
+
require 'ruby2js/converter/super'
|
151
152
|
require 'ruby2js/converter/sym'
|
152
153
|
require 'ruby2js/converter/undef'
|
153
154
|
require 'ruby2js/converter/until'
|
@@ -6,7 +6,7 @@ module Ruby2JS
|
|
6
6
|
# (const nil :B)
|
7
7
|
# (...)
|
8
8
|
|
9
|
-
# NOTE:
|
9
|
+
# NOTE: :prop and :method macros are defined at the bottom of this file
|
10
10
|
|
11
11
|
handle :class do |name, inheritance, *body|
|
12
12
|
init = s(:def, :initialize, s(:args))
|
@@ -28,17 +28,22 @@ module Ruby2JS
|
|
28
28
|
s(:prop, s(:attr, name, :prototype), sym,
|
29
29
|
enumerable: s(:true), configurable: s(:true),
|
30
30
|
set: s(:block, s(:send, nil, :proc), *m.children[1..-1]))
|
31
|
-
elsif m.children[1].children.length == 0 and m.children.first !~ /!/
|
32
|
-
# property getter
|
33
|
-
s(:prop, s(:attr, name, :prototype), m.children.first,
|
34
|
-
enumerable: s(:true), configurable: s(:true),
|
35
|
-
get: s(:block, s(:send, nil, :proc), m.children[1],
|
36
|
-
s(:autoreturn, *m.children[2..-1])))
|
37
31
|
else
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
32
|
+
if m.children[1].children.length == 0 and
|
33
|
+
m.children.first !~ /!/ and m.loc and m.loc.name and
|
34
|
+
m.loc.name.source_buffer.source[m.loc.name.end_pos] != '('
|
35
|
+
|
36
|
+
# property getter
|
37
|
+
s(:prop, s(:attr, name, :prototype), m.children.first,
|
38
|
+
enumerable: s(:true), configurable: s(:true),
|
39
|
+
get: s(:block, s(:send, nil, :proc), m.children[1],
|
40
|
+
s(:autoreturn, *m.children[2..-1])))
|
41
|
+
else
|
42
|
+
# method: add to prototype
|
43
|
+
s(:method, s(:attr, name, :prototype),
|
44
|
+
:"#{m.children[0].to_s.chomp('!')}=",
|
45
|
+
s(:block, s(:send, nil, :proc), *m.children[1..-1]))
|
46
|
+
end
|
42
47
|
end
|
43
48
|
elsif m.type == :defs and m.children.first == s(:self)
|
44
49
|
# class method definition: add to prototype
|
@@ -117,51 +122,54 @@ module Ruby2JS
|
|
117
122
|
end
|
118
123
|
|
119
124
|
if inheritance
|
120
|
-
body.unshift s(:send, name, :prototype=,
|
125
|
+
body.unshift s(:send, name, :prototype=,
|
126
|
+
s(:send, s(:const, nil, :Object), :create, inheritance)),
|
127
|
+
s(:send, s(:attr, name, :prototype), :constructor=, name)
|
121
128
|
else
|
122
129
|
body.compact!
|
123
130
|
|
124
|
-
# look for
|
131
|
+
# look for first sequence of methods and properties
|
125
132
|
methods = 0
|
133
|
+
start = 0
|
126
134
|
body.each do |node|
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
elsif node.type != :prop
|
135
|
+
if [:method, :prop].include? node.type
|
136
|
+
methods += 1
|
137
|
+
elsif methods == 0
|
138
|
+
start += 1
|
139
|
+
else
|
133
140
|
break
|
134
141
|
end
|
135
|
-
methods += 1
|
136
142
|
end
|
137
143
|
|
138
|
-
# collapse sequence
|
139
|
-
if methods > 1 or (methods == 1 and body[
|
140
|
-
pairs = body[
|
141
|
-
if node.type == :
|
142
|
-
s(:pair, s(:str, node.children[1].to_s.chomp('=')),
|
144
|
+
# collapse sequence to a single assignment
|
145
|
+
if methods > 1 or (methods == 1 and body[start].type == :prop)
|
146
|
+
pairs = body[start...start+methods].map do |node|
|
147
|
+
if node.type == :method
|
148
|
+
s(:pair, s(:str, node.children[1].to_s.chomp('=')),
|
143
149
|
node.children[2])
|
144
150
|
else
|
145
151
|
s(:pair, s(:prop, node.children[1]), node.children[2])
|
146
152
|
end
|
147
153
|
end
|
148
|
-
body
|
149
|
-
|
154
|
+
body[start...start+methods] =
|
155
|
+
s(:send, name, :prototype=, s(:hash, *pairs))
|
150
156
|
end
|
151
157
|
end
|
152
158
|
|
153
159
|
# prepend constructor
|
154
|
-
body.unshift s(:
|
160
|
+
body.unshift s(:constructor, parse(name), *init.children[1..-1])
|
155
161
|
|
156
162
|
begin
|
157
163
|
# save class name
|
158
164
|
class_name, @class_name = @class_name, name
|
165
|
+
class_parent, @class_parent = @class_parent, inheritance
|
159
166
|
# inhibit ivar substitution within a class definition. See ivars.rb
|
160
167
|
ivars, self.ivars = self.ivars, nil
|
161
168
|
parse s(:begin, *body.compact)
|
162
169
|
ensure
|
163
170
|
self.ivars = ivars
|
164
171
|
@class_name = class_name
|
172
|
+
@class_parent = class_parent
|
165
173
|
end
|
166
174
|
end
|
167
175
|
|
@@ -171,5 +179,25 @@ module Ruby2JS
|
|
171
179
|
obj, s(:sym, prop), s(:hash,
|
172
180
|
*descriptor.map { |key, value| s(:pair, s(:sym, key), value) }))
|
173
181
|
end
|
182
|
+
|
183
|
+
# capture methods for use by super
|
184
|
+
handle :method do |*args|
|
185
|
+
begin
|
186
|
+
instance_method, @instance_method = @instance_method, @ast
|
187
|
+
parse s(:send, *args)
|
188
|
+
ensure
|
189
|
+
@instance_method = instance_method
|
190
|
+
end
|
191
|
+
end
|
192
|
+
|
193
|
+
# capture constructors for use by super
|
194
|
+
handle :constructor do |*args|
|
195
|
+
begin
|
196
|
+
instance_method, @instance_method = @instance_method, @ast
|
197
|
+
parse s(:def, *args)
|
198
|
+
ensure
|
199
|
+
@instance_method = instance_method
|
200
|
+
end
|
201
|
+
end
|
174
202
|
end
|
175
203
|
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
module Ruby2JS
|
2
|
+
class Converter
|
3
|
+
|
4
|
+
# (zsuper)
|
5
|
+
#
|
6
|
+
# (super ...)
|
7
|
+
|
8
|
+
handle :super, :zsuper do |*args|
|
9
|
+
unless @class_parent and @instance_method
|
10
|
+
raise NotImplementedError, "super outside of a method"
|
11
|
+
end
|
12
|
+
|
13
|
+
# what to call
|
14
|
+
if @instance_method.type == :method
|
15
|
+
method = ".prototype.#{ @instance_method.children[1].to_s.chomp('=') }"
|
16
|
+
else
|
17
|
+
method = ''
|
18
|
+
end
|
19
|
+
|
20
|
+
# what to pass
|
21
|
+
if @ast.type == :zsuper
|
22
|
+
if @instance_method.type == :method
|
23
|
+
args = @instance_method.children[2].children[1].children
|
24
|
+
else
|
25
|
+
args = @instance_method.children[1].children
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
args = [s(:self), *args].map {|arg| parse arg}.join(', ')
|
30
|
+
"#{ parse @class_parent }#{ method }.call(#{ args })"
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
@@ -400,7 +400,7 @@ module Ruby2JS
|
|
400
400
|
# ...
|
401
401
|
# end
|
402
402
|
def ng_observe(node)
|
403
|
-
if @ngContext == :
|
403
|
+
if @ngContext == :controller and @ngScope
|
404
404
|
call = node.children[0]
|
405
405
|
expression = call.children[2]
|
406
406
|
call = s(:send, expression.children[0], :$observe,
|
@@ -461,6 +461,28 @@ module Ruby2JS
|
|
461
461
|
# map well known method names to the appropriate service
|
462
462
|
scope, method = NG_METHOD_MAP[node.children[1]]
|
463
463
|
|
464
|
+
if node.children[0..1] == [nil, :interpolate] and @ngScope
|
465
|
+
@ngClassUses << :$interpolate
|
466
|
+
if node.children.length > 3 and node.children[3].type == :nil
|
467
|
+
return process node.updated nil, [nil, :$interpolate,
|
468
|
+
node.children[2]]
|
469
|
+
else
|
470
|
+
return process s(:send, s(:send, nil, :$interpolate,
|
471
|
+
*node.children[2..-1]), nil, @ngScope)
|
472
|
+
end
|
473
|
+
|
474
|
+
elsif node.children[0..1] == [nil, :compile] and @ngScope
|
475
|
+
|
476
|
+
@ngClassUses << :$compile
|
477
|
+
if node.children.length > 3 and node.children.last.type == :nil
|
478
|
+
return process node.updated nil, [nil, :$compile,
|
479
|
+
*node.children[2..-2]]
|
480
|
+
else
|
481
|
+
return process s(:send, s(:send, nil, :$compile,
|
482
|
+
*node.children[2..-1]), nil, @ngScope)
|
483
|
+
end
|
484
|
+
end
|
485
|
+
|
464
486
|
return super unless node.children.first == nil and method
|
465
487
|
|
466
488
|
scope = s(:gvar, scope) if scope
|
@@ -483,32 +505,28 @@ module Ruby2JS
|
|
483
505
|
s(:send, @ngApp, :config, s(:array, s(:str, service.to_s), s(:block,
|
484
506
|
s(:send, nil, :proc), s(:args, s(:arg, service)), node)))
|
485
507
|
|
486
|
-
|
487
|
-
|
488
|
-
|
489
|
-
|
490
|
-
if node.children.length == 3
|
491
|
-
process node.updated nil, [nil, :$interpolate, node.children[2]]
|
492
|
-
else
|
493
|
-
process s(:send, s(:send, nil, :$interpolate, node.children[2]),
|
494
|
-
nil, node.children[3])
|
495
|
-
end
|
496
|
-
|
497
|
-
elsif node.children[0..1] == [nil, :compile]
|
508
|
+
else
|
509
|
+
super
|
510
|
+
end
|
511
|
+
end
|
498
512
|
|
499
|
-
|
500
|
-
|
501
|
-
|
502
|
-
|
503
|
-
|
504
|
-
|
513
|
+
# convert instance method definitions in controllers to $scope
|
514
|
+
def on_pair(node)
|
515
|
+
if @ngContext == :directive and node.children[0] == s(:sym, :link)
|
516
|
+
begin
|
517
|
+
ngScope = @ngScope
|
518
|
+
if node.children[1].type == :block
|
519
|
+
args = node.children[1].children[1]
|
520
|
+
if args.children.length > 0
|
521
|
+
@ngScope = s(:lvar, args.children[0].children[0])
|
522
|
+
@ngContext = :controller
|
523
|
+
end
|
505
524
|
end
|
506
|
-
|
507
|
-
|
508
|
-
else
|
509
525
|
super
|
526
|
+
ensure
|
527
|
+
@ngScope = ngScope
|
528
|
+
@ngContext = :directive
|
510
529
|
end
|
511
|
-
|
512
530
|
else
|
513
531
|
super
|
514
532
|
end
|
@@ -524,6 +542,8 @@ module Ruby2JS
|
|
524
542
|
end
|
525
543
|
|
526
544
|
def on_gvar(node)
|
545
|
+
return @ngScope if node.children.first == :$scope and @ngScope
|
546
|
+
|
527
547
|
if @ngClassUses
|
528
548
|
@ngClassUses << node.children.first
|
529
549
|
end
|
data/lib/ruby2js/version.rb
CHANGED
data/ruby2js.gemspec
CHANGED
@@ -2,14 +2,14 @@
|
|
2
2
|
|
3
3
|
Gem::Specification.new do |s|
|
4
4
|
s.name = "ruby2js"
|
5
|
-
s.version = "1.
|
5
|
+
s.version = "1.6.0"
|
6
6
|
|
7
7
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
8
8
|
s.authors = ["Sam Ruby"]
|
9
|
-
s.date = "2014-
|
9
|
+
s.date = "2014-02-26"
|
10
10
|
s.description = " The base package maps Ruby syntax to JavaScript semantics.\n Filters may be provided to add Ruby-specific or framework specific\n behavior.\n"
|
11
11
|
s.email = "rubys@intertwingly.net"
|
12
|
-
s.files = ["ruby2js.gemspec", "README.md", "lib/ruby2js", "lib/ruby2js/rails.rb", "lib/ruby2js/version.rb", "lib/ruby2js/converter", "lib/ruby2js/converter/kwbegin.rb", "lib/ruby2js/converter/const.rb", "lib/ruby2js/converter/return.rb", "lib/ruby2js/converter/prototype.rb", "lib/ruby2js/converter/opasgn.rb", "lib/ruby2js/converter/xstr.rb", "lib/ruby2js/converter/args.rb", "lib/ruby2js/converter/defs.rb", "lib/ruby2js/converter/literal.rb", "lib/ruby2js/converter/array.rb", "lib/ruby2js/converter/if.rb", "lib/ruby2js/converter/nil.rb", "lib/ruby2js/converter/logical.rb", "lib/ruby2js/converter/next.rb", "lib/ruby2js/converter/while.rb", "lib/ruby2js/converter/whilepost.rb", "lib/ruby2js/converter/arg.rb", "lib/ruby2js/converter/case.rb", "lib/ruby2js/converter/break.rb", "lib/ruby2js/converter/hash.rb", "lib/ruby2js/converter/for.rb", "lib/ruby2js/converter/boolean.rb", "lib/ruby2js/converter/module.rb", "lib/ruby2js/converter/var.rb", "lib/ruby2js/converter/undef.rb", "lib/ruby2js/converter/blockpass.rb", "lib/ruby2js/converter/until.rb", "lib/ruby2js/converter/regexp.rb", "lib/ruby2js/converter/untilpost.rb", "lib/ruby2js/converter/masgn.rb", "lib/ruby2js/converter/cvasgn.rb", "lib/ruby2js/converter/block.rb", "lib/ruby2js/converter/ivar.rb", "lib/ruby2js/converter/send.rb", "lib/ruby2js/converter/vasgn.rb", "lib/ruby2js/converter/defined.rb", "lib/ruby2js/converter/def.rb", "lib/ruby2js/converter/sym.rb", "lib/ruby2js/converter/cvar.rb", "lib/ruby2js/converter/ivasgn.rb", "lib/ruby2js/converter/casgn.rb", "lib/ruby2js/converter/self.rb", "lib/ruby2js/converter/begin.rb", "lib/ruby2js/converter/dstr.rb", "lib/ruby2js/converter/class.rb", "lib/ruby2js/cgi.rb", "lib/ruby2js/converter.rb", "lib/ruby2js/filter", "lib/ruby2js/filter/return.rb", "lib/ruby2js/filter/strict.rb", "lib/ruby2js/filter/angularrb.rb", "lib/ruby2js/filter/underscore.rb", "lib/ruby2js/filter/angular-resource.rb", "lib/ruby2js/filter/functions.rb", "lib/ruby2js/filter/jquery.rb", "lib/ruby2js/filter/angular-route.rb", "lib/ruby2js/sinatra.rb", "lib/ruby2js.rb"]
|
12
|
+
s.files = ["ruby2js.gemspec", "README.md", "lib/ruby2js", "lib/ruby2js/rails.rb", "lib/ruby2js/version.rb", "lib/ruby2js/converter", "lib/ruby2js/converter/kwbegin.rb", "lib/ruby2js/converter/const.rb", "lib/ruby2js/converter/return.rb", "lib/ruby2js/converter/prototype.rb", "lib/ruby2js/converter/opasgn.rb", "lib/ruby2js/converter/xstr.rb", "lib/ruby2js/converter/args.rb", "lib/ruby2js/converter/defs.rb", "lib/ruby2js/converter/literal.rb", "lib/ruby2js/converter/array.rb", "lib/ruby2js/converter/if.rb", "lib/ruby2js/converter/nil.rb", "lib/ruby2js/converter/logical.rb", "lib/ruby2js/converter/next.rb", "lib/ruby2js/converter/while.rb", "lib/ruby2js/converter/whilepost.rb", "lib/ruby2js/converter/arg.rb", "lib/ruby2js/converter/case.rb", "lib/ruby2js/converter/break.rb", "lib/ruby2js/converter/hash.rb", "lib/ruby2js/converter/for.rb", "lib/ruby2js/converter/boolean.rb", "lib/ruby2js/converter/module.rb", "lib/ruby2js/converter/var.rb", "lib/ruby2js/converter/undef.rb", "lib/ruby2js/converter/blockpass.rb", "lib/ruby2js/converter/until.rb", "lib/ruby2js/converter/regexp.rb", "lib/ruby2js/converter/untilpost.rb", "lib/ruby2js/converter/masgn.rb", "lib/ruby2js/converter/cvasgn.rb", "lib/ruby2js/converter/block.rb", "lib/ruby2js/converter/super.rb", "lib/ruby2js/converter/ivar.rb", "lib/ruby2js/converter/send.rb", "lib/ruby2js/converter/vasgn.rb", "lib/ruby2js/converter/defined.rb", "lib/ruby2js/converter/def.rb", "lib/ruby2js/converter/sym.rb", "lib/ruby2js/converter/cvar.rb", "lib/ruby2js/converter/ivasgn.rb", "lib/ruby2js/converter/casgn.rb", "lib/ruby2js/converter/self.rb", "lib/ruby2js/converter/begin.rb", "lib/ruby2js/converter/dstr.rb", "lib/ruby2js/converter/class.rb", "lib/ruby2js/cgi.rb", "lib/ruby2js/converter.rb", "lib/ruby2js/filter", "lib/ruby2js/filter/return.rb", "lib/ruby2js/filter/strict.rb", "lib/ruby2js/filter/angularrb.rb", "lib/ruby2js/filter/underscore.rb", "lib/ruby2js/filter/angular-resource.rb", "lib/ruby2js/filter/functions.rb", "lib/ruby2js/filter/jquery.rb", "lib/ruby2js/filter/angular-route.rb", "lib/ruby2js/sinatra.rb", "lib/ruby2js.rb"]
|
13
13
|
s.homepage = "http://github.com/rubys/ruby2js"
|
14
14
|
s.licenses = ["MIT"]
|
15
15
|
s.require_paths = ["lib"]
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ruby2js
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.6.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Sam Ruby
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-
|
11
|
+
date: 2014-02-26 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: parser
|
@@ -67,6 +67,7 @@ files:
|
|
67
67
|
- lib/ruby2js/converter/masgn.rb
|
68
68
|
- lib/ruby2js/converter/cvasgn.rb
|
69
69
|
- lib/ruby2js/converter/block.rb
|
70
|
+
- lib/ruby2js/converter/super.rb
|
70
71
|
- lib/ruby2js/converter/ivar.rb
|
71
72
|
- lib/ruby2js/converter/send.rb
|
72
73
|
- lib/ruby2js/converter/vasgn.rb
|