Skip to content

Commit 439fbae

Browse files
committed
extending modules
1 parent deba8de commit 439fbae

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

lib/ruby2js/converter/send.rb

+8-2
Original file line numberDiff line numberDiff line change
@@ -181,9 +181,15 @@ class Converter
181181
receiver.type == :send and
182182
receiver.children[1] == :+@ and
183183
Parser::AST::Node === receiver.children[0] and
184-
receiver.children[0].type == :class
184+
%i(class module).include? receiver.children[0].type
185185
then
186-
parse receiver.children[0].updated(:class_extend)
186+
if receiver.children[0].type == :class
187+
parse receiver.children[0].updated(:class_extend)
188+
else
189+
mod = receiver.children[0]
190+
parse s(:send, s(:const, nil, :Object), :assign,
191+
mod.children[0], mod.updated(nil, [nil, *mod.children[1..-1]]))
192+
end
187193
else
188194
put method.to_s[0]; parse receiver
189195
end

spec/es2015_spec.rb

+7
Original file line numberDiff line numberDiff line change
@@ -493,6 +493,13 @@ def to_js_fn(string)
493493
end
494494
end
495495

496+
describe 'module extensions' do
497+
it 'should handle methods' do
498+
to_js('++module M; def m(); end; end').
499+
must_equal 'Object.assign(M, {m: function() {}})'
500+
end
501+
end
502+
496503
describe 'keyword arguments' do
497504
it 'should handle keyword arguments in methods' do
498505
skip if RUBY_VERSION =~ /^(1\.9|2\.0)/

0 commit comments

Comments
 (0)