Cannot override method with property
parent
dea6972416
commit
12e5b48a7d
|
@ -22,6 +22,8 @@
|
||||||
* @package core
|
* @package core
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
var visibility = [ 'public', 'protected', 'private' ];
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Initializes member object
|
* Initializes member object
|
||||||
|
@ -81,6 +83,14 @@ exports.buildMethod = function( members, meta, name, value, keywords )
|
||||||
*/
|
*/
|
||||||
exports.buildProp = function( members, meta, name, value, keywords )
|
exports.buildProp = function( members, meta, name, value, keywords )
|
||||||
{
|
{
|
||||||
|
// disallow overriding methods with properties
|
||||||
|
if ( scanMembers( members, name ) instanceof Function )
|
||||||
|
{
|
||||||
|
throw new TypeError(
|
||||||
|
"Cannot override method '" + name + "' with property"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
getMemberVisibility( members, keywords )[ name ] = value;
|
getMemberVisibility( members, keywords )[ name ] = value;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -164,3 +174,30 @@ function getMemberVisibility( members, keywords )
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Scan each level of visibility for the requested member
|
||||||
|
*
|
||||||
|
* @param {{public: Object, protected: Object, private: Object}} members
|
||||||
|
*
|
||||||
|
* @param {string} name member to locate
|
||||||
|
*
|
||||||
|
* @return {*} member, if located, otherwise undefined
|
||||||
|
*/
|
||||||
|
function scanMembers( members, name )
|
||||||
|
{
|
||||||
|
var i = visibility.length,
|
||||||
|
member = null;
|
||||||
|
|
||||||
|
// locate requested member by scanning each level of visibility
|
||||||
|
while ( i-- )
|
||||||
|
{
|
||||||
|
if ( member = members[ visibility[ i ] ][ name ] )
|
||||||
|
{
|
||||||
|
return member;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return undefined;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
|
@ -37,12 +37,15 @@ exports.buildMember = null;
|
||||||
/**
|
/**
|
||||||
* Partially applied function to quickly build properties using common test data
|
* Partially applied function to quickly build properties using common test data
|
||||||
*/
|
*/
|
||||||
exports.buildMemberQuick = function( keywords )
|
exports.buildMemberQuick = function( keywords, preserve_prior )
|
||||||
{
|
{
|
||||||
keywords = keywords || {};
|
keywords = keywords || {};
|
||||||
|
|
||||||
// clear out the members for a fresh start
|
// clear out the members for a fresh start
|
||||||
exports.members = { 'public': {}, 'protected': {}, 'private': {} };
|
if ( !preserve_prior )
|
||||||
|
{
|
||||||
|
exports.members = { 'public': {}, 'protected': {}, 'private': {} };
|
||||||
|
}
|
||||||
|
|
||||||
return exports.buildMember(
|
return exports.buildMember(
|
||||||
exports.members,
|
exports.members,
|
||||||
|
|
|
@ -23,6 +23,7 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
var common = require( './common' ),
|
var common = require( './common' ),
|
||||||
|
assert = require( 'assert' ),
|
||||||
mb_common = require( './inc-member_builder-common' )
|
mb_common = require( './inc-member_builder-common' )
|
||||||
;
|
;
|
||||||
|
|
||||||
|
@ -32,3 +33,18 @@ mb_common.buildMember = common.require( 'member_builder' ).buildProp;
|
||||||
// do assertions common to all member builders
|
// do assertions common to all member builders
|
||||||
mb_common.assertCommon();
|
mb_common.assertCommon();
|
||||||
|
|
||||||
|
|
||||||
|
( function testCannotOverrideMethodWithProperty()
|
||||||
|
{
|
||||||
|
// this will be considered a method, as it is a function
|
||||||
|
mb_common.value = function() {};
|
||||||
|
mb_common.buildMemberQuick();
|
||||||
|
|
||||||
|
assert.throws( function()
|
||||||
|
{
|
||||||
|
// attempt to override with property
|
||||||
|
mb_common.value = 'foo';
|
||||||
|
mb_common.buildMemberQuick( {}, true );
|
||||||
|
}, TypeError, "Cannot override method with property" );
|
||||||
|
} )();
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue