1
0
Fork 0

Forbid trait extending of final classes

textend
Mike Gerwitz 2015-05-12 00:22:47 -04:00
parent c90fb6eb81
commit 4869d15b2b
2 changed files with 29 additions and 2 deletions

View File

@ -22,6 +22,7 @@
var AbstractClass = require( './class_abstract' ), var AbstractClass = require( './class_abstract' ),
Class = require( './class' ), Class = require( './class' ),
ClassBuilder = require( './ClassBuilder' ), ClassBuilder = require( './ClassBuilder' ),
FinalClass = require( './class_final' ),
Interface = require( './interface' ); Interface = require( './interface' );
@ -123,9 +124,19 @@ Trait.extend = function()
type = _getTraitType( dfn ), type = _getTraitType( dfn ),
isparam = ( type === 'param' ); isparam = ( type === 'param' );
if ( has_extend && !Class.isClass( extend ) ) if ( has_extend )
{ {
throw TypeError( "Trait " + name + " cannot extend non-class" ); if ( !Class.isClass( extend ) )
{
throw TypeError( "Trait " + name + " cannot extend non-class" );
}
else if ( extend.___$$final$$ === true )
{
throw TypeError(
"Trait " + name + " cannot extend final class " +
extend.toString()
)
}
} }
// augment the parser to handle our own oddities // augment the parser to handle our own oddities

View File

@ -25,6 +25,7 @@ require( 'common' ).testCase(
{ {
this.Sut = this.require( 'Trait' ); this.Sut = this.require( 'Trait' );
this.Class = this.require( 'class' ); this.Class = this.require( 'class' );
this.FinalClass = this.require( 'class_final' );
// nonsensical extend bases // nonsensical extend bases
this.nonsense = [ this.nonsense = [
@ -259,4 +260,19 @@ require( 'common' ).testCase(
_self.Sut.extend( _self.Sut( {} ), {} ); _self.Sut.extend( _self.Sut( {} ), {} );
}, TypeError ); }, TypeError );
}, },
/**
* For consistency with the rest of the system, final classes are not
* permitted to be extended.
*/
'Traits cannot extend final classes': function()
{
var _self = this;
this.assertThrows( function()
{
_self.Sut.extend( _self.FinalClass( {} ), {} );
}, TypeError );
},
} ); } );