diff --git a/lib/class.js b/lib/class.js index ff740cf..4eaac7e 100644 --- a/lib/class.js +++ b/lib/class.js @@ -361,6 +361,16 @@ function createImplement( base, ifaces, cname ) ext_base = args.pop() ; + // if a base was already provided for extending, don't allow them to + // give us yet another one (doesn't make sense) + if ( base && ext_base ) + { + throw Error( + "Cannot override parent " + base.toString() + " with " + + ext_base.toString() + " via extend()" + ); + } + // if a name was provided, use it if ( cname ) { diff --git a/test/test-class-implement.js b/test/test-class-implement.js index ff46d61..54bf454 100644 --- a/test/test-class-implement.js +++ b/test/test-class-implement.js @@ -189,3 +189,27 @@ var Type = Interface.extend( { ); } )(); + +/** + * Consider the following scenario: + * + * MyClass.implement( Type ).extend( MyOtherClass, {} ); + * + * What the above is essentially saying is: "I'd like to extend MyClass by + * implementing Type. Oh, no, wait, I'd actually like it to extend + * MyOtherClass." That doesn't make sense! Likely, it's unintended. Prevent + * confusion and bugs. Throw an error. + */ +( function testCannotSpecifyParentAfterImplementingAtopExistingClass() +{ + assert.throws( function() + { + // should not be permitted + PlainFoo.implement( Type, Type2 ).extend( PlainFoo2, {} ); + }, + Error, + "Cannot specify new parent for extend() when implementing from " + + "existing class" + ); +} )(); +