1
0
Fork 0

Disallowing member redeclaration in same class definition

closure/master
Mike Gerwitz 2011-03-19 00:48:02 -04:00
parent 7d23b7cdf9
commit 5af833ab05
3 changed files with 51 additions and 1 deletions

1
TODO
View File

@ -5,7 +5,6 @@
[ target: 0.1.0 ] [ target: 0.1.0 ]
Misc Misc
- Class module is becoming too large; refactor - Class module is becoming too large; refactor
- Disallow member redeclaration in definition
- Permit binding on class methods - Permit binding on class methods
- Move tests to directly test propobj - Move tests to directly test propobj
- Was never done after refactoring. It's tested as a consequence of being - Was never done after refactoring. It's tested as a consequence of being

View File

@ -444,6 +444,7 @@ var extend = ( function( extending )
properties = {}, properties = {},
prop_init = member_builder.initMembers(), prop_init = member_builder.initMembers(),
members = member_builder.initMembers( prototype ), members = member_builder.initMembers( prototype ),
defs = {},
abstract_methods = abstract_methods =
util.clone( getMeta( base ).abstractMethods ) util.clone( getMeta( base ).abstractMethods )
@ -480,6 +481,19 @@ var extend = ( function( extending )
"__initProps is a reserved method" "__initProps is a reserved method"
); );
} }
// if a member was defined multiple times in the same class
// declaration, throw an error
if ( hasOwn.call( defs, name ) )
{
throw Error(
"Cannot redefine method '" + name + "' in same declaration"
);
}
// keep track of the definitions (only during class declaration)
// to catch duplicates
defs[ name ] = 1;
}, },
property: function( name, value, keywords ) property: function( name, value, keywords )

View File

@ -311,3 +311,40 @@ for ( var i = 0; i < class_count; i++ )
); );
} )(); } )();
/**
* In ease.js's initial design, keywords were not included. This meant that
* duplicate member definitions were not possible - it'd throw a parse error.
* However, with keywords, it is now possible to redeclare a member with the
* same name in the same class definition. Since this doesn't make much sense,
* we must disallow it.
*/
( function testCannotProvideDuplicateMemberDefintions()
{
assert.throws( function()
{
Class(
{
// declare as protected first so that we won't get a visibility
// de-escalation error with the below re-definition
'protected foo': '',
// should fail; redefinition
'public foo': '',
} );
}, Error, "Cannot redeclare property in same class definition" );
assert.throws( function()
{
Class(
{
// declare as protected first so that we won't get a visibility
// de-escalation error with the below re-definition
'protected foo': function() {},
// should fail; redefinition
'public foo': function() {},
} );
}, Error, "Cannot redeclare method in same class definition" );
} )();