Skip to content

Commit

Permalink
Consistently use proper ES6 subclass syntax.
Browse files Browse the repository at this point in the history
  • Loading branch information
cscott committed Jun 4, 2015
1 parent 8049546 commit b5961ed
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 10 deletions.
5 changes: 3 additions & 2 deletions es6-shim.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,9 @@
Object.setPrototypeOf(o, Subclass.prototype);
return o;
};
Object.setPrototypeOf(Sub, C);
Sub.prototype = create(C.prototype, {
constructor: { value: C }
constructor: { value: Sub }
});
return f(Sub);
});
Expand Down Expand Up @@ -1925,7 +1926,7 @@
// implementation is buggy in a number of ways. Let's check subclassing
// support to see if we have a buggy implementation.
var promiseSupportsSubclassing = supportsSubclassing(globals.Promise, function (S) {
return S.resolve(42) instanceof S;
return S.resolve(42).then(function () {}) instanceof S;
});
var promiseIgnoresNonFunctionThenCallbacks = !throwsError(function () { globals.Promise.reject(42).then(null, 5).then(null, noop); });
var promiseRequiresObjectContext = throwsError(function () { globals.Promise.call(3, noop); });
Expand Down
2 changes: 2 additions & 0 deletions test/collections.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ describe('Collections', function () {
Object.setPrototypeOf(map, MyMap.prototype);
return map;
};
Object.setPrototypeOf(MyMap, Map);
MyMap.prototype = Object.create(Map.prototype, {
constructor: { value: MyMap }
});
Expand Down Expand Up @@ -652,6 +653,7 @@ describe('Collections', function () {
Object.setPrototypeOf(set, MySet.prototype);
return set;
};
Object.setPrototypeOf(MySet, Set);
MySet.prototype = Object.create(Set.prototype, {
constructor: { value: MySet }
});
Expand Down
7 changes: 5 additions & 2 deletions test/promise/all.js
Original file line number Diff line number Diff line change
Expand Up @@ -158,18 +158,21 @@ describe('Promise.all', function () {
var hijack = true;
var actualArguments = [];
var P = function (resolver) {
var self;
if (hijack) {
hijack = false;
Promise.call(this, function (resolve, reject) {
self = new Promise(function (resolve, reject) {
return resolver(function (values) {
// record arguments & # of times resolve function is called
actualArguments.push(values.slice());
return resolve(values);
}, reject);
});
} else {
Promise.call(this, resolver);
self = new Promise(resolver);
}
Object.setPrototypeOf(self, P.prototype);
return self;
};
if (!Object.setPrototypeOf) { return done(); } // skip test if on IE < 11
Object.setPrototypeOf(P, Promise);
Expand Down
8 changes: 6 additions & 2 deletions test/promise/evil-promises.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@ describe('Evil promises should not be able to break invariants', function () {
specify('resolving to a promise that calls onFulfilled twice', function (done) {
// note that we have to create a trivial subclass, as otherwise the
// Promise.resolve(evilPromise) is just the identity function.
var EvilPromise = function (executor) { Promise.call(this, executor); };
var EvilPromise = function (executor) {
var self = new Promise(executor);
Object.setPrototypeOf(self, EvilPromise.prototype);
return self;
};
if (!Object.setPrototypeOf) { return done(); } // skip test if on IE < 11
Object.setPrototypeOf(EvilPromise, Promise);
EvilPromise.prototype = Object.create(Promise.prototype, {
Expand All @@ -20,7 +24,7 @@ describe('Evil promises should not be able to break invariants', function () {
};

var calledAlready = false;
Promise.resolve(evilPromise).then(function (value) {
new Promise(function (r) { r(evilPromise); }).then(function (value) {
assert.strictEqual(calledAlready, false);
calledAlready = true;
assert.strictEqual(value, 1);
Expand Down
10 changes: 6 additions & 4 deletions test/promise/subclass.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ describe('Support user subclassing of Promise', function () {
'use strict';

it('should work if you do it right', function (done) {
// This is the "correct" es6-compatible way; see gh #170
// (Thanks, @domenic!)
// This is the "correct" es6-compatible way.
// (Thanks, @domenic and @zloirock!)
var MyPromise = function (executor) {
Promise.call(this, executor);
this.mine = 'yeah';
var self = new Promise(executor);
Object.setPrototypeOf(self, MyPromise.prototype);
self.mine = 'yeah';
return self;
};
if (!Object.setPrototypeOf) { return done(); } // skip test if on IE < 11
Object.setPrototypeOf(MyPromise, Promise);
Expand Down

0 comments on commit b5961ed

Please sign in to comment.