Bartosz Dziewoński has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/309987

Change subject: Improve failure message when inheritClass/mixinClass called 
with 'undefined'
......................................................................

Improve failure message when inheritClass/mixinClass called with 'undefined'

Currently, calling `OO.inheritClass( MyClass, OO.Foo )` (where OO.Foo
is undefined because you forgot to specify a dependency) results in a
fairly confusing exception like "Right-hand side of 'instanceof' is
not an object". I've debugged issues like this at least two times in
the past so I think the extra check is worth it.

Bug: T145315
Change-Id: I317ddaa50245aeed4a8eaf299dd38808fdd357e0
---
M src/core.js
M tests/unit/core.test.js
2 files changed, 16 insertions(+), 1 deletion(-)


  git pull ssh://gerrit.wikimedia.org:29418/oojs/core refs/changes/87/309987/1

diff --git a/src/core.js b/src/core.js
index 94a3201..6f42fd6 100644
--- a/src/core.js
+++ b/src/core.js
@@ -78,8 +78,11 @@
 oo.inheritClass = function ( targetFn, originFn ) {
        var targetConstructor;
 
+       if ( !( originFn instanceof Function ) ) {
+               throw new Error( 'inheritClass: Origin is not a function 
(actually ' + typeof originFn + ')' );
+       }
        if ( targetFn.prototype instanceof originFn ) {
-               throw new Error( 'Target already inherits from origin' );
+               throw new Error( 'inheritClass: Target already inherits from 
origin' );
        }
 
        targetConstructor = targetFn.prototype.constructor;
@@ -139,6 +142,10 @@
 oo.mixinClass = function ( targetFn, originFn ) {
        var key;
 
+       if ( !( originFn instanceof Function ) ) {
+               throw new Error( 'mixinClass: Origin is not a function 
(actually ' + typeof originFn + ')' );
+       }
+
        // Copy prototype properties
        for ( key in originFn.prototype ) {
                if ( key !== 'constructor' && hasOwn.call( originFn.prototype, 
key ) ) {
diff --git a/tests/unit/core.test.js b/tests/unit/core.test.js
index 32fd165..727daca 100644
--- a/tests/unit/core.test.js
+++ b/tests/unit/core.test.js
@@ -81,6 +81,10 @@
                        oo.inheritClass( Child, Object );
                }, 'Throw if target already inherits from source (naturally, 
Object)' );
 
+               assert.throws( function () {
+                       oo.inheritClass( Child, undefined );
+               }, 'Throw if source is undefined (e.g. due to missing 
dependency)' );
+
                enumKeys = [];
                for ( key in child ) {
                        enumKeys.push( key );
@@ -173,6 +177,10 @@
                obj = new Mixer();
 
                assert.strictEqual( obj.protoFunction2(), 'Child', 'method 
works as expected' );
+
+               assert.throws( function () {
+                       oo.mixinClass( Mixer, undefined );
+               }, 'Throw if source is undefined (e.g. due to missing 
dependency)' );
        } );
 
        QUnit.test( 'isSubclass', function ( assert ) {

-- 
To view, visit https://gerrit.wikimedia.org/r/309987
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I317ddaa50245aeed4a8eaf299dd38808fdd357e0
Gerrit-PatchSet: 1
Gerrit-Project: oojs/core
Gerrit-Branch: master
Gerrit-Owner: Bartosz Dziewoński <matma....@gmail.com>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to