Skip to content
Browse files

support get and set class properties

  • Loading branch information...
1 parent b554e0a commit ed71d8a67d4db5311603e77c2df0a8458e7005e6 @Swatinem Swatinem committed Sep 2, 2012
Showing with 77 additions and 12 deletions.
  1. +17 −3 demo/lib/harmonizr.js
  2. +17 −3 lib/harmonizr.js
  3. +17 −3 src/harmonizr.js
  4. +17 −3 test/lib/harmonizr.js
  5. +9 −0 test/test.js
View
20 demo/lib/harmonizr.js
@@ -375,10 +375,24 @@ function processClasses(modifier, options) {
modifier.replace(method.key.loc.start, method.key.loc.end,
'function ' + name);
} else {
- modifier.replace(method.key.loc.start, method.key.loc.end,
- name + '.prototype.' + method.key.name + ' = function');
+ var prop = method.key.name;
+ if (method.kind) {
+ var otherkind = method.kind === 'set' ? 'get' : 'set';
+ var protoprop = name + '.prototype, "' + prop + '"';
+ modifier.insert(method.loc.end,
+ '})');
+ modifier.replace(method.loc.start, method.key.loc.end,
+ 'var __old_' + prop + ' = ' +
+ 'Object.getOwnPropertyDescriptor(' + protoprop + '); ' +
+ 'Object.defineProperty(' + protoprop + ', ' +
+ '{configurable: true, ' + otherkind + ': __old_' + prop +
+ ' && __old_' + prop + '.' + otherkind + ', ' +
+ method.kind + ': function');
+ } else {
+ modifier.replace(method.key.loc.start, method.key.loc.end,
+ name + '.prototype.' + prop + ' = function');
+ }
}
- // TODO: get and set methods
}
function hasConstructor() {
View
20 lib/harmonizr.js
@@ -375,10 +375,24 @@ function processClasses(modifier, options) {
modifier.replace(method.key.loc.start, method.key.loc.end,
'function ' + name);
} else {
- modifier.replace(method.key.loc.start, method.key.loc.end,
- name + '.prototype.' + method.key.name + ' = function');
+ var prop = method.key.name;
+ if (method.kind) {
+ var otherkind = method.kind === 'set' ? 'get' : 'set';
+ var protoprop = name + '.prototype, "' + prop + '"';
+ modifier.insert(method.loc.end,
+ '})');
+ modifier.replace(method.loc.start, method.key.loc.end,
+ 'var __old_' + prop + ' = ' +
+ 'Object.getOwnPropertyDescriptor(' + protoprop + '); ' +
+ 'Object.defineProperty(' + protoprop + ', ' +
+ '{configurable: true, ' + otherkind + ': __old_' + prop +
+ ' && __old_' + prop + '.' + otherkind + ', ' +
+ method.kind + ': function');
+ } else {
+ modifier.replace(method.key.loc.start, method.key.loc.end,
+ name + '.prototype.' + prop + ' = function');
+ }
}
- // TODO: get and set methods
}
function hasConstructor() {
View
20 src/harmonizr.js
@@ -375,10 +375,24 @@ function processClasses(modifier, options) {
modifier.replace(method.key.loc.start, method.key.loc.end,
'function ' + name);
} else {
- modifier.replace(method.key.loc.start, method.key.loc.end,
- name + '.prototype.' + method.key.name + ' = function');
+ var prop = method.key.name;
+ if (method.kind) {
+ var otherkind = method.kind === 'set' ? 'get' : 'set';
+ var protoprop = name + '.prototype, "' + prop + '"';
+ modifier.insert(method.loc.end,
+ '})');
+ modifier.replace(method.loc.start, method.key.loc.end,
+ 'var __old_' + prop + ' = ' +
+ 'Object.getOwnPropertyDescriptor(' + protoprop + '); ' +
+ 'Object.defineProperty(' + protoprop + ', ' +
+ '{configurable: true, ' + otherkind + ': __old_' + prop +
+ ' && __old_' + prop + '.' + otherkind + ', ' +
+ method.kind + ': function');
+ } else {
+ modifier.replace(method.key.loc.start, method.key.loc.end,
+ name + '.prototype.' + prop + ' = function');
+ }
}
- // TODO: get and set methods
}
function hasConstructor() {
View
20 test/lib/harmonizr.js
@@ -375,10 +375,24 @@ function processClasses(modifier, options) {
modifier.replace(method.key.loc.start, method.key.loc.end,
'function ' + name);
} else {
- modifier.replace(method.key.loc.start, method.key.loc.end,
- name + '.prototype.' + method.key.name + ' = function');
+ var prop = method.key.name;
+ if (method.kind) {
+ var otherkind = method.kind === 'set' ? 'get' : 'set';
+ var protoprop = name + '.prototype, "' + prop + '"';
+ modifier.insert(method.loc.end,
+ '})');
+ modifier.replace(method.loc.start, method.key.loc.end,
+ 'var __old_' + prop + ' = ' +
+ 'Object.getOwnPropertyDescriptor(' + protoprop + '); ' +
+ 'Object.defineProperty(' + protoprop + ', ' +
+ '{configurable: true, ' + otherkind + ': __old_' + prop +
+ ' && __old_' + prop + '.' + otherkind + ', ' +
+ method.kind + ': function');
+ } else {
+ modifier.replace(method.key.loc.start, method.key.loc.end,
+ name + '.prototype.' + prop + ' = function');
+ }
}
- // TODO: get and set methods
}
function hasConstructor() {
View
9 test/test.js
@@ -491,6 +491,15 @@ describe('harmonizr', function() {
harmonize(src, expected);
});
+ it('supports getter and setter members', function() {
+ var src = 'class A {set a(a) {}; get b() {return b;}}';
+ var expected = 'var A = (function () {function A() {};' +
+ 'var __old_a = Object.getOwnPropertyDescriptor(A.prototype, "a"); Object.defineProperty(A.prototype, "a", {configurable: true, get: __old_a && __old_a.get, set: function(a) {}}); ' +
+ 'var __old_b = Object.getOwnPropertyDescriptor(A.prototype, "b"); Object.defineProperty(A.prototype, "b", {configurable: true, set: __old_b && __old_b.set, get: function() {return b;}})'+
+ '; return A;})();';
+ harmonize(src, expected);
+ });
+
it('supports constructors and member functions', function() {
var src = 'class A {constructor(a) { this.a = a; }; a(a) {}}';
var expected = 'var A = (function () {function A(a) { this.a = a; }; A.prototype.a = function(a) {}; return A;})();';

0 comments on commit ed71d8a

Please sign in to comment.
Something went wrong with that request. Please try again.