Skip to content

Commit 358b2f5

Browse files
committed
further progress towards open classes
1 parent 95c6c0a commit 358b2f5

File tree

7 files changed

+29
-16
lines changed

7 files changed

+29
-16
lines changed

lib/ruby2js/converter/assign.rb

+4-4
Original file line numberDiff line numberDiff line change
@@ -93,10 +93,10 @@ class Converter
9393
pair.updated(:defm, [nil, *pair.children[1..-1]]))
9494
})
9595

96-
elsif modname.type == :lvar and not es2015
97-
s(:for, s(:lvasgn, :$_), modname,
98-
s(:send, target, :[]=,
99-
s(:lvar, :$_), s(:send, modname, :[], s(:lvar, :$_))))
96+
elsif modname.type == :lvar and not es2015
97+
s(:for, s(:lvasgn, :$_), modname,
98+
s(:send, target, :[]=,
99+
s(:lvar, :$_), s(:send, modname, :[], s(:lvar, :$_))))
100100

101101
else
102102
if es2017

lib/ruby2js/converter/class.rb

+3-3
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ class Converter
4949
end
5050

5151
body.compact!
52-
visible = {}
52+
visible = @namespace.getOwnProps
5353
body.map! do |m|
5454
if \
5555
@ast.type == :class_module and m.type == :defs and
@@ -68,15 +68,15 @@ class Converter
6868
sym = :"#{m.children.first.to_s[0..-2]}"
6969
s(:prop, s(:attr, name, :prototype), sym =>
7070
{enumerable: s(:true), configurable: s(:true),
71-
set: s(:block, s(:send, nil, :proc), *m.children[1..-1])})
71+
set: s(:defm, nil, *m.children[1..-1])})
7272
else
7373
visible[m.children[0]] = s(:self)
7474

7575
if not m.is_method?
7676
# property getter
7777
s(:prop, s(:attr, name, :prototype), m.children.first =>
7878
{enumerable: s(:true), configurable: s(:true),
79-
get: s(:block, s(:send, nil, :proc), m.children[1],
79+
get: s(:defm, nil, m.children[1],
8080
m.updated(:autoreturn, m.children[2..-1]))})
8181
else
8282
# method: add to prototype

lib/ruby2js/converter/class2.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ class Converter
4040
begin
4141
class_name, @class_name = @class_name, name
4242
class_parent, @class_parent = @class_parent, inheritance
43-
@rbstack.push({})
43+
@rbstack.push(@namespace.getOwnProps)
4444
constructor = []
4545
index = 0
4646

@@ -350,7 +350,7 @@ class Converter
350350
ensure
351351
@class_name = class_name
352352
@class_parent = class_parent
353-
@rbstack.pop
353+
@namespace.defineProps @rbstack.pop
354354
end
355355
end
356356
end

lib/ruby2js/converter/hash.rb

+4-1
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ class Converter
7373
if right.type == :hash
7474
right.children.each do |pair|
7575
next unless Parser::AST::Node === pair.children.last
76-
if [:block, :def, :async].include? pair.children.last.type
76+
if %i[block def defm async].include? pair.children.last.type
7777
if @comments[pair.children.last]
7878
(puts ''; singleton = false) if singleton
7979
comments(pair.children.last).each do |comment|
@@ -130,6 +130,9 @@ class Converter
130130
left.children.last == right.children.last
131131
then
132132
parse right
133+
elsif right.type == :defm and %i[sym str].include? left.type and es2015
134+
@prop = left.children.first.to_s
135+
parse right
133136
else
134137
if not [:str, :sym].include? left.type and es2015
135138
put '['

lib/ruby2js/namespace.rb

+6-2
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,12 @@ def enter(name)
2222
previous
2323
end
2424

25-
def find(name)
26-
@seen[active + resolve(name)]
25+
def getOwnProps(name = nil)
26+
@seen[active + resolve(name)] || {}
27+
end
28+
29+
def defineProps(props)
30+
@seen[active].merge! props || {}
2731
end
2832

2933
def leave()

spec/es2015_spec.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -496,7 +496,7 @@ def to_js_fn(string)
496496
describe 'module extensions' do
497497
it 'should handle methods' do
498498
to_js('++module M; def m(); end; end').
499-
must_equal 'Object.assign(M, {m: function() {}})'
499+
must_equal 'Object.assign(M, {m() {}})'
500500
end
501501
end
502502

spec/namespace_spec.rb

+9-3
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,21 @@ def to_js(string)
1111
it "should extend modules" do
1212
to_js( 'module M; def f(); end; end;' +
1313
'module M; def g(); end; end').
14-
must_equal('const M = {f: function() {}}; ' +
14+
must_equal('const M = {f() {}}; ' +
1515
'M.g = function() {}');
1616
end
1717

1818
it "should extend nested modules" do
1919
to_js( 'module M; module N; def f(); end; end; end;' +
2020
'module M::N; def g(); end; end').
21-
must_equal('const M = {N: {f: function() {}}}; ' +
21+
must_equal('const M = {N: {f() {}}}; ' +
2222
'M.N.g = function() {}');
2323
end
2424

2525
it "should extend nested modules with getter" do
2626
to_js( 'module M; module N; def f(); end; end; end;' +
2727
'module M::N; def g; end; end').
28-
must_equal('const M = {N: {f: function() {}}}; ' +
28+
must_equal('const M = {N: {f() {}}}; ' +
2929
'Object.defineProperties(M.N, ' +
3030
'Object.getOwnPropertyDescriptors({get g() {}}))');
3131
end
@@ -46,5 +46,11 @@ def to_js(string)
4646
'Object.defineProperty(M.N.prototype, "g", ' +
4747
'{enumerable: true, configurable: true, get() {}})');
4848
end
49+
50+
it "should bind references to methods defined in original class" do
51+
to_js( 'class C; def f(); end; end' +
52+
'class C; def g; f; end; end').
53+
must_include 'return this.f.bind(this)'
54+
end
4955
end
5056
end

0 commit comments

Comments
 (0)