From 98c20ea906c6aa37d92d71de22de227444d57278 Mon Sep 17 00:00:00 2001 From: Nathan Houle Date: Sat, 4 Jul 2015 18:51:55 -0700 Subject: [PATCH 1/2] Replace __proto__ with lodash.create --- lib/hook.js | 6 +++++- lib/reporters/dot.js | 5 ++++- lib/reporters/landing.js | 5 ++++- lib/reporters/list.js | 5 ++++- lib/reporters/min.js | 5 ++++- lib/reporters/nyan.js | 15 +++++++++------ lib/reporters/progress.js | 5 ++++- lib/reporters/spec.js | 5 ++++- lib/reporters/xunit.js | 5 ++++- lib/runnable.js | 5 ++++- lib/runner.js | 5 ++++- lib/suite.js | 5 ++++- lib/test.js | 5 ++++- package.json | 1 + 14 files changed, 59 insertions(+), 18 deletions(-) diff --git a/lib/hook.js b/lib/hook.js index 3dec54b795..429a60cb2b 100644 --- a/lib/hook.js +++ b/lib/hook.js @@ -3,6 +3,7 @@ */ var Runnable = require('./runnable'); +var create = require('lodash.create'); /** * Expose `Hook`. @@ -25,7 +26,10 @@ function Hook(title, fn) { /** * Inherit from `Runnable.prototype`. */ -Hook.prototype.__proto__ = Runnable.prototype; + +Hook.prototype = create(Runnable.prototype, { + constructor: Hook +}); /** * Get or set the test `err`. diff --git a/lib/reporters/dot.js b/lib/reporters/dot.js index b9cb785b25..2ecdb5e1be 100644 --- a/lib/reporters/dot.js +++ b/lib/reporters/dot.js @@ -3,6 +3,7 @@ */ var Base = require('./base'); +var create = require('lodash.create'); var color = Base.color; /** @@ -63,4 +64,6 @@ function Dot(runner) { * Inherit from `Base.prototype`. */ -Dot.prototype.__proto__ = Base.prototype; +Dot.prototype = create(Base.prototype, { + constructor: Dot +}); diff --git a/lib/reporters/landing.js b/lib/reporters/landing.js index 1dd952cb7d..a29c24728c 100644 --- a/lib/reporters/landing.js +++ b/lib/reporters/landing.js @@ -3,6 +3,7 @@ */ var Base = require('./base'); +var create = require('lodash.create'); var cursor = Base.cursor; var color = Base.color; @@ -89,4 +90,6 @@ function Landing(runner) { * Inherit from `Base.prototype`. */ -Landing.prototype.__proto__ = Base.prototype; +Landing.prototype = create(Base.prototype, { + constructor: Landing +}); diff --git a/lib/reporters/list.js b/lib/reporters/list.js index 5f6d37e8b1..f9b3969676 100644 --- a/lib/reporters/list.js +++ b/lib/reporters/list.js @@ -3,6 +3,7 @@ */ var Base = require('./base'); +var create = require('lodash.create'); var color = Base.color; var cursor = Base.cursor; @@ -58,4 +59,6 @@ function List(runner) { * Inherit from `Base.prototype`. */ -List.prototype.__proto__ = Base.prototype; +List.prototype = create(Base.prototype, { + constructor: List +}); diff --git a/lib/reporters/min.js b/lib/reporters/min.js index f0b85578d0..f9c3a148c4 100644 --- a/lib/reporters/min.js +++ b/lib/reporters/min.js @@ -3,6 +3,7 @@ */ var Base = require('./base'); +var create = require('lodash.create'); /** * Expose `Min`. @@ -33,4 +34,6 @@ function Min(runner) { * Inherit from `Base.prototype`. */ -Min.prototype.__proto__ = Base.prototype; +Min.prototype = create(Base.prototype, { + constructor: Min +}); diff --git a/lib/reporters/nyan.js b/lib/reporters/nyan.js index 21b7749868..2baa901031 100644 --- a/lib/reporters/nyan.js +++ b/lib/reporters/nyan.js @@ -3,6 +3,7 @@ */ var Base = require('./base'); +var create = require('lodash.create'); /** * Expose `Dot`. @@ -58,6 +59,14 @@ function NyanCat(runner) { }); } +/** + * Inherit from `Base.prototype`. + */ + +NyanCat.prototype = create(Base.prototype, { + constructor: NyanCat +}); + /** * Draw the nyan cat * @@ -253,9 +262,3 @@ NyanCat.prototype.rainbowify = function(str) { function write(string) { process.stdout.write(string); } - -/** - * Inherit from `Base.prototype`. - */ - -NyanCat.prototype.__proto__ = Base.prototype; diff --git a/lib/reporters/progress.js b/lib/reporters/progress.js index 29c6785451..2ea25bda94 100644 --- a/lib/reporters/progress.js +++ b/lib/reporters/progress.js @@ -3,6 +3,7 @@ */ var Base = require('./base'); +var create = require('lodash.create'); var color = Base.color; var cursor = Base.cursor; @@ -86,4 +87,6 @@ function Progress(runner, options) { * Inherit from `Base.prototype`. */ -Progress.prototype.__proto__ = Base.prototype; +Progress.prototype = create(Base.prototype, { + constructor: Progress +}); diff --git a/lib/reporters/spec.js b/lib/reporters/spec.js index 1b447c1738..9fbd805a3e 100644 --- a/lib/reporters/spec.js +++ b/lib/reporters/spec.js @@ -3,6 +3,7 @@ */ var Base = require('./base'); +var create = require('lodash.create'); var color = Base.color; var cursor = Base.cursor; @@ -80,4 +81,6 @@ function Spec(runner) { * Inherit from `Base.prototype`. */ -Spec.prototype.__proto__ = Base.prototype; +Spec.prototype = create(Base.prototype, { + constructor: Spec +}); diff --git a/lib/reporters/xunit.js b/lib/reporters/xunit.js index 8286cd3f52..70bfc755a0 100644 --- a/lib/reporters/xunit.js +++ b/lib/reporters/xunit.js @@ -3,6 +3,7 @@ */ var Base = require('./base'); +var create = require('lodash.create'); var fs = require('fs'); var escape = require('../utils').escape; @@ -95,7 +96,9 @@ XUnit.prototype.done = function(failures, fn) { * Inherit from `Base.prototype`. */ -XUnit.prototype.__proto__ = Base.prototype; +XUnit.prototype = create(Base.prototype, { + constructor: XUnit +}); /** * Write out the given line. diff --git a/lib/runnable.js b/lib/runnable.js index aa82417413..d0c0f60732 100644 --- a/lib/runnable.js +++ b/lib/runnable.js @@ -4,6 +4,7 @@ var EventEmitter = require('events').EventEmitter; var Pending = require('./pending'); +var create = require('lodash.create'); var debug = require('debug')('mocha:runnable'); var milliseconds = require('./ms'); var utils = require('./utils'); @@ -55,7 +56,9 @@ function Runnable(title, fn) { * Inherit from `EventEmitter.prototype`. */ -Runnable.prototype.__proto__ = EventEmitter.prototype; +Runnable.prototype = create(EventEmitter.prototype, { + constructor: Runnable +}); /** * Set & get timeout `ms`. diff --git a/lib/runner.js b/lib/runner.js index 51bd3c5427..18e941191a 100644 --- a/lib/runner.js +++ b/lib/runner.js @@ -4,6 +4,7 @@ var EventEmitter = require('events').EventEmitter; var Pending = require('./pending'); +var create = require('lodash.create'); var debug = require('debug')('mocha:runner'); var filter = require('./utils').filter; var indexOf = require('./utils').indexOf; @@ -86,7 +87,9 @@ Runner.immediately = global.setImmediate || process.nextTick; * Inherit from `EventEmitter.prototype`. */ -Runner.prototype.__proto__ = EventEmitter.prototype; +Runner.prototype = create(EventEmitter.prototype, { + constructor: Runner +}); /** * Run tests with full titles matching `re`. Updates runner.total diff --git a/lib/suite.js b/lib/suite.js index b59b3c8ae9..eb89fb346e 100644 --- a/lib/suite.js +++ b/lib/suite.js @@ -4,6 +4,7 @@ var EventEmitter = require('events').EventEmitter; var Hook = require('./hook'); +var create = require('lodash.create'); var debug = require('debug')('mocha:suite'); var milliseconds = require('./ms'); var utils = require('./utils'); @@ -66,7 +67,9 @@ function Suite(title, parentContext) { * Inherit from `EventEmitter.prototype`. */ -Suite.prototype.__proto__ = EventEmitter.prototype; +Suite.prototype = create(EventEmitter.prototype, { + constructor: Suite +}); /** * Return a clone of this `Suite`. diff --git a/lib/test.js b/lib/test.js index c6cbfd147b..057e772824 100644 --- a/lib/test.js +++ b/lib/test.js @@ -3,6 +3,7 @@ */ var Runnable = require('./runnable'); +var create = require('lodash.create'); /** * Expose `Test`. @@ -27,4 +28,6 @@ function Test(title, fn) { * Inherit from `Runnable.prototype`. */ -Test.prototype.__proto__ = Runnable.prototype; +Test.prototype = create(Runnable.prototype, { + constructor: Test +}); diff --git a/package.json b/package.json index d544796c21..fb7a8476b4 100644 --- a/package.json +++ b/package.json @@ -282,6 +282,7 @@ "glob": "3.2.3", "growl": "1.8.1", "jade": "0.26.3", + "lodash.create": "^3.1.1", "mkdirp": "0.5.0", "supports-color": "1.2.0" }, From 993d1ebe03a98c9df3afa55d6664d95baa65a02e Mon Sep 17 00:00:00 2001 From: Nathan Houle Date: Sat, 4 Jul 2015 18:52:12 -0700 Subject: [PATCH 2/2] Remove __proto__ parsing from browser build scripts --- support/compile.js | 16 +--------------- 1 file changed, 1 insertion(+), 15 deletions(-) diff --git a/support/compile.js b/support/compile.js index bea017ab51..cd99fc47bc 100644 --- a/support/compile.js +++ b/support/compile.js @@ -33,7 +33,7 @@ args.forEach(function(file){ */ function parse(js) { - return parseRequires(parseInheritance(js)); + return parseRequires(js); } /** @@ -52,20 +52,6 @@ function parseRequires(js) { .replace(/require\('fs'\)/g , "require('browser/fs')"); } -/** - * Parse __proto__. - */ - -function parseInheritance(js) { - return js - .replace(/^ *(\w+)\.prototype\.__proto__ * = *(\w+)\.prototype *;?/gm, function(_, child, parent){ - return 'function F(){};\n' - + 'F.prototype = ' + parent + '.prototype;\n' - + child + '.prototype = new F;\n' - + child + '.prototype.constructor = '+ child + ';\n'; - }); -} - /** * Compile the files. */