jenkins-bot has submitted this change and it was merged. Change subject: core: Improve error message when inheritClass/mixinClass called with 'undefined' ......................................................................
core: Improve error 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(-) Approvals: Krinkle: Looks good to me, approved jenkins-bot: Verified diff --git a/src/core.js b/src/core.js index 94a3201..933bcad 100644 --- a/src/core.js +++ b/src/core.js @@ -78,8 +78,11 @@ oo.inheritClass = function ( targetFn, originFn ) { var targetConstructor; + if ( !originFn ) { + throw new Error( 'inheritClass: Origin is not a function (actually ' + 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 ) { + throw new Error( 'mixinClass: Origin is not a function (actually ' + 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..603f5ec 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 ); + }, /Origin is not a function/, '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 ); + }, /Origin is not a function/, '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: merged Gerrit-Change-Id: I317ddaa50245aeed4a8eaf299dd38808fdd357e0 Gerrit-PatchSet: 2 Gerrit-Project: oojs/core Gerrit-Branch: master Gerrit-Owner: Bartosz Dziewoński <matma....@gmail.com> Gerrit-Reviewer: Bartosz Dziewoński <matma....@gmail.com> Gerrit-Reviewer: Jforrester <jforres...@wikimedia.org> Gerrit-Reviewer: Krinkle <krinklem...@gmail.com> Gerrit-Reviewer: jenkins-bot <> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits