diff --git a/lib/member_builder.js b/lib/member_builder.js index ad4e844..af03593 100644 --- a/lib/member_builder.js +++ b/lib/member_builder.js @@ -251,6 +251,12 @@ exports.buildProp = function( members, meta, name, value, keywords, base ) ); } + // properties are inherently virtual + if ( keywords['virtual'] ) + { + throw TypeError( "Cannot declare property '" + name + "' as virtual" ); + } + getMemberVisibility( members, keywords )[ name ] = [ value, keywords ]; }; diff --git a/test/test-member_builder-prop.js b/test/test-member_builder-prop.js index b1afbb3..eddbfda 100644 --- a/test/test-member_builder-prop.js +++ b/test/test-member_builder-prop.js @@ -66,3 +66,29 @@ mb_common.assertCommon(); }, TypeError, "Cannot declare abstract property" ); } )(); + +/** + * Properties, unlike methods, are virtual by default. If a property's value can + * be reassigned, why would a subclass not be able to reassign it? If one wishes + * to prevent a property's value from changing, they should use the visibility + * modifiers or declare the property as a constant. + */ +( function testCannotDeclareVirtualProperty() +{ + try + { + mb_common.buildMemberQuick( { 'virtual': true } ); + } + catch ( e ) + { + assert.ok( + e.message.search( mb_common.name ) !== -1, + "Virtual property error message should contain property name" + ); + + return; + } + + assert.fail( "Should not be permitted to declare virtual properties" ); +} )(); +