diff --git a/lib/class_builder.js b/lib/class_builder.js index 868bef4..4c3eee6 100644 --- a/lib/class_builder.js +++ b/lib/class_builder.js @@ -745,18 +745,28 @@ function attachStatic( ctor, members, base, inheriting ) return base.$.apply( context, arguments ); } + var prop_item = props[ found ][ prop ]; + // if a value was provided, this method should be treated as a // setter rather than a getter (we *must* test using // arguments.length to ensure that setting to undefined works) if ( arguments.length > 1 ) { - props[ found ][ prop ] = val; + // if const, disallow modification + if ( prop_item[ 1 ][ 'const' ] ) + { + throw TypeError( + "Cannot modify constant property '" + prop + "'" + ); + } + + prop_item[ 0 ] = val; return context; } else { // return the value - return props[ found ][ prop ]; + return prop_item[ 0 ]; } } ); } diff --git a/lib/member_builder.js b/lib/member_builder.js index b6d1803..ed14d2e 100644 --- a/lib/member_builder.js +++ b/lib/member_builder.js @@ -235,7 +235,7 @@ exports.buildProp = function( members, meta, name, value, keywords, base ) ); } - getMemberVisibility( members, keywords )[ name ] = value; + getMemberVisibility( members, keywords )[ name ] = [ value, keywords ]; }; diff --git a/lib/propobj.js b/lib/propobj.js index 42e7c6b..d4f7f04 100644 --- a/lib/propobj.js +++ b/lib/propobj.js @@ -137,7 +137,7 @@ function doSetup( dest, properties, methods, unless_keyword ) { if ( hasOwn.call( properties, prop ) ) { - dest[ prop ] = util.clone( properties[ prop ] ); + dest[ prop ] = util.clone( properties[ prop ][ 0 ] ); } } } diff --git a/test/test-class_builder-const.js b/test/test-class_builder-const.js index 59fd8de..b34d76a 100644 --- a/test/test-class_builder-const.js +++ b/test/test-class_builder-const.js @@ -103,3 +103,27 @@ var common = require( './common' ), ); } )(); + +/** + * As the name implies, constant properties should not be writable. + */ +( function testConstKeywordCreatesImmutableProperty() +{ + try + { + // this should fail (trying to alter const prop foo) + builder.build( { 'const foo': 'bar' } ).$( 'foo', 'baz' ); + } + catch ( e ) + { + assert.ok( + e.message.search( 'foo' ) !== -1, + "Const modification error should contain name of property" + ); + + return; + } + + assert.fail( "Constant properties should not be writable" ); +} )(); +