From a394fce8745ae0eb3fd81e10a1151707736b503a Mon Sep 17 00:00:00 2001 From: Sindre Sorhus Date: Wed, 26 Dec 2018 21:23:18 +0100 Subject: [PATCH] Update bundled `auto-bind` See https://github.com/sindresorhus/auto-bind/releases/tag/v2.0.0 and https://github.com/sindresorhus/auto-bind/compare/v1.2.1...v2.0.0 --- auto-bind.js | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/auto-bind.js b/auto-bind.js index c024fe6..4018a3a 100644 --- a/auto-bind.js +++ b/auto-bind.js @@ -1,6 +1,20 @@ // From https://github.com/sindresorhus/auto-bind/blob/master/index.js // Bundled because of Create React App… 'use strict'; + +// Gets all non-builtin properties up the prototype chain +const getAllProperties = object => { + const props = new Set(); + + do { + for (const key of Reflect.ownKeys(object)) { + props.add([object, key]); + } + } while ((object = Reflect.getPrototypeOf(object)) && object !== Object.prototype); + + return props; +}; + module.exports = (self, options) => { options = Object.assign({}, options); @@ -18,11 +32,14 @@ module.exports = (self, options) => { return true; }; - for (const key of Object.getOwnPropertyNames(self.constructor.prototype)) { - const val = self[key]; + for (const [object, key] of getAllProperties(self.constructor.prototype)) { + if (key === 'constructor' || !filter(key)) { + continue; + } - if (key !== 'constructor' && typeof val === 'function' && filter(key)) { - self[key] = val.bind(self); + const descriptor = Reflect.getOwnPropertyDescriptor(object, key); + if (descriptor && typeof descriptor.value === 'function') { + self[key] = self[key].bind(self); } }