diff --git a/lib/class_builder.js b/lib/class_builder.js index 32160e1..07db8ea 100644 --- a/lib/class_builder.js +++ b/lib/class_builder.js @@ -200,6 +200,12 @@ exports.build = function extend() || { __length: 0 } ; + // must extend from constructor or class + if ( typeof base !== 'function' ) + { + throw TypeError( "Must extend from class or constructor"); + } + // prevent extending final classes if ( base.___$$final$$ === true ) { diff --git a/test/test-class-extend.js b/test/test-class-extend.js index 40c3796..85f5026 100644 --- a/test/test-class-extend.js +++ b/test/test-class-extend.js @@ -373,3 +373,31 @@ for ( var i = 0; i < class_count; i++ ) ); } )(); + +/** + * Previously, when attempting to extend from an invalid supertype, you'd get a + * CALL_NON_FUNCTION_AS_CONSTRUCTOR error, which is not very helpful to someone + * who is not familiar with the ease.js internals. Let's provide a more useful + * error that clearly states what's going on. + */ +( function testExtendingFromNonCtorOrClassProvidesUsefulError() +{ + try + { + // invalid supertype + Class.extend( 'oops', {} ); + } + catch ( e ) + { + assert.ok( e.message.search( 'extend from' ), + "Error message for extending from non-ctor or class makes sense" + ); + + return; + } + + assert.fail( + "Attempting to extend from non-ctor or class should throw exception" + ); +} )(); +