1
0
Fork 0

Throws TypeError if multiple visibility keywords are given to member builder

closure/master
Mike Gerwitz 2011-01-20 22:11:36 -05:00
parent 9a5fe96c3e
commit a58b7989ee
2 changed files with 59 additions and 4 deletions

View File

@ -39,19 +39,46 @@
*/ */
exports.buildProp = function( members, meta, name, value, keywords ) exports.buildProp = function( members, meta, name, value, keywords )
{ {
getMemberVisibility( members, keywords )[ name ] = value;
};
/**
* Returns member prototype to use for the requested visibility
*
* @param {{public: Object, protected: Object, private: Object}} members
*
* @param {Object.<string,boolean>} keywords parsed keywords
*
* @return {Object} reference to visibility of members argument to use
*/
function getMemberVisibility( members, keywords )
{
var viserr = function()
{
throw TypeError(
"Only one of public, protected or private may be used"
);
}
// there's cleaner ways of doing this, but consider it loop unrolling for
// performance
if ( keywords[ 'private' ] ) if ( keywords[ 'private' ] )
{ {
members[ 'private' ][ name ] = value; ( keywords[ 'public' ] || keywords[ 'protected' ] ) && viserr();
return members[ 'private' ];
} }
else if ( keywords[ 'protected' ] ) else if ( keywords[ 'protected' ] )
{ {
members[ 'protected' ][ name ] = value; ( keywords[ 'public' ] || keywords[ 'private' ] ) && viserr();
return members[ 'protected' ];
} }
else else
{ {
// public keyword is the default, so explicitly specifying it is only // public keyword is the default, so explicitly specifying it is only
// for clarity // for clarity
members[ 'public' ][ name ] = value; ( keywords[ 'private' ] || keywords[ 'protected' ] ) && viserr();
return members[ 'public' ];
} }
}; }

View File

@ -94,3 +94,31 @@ function buildPropQuick( keywords )
); );
} )(); } )();
( function testThrowsTypeErrorIfMultipleVisibilityKeywordsAreGiven()
{
assert.throws( function()
{
buildPropQuick( {
'public': true,
'protected': true,
} );
}, TypeError, "Cannot specify multiple visibility keywords (0)" );
assert.throws( function()
{
buildPropQuick( {
'public': true,
'private': true,
} );
}, TypeError, "Cannot specify multiple visibility keywords (1)" );
assert.throws( function()
{
buildPropQuick( {
'protected': true,
'private': true,
} );
}, TypeError, "Cannot specify multiple visibility keywords (2)" );
} )();