From cc43f4b339a970101eaaf142d6d7ef255454f822 Mon Sep 17 00:00:00 2001 From: Mike Gerwitz Date: Tue, 11 Mar 2014 06:58:39 -0400 Subject: [PATCH] Prohibiting trait getters/setters --- lib/Trait.js | 23 ++++++++++++++++++++++ test/Trait/DefinitionTest.js | 38 ++++++++++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+) diff --git a/lib/Trait.js b/lib/Trait.js index 90687d6..3aa9e6c 100644 --- a/lib/Trait.js +++ b/lib/Trait.js @@ -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 * diff --git a/test/Trait/DefinitionTest.js b/test/Trait/DefinitionTest.js index fba9190..21325ec 100644 --- a/test/Trait/DefinitionTest.js +++ b/test/Trait/DefinitionTest.js @@ -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 ); + } ); + }, } );