From 40b4bcd3a2671036cb96df50d650931b624c6f87 Mon Sep 17 00:00:00 2001 From: Sindre Sorhus Date: Tue, 9 Aug 2016 22:07:18 +0200 Subject: [PATCH] modularize the class binding one less TODO \o/ --- api.js | 5 ++--- lib/caching-precompiler.js | 5 ++--- lib/concurrent.js | 5 ++--- lib/logger.js | 7 +++---- lib/run-status.js | 5 ++--- lib/sequence.js | 6 ++---- package.json | 1 + 7 files changed, 14 insertions(+), 20 deletions(-) diff --git a/api.js b/api.js index 0b1075136..794359b71 100644 --- a/api.js +++ b/api.js @@ -11,6 +11,7 @@ var findCacheDir = require('find-cache-dir'); var debounce = require('lodash.debounce'); var ms = require('ms'); var AvaFiles = require('ava-files'); +var autoBind = require('auto-bind'); var AvaError = require('./lib/ava-error'); var fork = require('./lib/fork'); var CachingPrecompiler = require('./lib/caching-precompiler'); @@ -38,9 +39,7 @@ function Api(options) { return ret; }); - Object.keys(Api.prototype).forEach(function (key) { - this[key] = this[key].bind(this); - }, this); + autoBind(this); } util.inherits(Api, EventEmitter); diff --git a/lib/caching-precompiler.js b/lib/caching-precompiler.js index fd26edc1d..7ecd6485b 100644 --- a/lib/caching-precompiler.js +++ b/lib/caching-precompiler.js @@ -6,6 +6,7 @@ var cachingTransform = require('caching-transform'); var stripBom = require('strip-bom'); var md5Hex = require('md5-hex'); var packageHash = require('package-hash'); +var autoBind = require('auto-bind'); var babelConfigHelper = require('./babel-config'); function CachingPrecompiler(cacheDirPath, babelConfig) { @@ -17,9 +18,7 @@ function CachingPrecompiler(cacheDirPath, babelConfig) { this.cacheDirPath = cacheDirPath; this.fileHashes = {}; - Object.keys(CachingPrecompiler.prototype).forEach(function (name) { - this[name] = this[name].bind(this); - }, this); + autoBind(this); this.transform = this._createTransform(); } diff --git a/lib/concurrent.js b/lib/concurrent.js index 8f38a5e3e..86fee6417 100644 --- a/lib/concurrent.js +++ b/lib/concurrent.js @@ -1,6 +1,7 @@ 'use strict'; var Promise = require('bluebird'); var isPromise = require('is-promise'); +var autoBind = require('auto-bind'); var AvaError = require('./ava-error'); function noop() {} @@ -22,9 +23,7 @@ function Concurrent(tests, bail) { this.tests = tests; this.bail = bail || false; - Object.keys(Concurrent.prototype).forEach(function (key) { - this[key] = this[key].bind(this); - }, this); + autoBind(this); } Concurrent.prototype.run = function () { diff --git a/lib/logger.js b/lib/logger.js index 0d6ea7ea1..c79012698 100644 --- a/lib/logger.js +++ b/lib/logger.js @@ -1,15 +1,14 @@ 'use strict'; +var autoBind = require('auto-bind'); function Logger(reporter) { if (!(this instanceof Logger)) { throw new TypeError('Class constructor Logger cannot be invoked without \'new\''); } - Object.keys(Logger.prototype).forEach(function (key) { - this[key] = this[key].bind(this); - }, this); - this.reporter = reporter; + + autoBind(this); } module.exports = Logger; diff --git a/lib/run-status.js b/lib/run-status.js index a8495d7dc..b733f21e0 100644 --- a/lib/run-status.js +++ b/lib/run-status.js @@ -5,6 +5,7 @@ var chalk = require('chalk'); var isObj = require('is-obj'); var flatten = require('arr-flatten'); var figures = require('figures'); +var autoBind = require('auto-bind'); var prefixTitle = require('./prefix-title'); function RunStatus(opts) { @@ -33,9 +34,7 @@ function RunStatus(opts) { this.stats = []; this.tests = []; - Object.keys(RunStatus.prototype).forEach(function (key) { - this[key] = this[key].bind(this); - }, this); + autoBind(this); } util.inherits(RunStatus, EventEmitter); diff --git a/lib/sequence.js b/lib/sequence.js index 7d2b0a2e5..9e6cb63e3 100644 --- a/lib/sequence.js +++ b/lib/sequence.js @@ -1,5 +1,6 @@ 'use strict'; var isPromise = require('is-promise'); +var autoBind = require('auto-bind'); var AvaError = require('./ava-error'); function noop() {} @@ -21,10 +22,7 @@ function Sequence(tests, bail) { this.tests = tests; this.bail = bail || false; - // TODO(vdemedes): separate into a utility (it's being used in serveral places) - Object.keys(Sequence.prototype).forEach(function (key) { - this[key] = this[key].bind(this); - }, this); + autoBind(this); } Sequence.prototype.run = function () { diff --git a/package.json b/package.json index b8859fbb5..e1eed53d6 100644 --- a/package.json +++ b/package.json @@ -89,6 +89,7 @@ "array-union": "^1.0.1", "array-uniq": "^1.0.2", "arrify": "^1.0.0", + "auto-bind": "^0.1.0", "ava-files": "^0.1.1", "ava-init": "^0.1.0", "babel-code-frame": "^6.7.5",