1
0
Fork 0

Prohibiting trait getters/setters

perfodd
Mike Gerwitz 2014-03-11 06:58:39 -04:00
parent bfadd9fce9
commit cc43f4b339
2 changed files with 61 additions and 0 deletions

View File

@ -99,6 +99,7 @@ Trait.extend = function( dfn )
dfn.___$$parser$$ = {
each: _parseMember,
property: _parseProps,
getset: _parseGetSet,
};
// give the abstract trait class a distinctive name for debugging
@ -208,6 +209,28 @@ function _parseProps( name, value, keywords, h )
}
/**
* Immediately throws an exception, as getters/setters are unsupported
*
* This is a temporary restriction; they will be supported in future
* releases.
*
* @param {string} name property name
* @param {*} value property value
* @param {Object} keywords property keywords
* @param {Function} h original handler that we replaced
*
* @return {undefined}
*/
function _parseGetSet( name, value, keywords, h )
{
throw Error(
"Cannot define property `" + name + "'; getters/setters are " +
"currently unsupported"
);
}
/**
* Implement one or more interfaces
*

View File

@ -28,6 +28,10 @@ require( 'common' ).testCase(
this.Interface = this.require( 'interface' );
this.AbstractClass = this.require( 'class_abstract' );
this.hasGetSet = !(
this.require( 'util' ).definePropertyFallback()
);
// means of creating anonymous traits
this.ctor = [
this.Sut.extend,
@ -413,4 +417,38 @@ require( 'common' ).testCase(
Sut( { 'static foo': function() {} } );
} );
},
/**
* For the same reasons as static members (described immediately above),
* getters/setters are unsupported until future versions.
*
* Note that we use defineProperty instead of the short-hand object
* literal notation to avoid syntax errors in pre-ES5 environments.
*/
'Trait getters and setters are prohibited': function()
{
// perform these tests only when getters/setters are supported by
// our environment
if ( !( this.hasGetSet ) )
{
return;
}
var Sut = this.Sut;
this.assertThrows( function()
{
var dfn = {};
Object.defineProperty( dfn, 'foo',
{
get: function() {},
set: function() {},
enumerable: true,
} );
Sut( dfn );
} );
},
} );