@@ -614,7 +614,7 @@ def to_js( string, opts={} )
614
614
615
615
it "should parse class with attr_accessor" do
616
616
to_js ( 'class Person; attr_accessor :a; end' ) .
617
- must_equal 'function Person() {}; Person.prototype = { get a () {return this._a}, set a (a) {this._a = a}}'
617
+ must_equal 'function Person() {}; Object.defineProperty( Person.prototype, "a", {enumerable: true, configurable: true, get: function () {return this._a}, set: function (a) {this._a = a}}) '
618
618
end
619
619
620
620
it "should parse class with constructor" do
@@ -629,27 +629,27 @@ def to_js( string, opts={} )
629
629
630
630
it "should parse class with constructor and method" do
631
631
to_js ( 'class Person; def initialize(name); @name = name; end; def name; @name; end; end' ) .
632
- must_equal 'function Person(name) {this._name = name}; Person.prototype = { get name () {return this._name}}'
632
+ must_equal 'function Person(name) {this._name = name}; Object.defineProperty( Person.prototype, "name", {enumerable: true, configurable: true, get: function () {return this._name}}) '
633
633
end
634
634
635
635
it "should parse class with constructor and two methods" do
636
636
to_js ( 'class Person; def initialize(name); @name = name; end; def name; @name; end; def reset!; @name = nil; end; end' ) .
637
- must_equal 'function Person(name) {this._name = name}; Person.prototype = { get name () {return this._name}, reset: function() {this._name = null} }'
637
+ must_equal 'function Person(name) {this._name = name}; Object.defineProperty( Person.prototype, "name", {enumerable: true, configurable: true, get: function () {return this._name}}); Person.prototype. reset = function() {this._name = null}'
638
638
end
639
639
640
640
it "should parse class with constructor and methods with multiple arguments" do
641
641
to_js ( 'class Person; def initialize(name, surname); @name, @surname = name, surname; end; def full_name; @name + @surname; end; end' ) .
642
- must_equal 'function Person(name, surname) {this._name = name; this._surname = surname}; Person.prototype = { get full_name () {return this._name + this._surname}}'
642
+ must_equal 'function Person(name, surname) {this._name = name; this._surname = surname}; Object.defineProperty( Person.prototype, "full_name", {enumerable: true, configurable: true, get: function () {return this._name + this._surname}}) '
643
643
end
644
644
645
645
it "should collapse multiple methods in a class" do
646
646
to_js ( 'class C; def a; end; def b; end; end' ) .
647
- must_equal 'function C() {}; C.prototype = { get a () {}, get b () {}}'
647
+ must_equal 'function C() {}; Object.defineProperties( C.prototype, {a: {enumerable: true, configurable: true, get: function () {}}, b: {enumerable: true, configurable: true, get: function () {}}}) '
648
648
end
649
649
650
650
it "should collapse getters and setters in a class" do
651
651
to_js ( 'class C; def a; end; def a=(a); end; end' ) .
652
- must_equal 'function C() {}; C.prototype = { get a () {}, set a (a) {}}'
652
+ must_equal 'function C() {}; Object.defineProperty( C.prototype, "a", {enumerable: true, configurable: true, get: function () {}, set: function (a) {}}) '
653
653
end
654
654
655
655
it "should collapse properties" do
@@ -685,21 +685,21 @@ def to_js( string, opts={} )
685
685
must_equal 'function Person() {}; Person._count = 0; Person.prototype.offset = function(x) {return Person._count + x}'
686
686
687
687
to_js ( 'class Person; @@count=0; def count; @@count; end; end' ) .
688
- must_equal 'function Person() {}; Person._count = 0; Person.prototype = { get count () {return Person._count}}'
688
+ must_equal 'function Person() {}; Person._count = 0; Object.defineProperty( Person.prototype, "count", {enumerable: true, configurable: true, get: function () {return Person._count}}) '
689
689
690
690
to_js ( 'class Person; @@count=0; def count(); return @@count; end; end' ) .
691
691
must_equal 'function Person() {}; Person._count = 0; Person.prototype.count = function() {return Person._count}'
692
692
693
693
to_js ( 'class Person; def initialize(name); @name = name; end; def name; @name; end; @@count=0; def count; return @@count; end; end' ) .
694
- must_equal 'function Person(name) {this._name = name}; Person.prototype = { get name () {return this._name}}; Person._count = 0; Object.defineProperty(Person.prototype, "count", {enumerable: true, configurable: true, get: function() {return Person._count}})'
694
+ must_equal 'function Person(name) {this._name = name}; Object.defineProperty( Person.prototype, "name", {enumerable: true, configurable: true, get: function () {return this._name}}) ; Person._count = 0; Object.defineProperty(Person.prototype, "count", {enumerable: true, configurable: true, get: function() {return Person._count}})'
695
695
696
696
to_js ( 'class Person; def initialize(name); @name = name; end; def name; @name; end; @@count=0; def count(); return @@count; end; end' ) .
697
- must_equal 'function Person(name) {this._name = name}; Person.prototype = { get name () {return this._name}}; Person._count = 0; Person.prototype.count = function() {return Person._count}'
697
+ must_equal 'function Person(name) {this._name = name}; Object.defineProperty( Person.prototype, "name", {enumerable: true, configurable: true, get: function () {return this._name}}) ; Person._count = 0; Person.prototype.count = function() {return Person._count}'
698
698
end
699
699
700
700
it "should parse instance methods with class variables" do
701
701
to_js ( 'class Person; def count; @@count; end; end' ) .
702
- must_equal 'function Person() {}; Person.prototype = { get count () {return Person._count}}'
702
+ must_equal 'function Person() {}; Object.defineProperty( Person.prototype, "count", {enumerable: true, configurable: true, get: function () {return Person._count}}) '
703
703
end
704
704
705
705
it "should parse class methods with class variables" do
@@ -762,18 +762,28 @@ def to_js( string, opts={} )
762
762
763
763
it "should prefix intra-method calls with 'this.'" do
764
764
to_js ( 'class C; def m1; end; def m2; m1; end; end' ) .
765
- must_equal 'function C() {}; C.prototype = ' +
766
- '{get m1() {}, get m2() {return this.m1}}'
765
+ must_equal 'function C() {}; Object.defineProperties(C.prototype, ' +
766
+ '{m1: {enumerable: true, configurable: true, get: function() {}}, ' +
767
+ 'm2: {enumerable: true, configurable: true, get: function() ' +
768
+ '{return this.m1}}})'
769
+ end
770
+
771
+ it "should prefix bind references to methods as properties" do
772
+ to_js ( 'class C; def m1(); end; def m2; m1; end; end' ) .
773
+ must_equal 'function C() {}; C.prototype.m1 = function() {}; ' +
774
+ 'Object.defineProperty(C.prototype, ' +
775
+ '"m2", {enumerable: true, configurable: true, get: function() ' +
776
+ '{return this.m1.bind(this)}})'
767
777
end
768
778
769
779
it "should prefix class constants referenced in methods by class name" do
770
780
to_js ( 'class C; X = 1; def m; X; end; end' ) .
771
- must_equal 'function C() {}; C.X = 1; C.prototype = { get m () {return C.X}}'
781
+ must_equal 'function C() {}; C.X = 1; Object.defineProperty( C.prototype, "m", {enumerable: true, configurable: true, get: function () {return C.X}}) '
772
782
end
773
783
774
784
it "should insert var self = this when needed" do
775
785
to_js ( 'class C; def m; list.each do; @ivar; end; end; end' ) .
776
- must_equal 'function C() {}; C.prototype = { get m () {var self = this; return list.each(function() {self._ivar})}}'
786
+ must_equal 'function C() {}; Object.defineProperty( C.prototype, "m", {enumerable: true, configurable: true, get: function () {var self = this; return list.each(function() {self._ivar})}}) '
777
787
778
788
to_js ( 'class C; def m(); list.each do; @ivar; @ivar; end; end; end' ) .
779
789
must_equal 'function C() {}; C.prototype.m = function() {var self = this; list.each(function() {self._ivar; self._ivar})}'
@@ -841,7 +851,7 @@ def to_js( string, opts={} )
841
851
to_js ( 'module A; B=1; end' ) .
842
852
must_equal 'A = function() {var B = 1; return {B: B}}()'
843
853
to_js ( 'module A; def b; return 1; end; end' ) .
844
- must_equal 'var A = {get b () {return 1}}'
854
+ must_equal 'var A = {}; Object.defineProperty(A.prototype, "b", {enumerable: true, configurable: true, get: function () {return 1}}) '
845
855
to_js ( 'module A; def b(); return 1; end; end' ) .
846
856
must_equal 'var A = {b: function() {return 1}}'
847
857
to_js ( 'module A; class B; def initialize; @c=1; end; end; end' ) .
@@ -1107,7 +1117,7 @@ def to_js( string, opts={} )
1107
1117
1108
1118
it "should not replace ivars in class definitions" do
1109
1119
to_js ( 'class F; def f; @x; end; end' , ivars : { :@x => 1 } ) .
1110
- must_equal 'function F() {}; F.prototype = { get f () {return this._x}}'
1120
+ must_equal 'function F() {}; Object.defineProperty( F.prototype, "f", {enumerable: true, configurable: true, get: function () {return this._x}}) '
1111
1121
end
1112
1122
end
1113
1123
0 commit comments