Skip to content

Commit e0b0f6a

Browse files
committed
handle modules with a single method
1 parent fd59c9c commit e0b0f6a

File tree

3 files changed

+6
-2
lines changed

3 files changed

+6
-2
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
* Support static method calls with blocks in es2015+
44
* Auto-bind instance methods referenced as properties within a class
55
* handle begin, if, and case as expressions
6+
* handle modules with exactly one method
67

78
# 3.6.1 / 2020-12-31
89

lib/ruby2js/converter/class.rb

+3-2
Original file line numberDiff line numberDiff line change
@@ -219,8 +219,9 @@ class Converter
219219

220220
# collapse sequence to a single assignment
221221
if \
222-
@ast.type != :class_extend and
223-
(methods > 1 or (methods == 1 and body[start].type == :prop))
222+
@ast.type == :class_module or
223+
(@ast.type == :class and
224+
(methods > 1 or (methods == 1 and body[start].type == :prop)))
224225
then
225226
pairs = body[start...start+methods].map do |node|
226227
if node.type == :method

spec/transliteration_spec.rb

+2
Original file line numberDiff line numberDiff line change
@@ -824,6 +824,8 @@ def to_js( string, opts={} )
824824
must_equal 'A = function() {var B = 1; return {B: B}}()'
825825
to_js( 'module A; def b; return 1; end; end' ).
826826
must_equal 'var A = {get b() {return 1}}'
827+
to_js( 'module A; def b(); return 1; end; end' ).
828+
must_equal 'var A = {b: function() {return 1}}'
827829
to_js( 'module A; class B; def initialize; @c=1; end; end; end' ).
828830
must_equal 'A = function() {function B() {this._c = 1}; return {B: B}}()'
829831
end

0 commit comments

Comments
 (0)