From 0c5293991c97ce8271d7dc6697db764d35e88b6e Mon Sep 17 00:00:00 2001 From: Mike Gerwitz Date: Wed, 18 May 2011 20:51:02 -0400 Subject: [PATCH] Disallowed use of final keyword with properties --- lib/member_builder.js | 10 ++++++++++ test/test-class_builder-final.js | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+) diff --git a/lib/member_builder.js b/lib/member_builder.js index e83cda2..f4ea3b6 100644 --- a/lib/member_builder.js +++ b/lib/member_builder.js @@ -208,6 +208,16 @@ exports.buildProp = function( members, meta, name, value, keywords, base ) ); } + // nor do final properties, because of late static binding (const should be + // used instead; read up on the rationale in the documentation) + if ( keywords[ 'final' ] ) + { + throw TypeError( + "Property '" + name + "' cannot be declared as final; consider " + + "const" + ); + } + getMemberVisibility( members, keywords )[ name ] = value; }; diff --git a/test/test-class_builder-final.js b/test/test-class_builder-final.js index 1d90c19..3550031 100644 --- a/test/test-class_builder-final.js +++ b/test/test-class_builder-final.js @@ -61,3 +61,35 @@ var common = require( './common' ), assert.fail( 'Should not be able to override final methods' ); } )(); + +/** + * Due to our static implementation (late static binding), the use of 'final' + * with properties doesn't make much sense. We have to deal with different + * problems than languages like Java that truly bind members statically. Consult + * the documentation for rationale. + * + * See also const keyword. + */ +( function testFinalKeywordCannotBeUsedWithProperties() +{ + try + { + // attempt to define a 'final' property (should fail) + builder.build( + { + 'final public foo': 'bar', + } ); + } + catch ( e ) + { + assert.ok( + e.message.search( 'foo' ) !== -1, + "Final property error message contains name of method" + ); + + return; + } + + assert.fail( "Should not be able to use final keyword with properties" ); +} )(); +