From 2d91a221aa7d48697aa8cab564044daffff9d846 Mon Sep 17 00:00:00 2001 From: Mike Gerwitz Date: Thu, 19 May 2011 19:07:31 -0400 Subject: [PATCH] Const keyword now results in static property --- lib/class_builder.js | 26 ++++++++++++++++++++++---- test/test-class_builder-const.js | 19 +++++++++++++++++++ 2 files changed, 41 insertions(+), 4 deletions(-) diff --git a/lib/class_builder.js b/lib/class_builder.js index dbd5119..868bef4 100644 --- a/lib/class_builder.js +++ b/lib/class_builder.js @@ -470,7 +470,7 @@ function buildMembers( property: function( name, value, keywords ) { - var dest = ( keywords[ 'static' ] ) ? sprops : prop_init; + var dest = ( keywordStatic( keywords ) ) ? sprops : prop_init; // build a new property, passing in the other members to compare // against for preventing nonsensical overrides @@ -481,7 +481,7 @@ function buildMembers( getter: function( name, value, keywords ) { - var dest = ( keywords[ 'static' ] ) ? smethods : members; + var dest = ( keywordStatic( keywords ) ) ? smethods : members; member_builder.buildGetter( dest, null, name, value, keywords @@ -490,7 +490,7 @@ function buildMembers( setter: function( name, value, keywords ) { - var dest = ( keywords[ 'static' ] ) ? smethods : members; + var dest = ( keywordStatic( keywords ) ) ? smethods : members; member_builder.buildSetter( dest, null, name, value, keywords @@ -499,7 +499,7 @@ function buildMembers( method: function( name, func, is_abstract, keywords ) { - var dest = ( keywords[ 'static' ] ) ? smethods : members; + var dest = ( keywordStatic( keywords ) ) ? smethods : members; // constructor check if ( public_methods[ name ] === true ) @@ -536,6 +536,24 @@ function buildMembers( } +/** + * Determines if the given keywords should result in a static member + * + * A member will be considered static if the static or const keywords are given. + * + * @param {Object} keywords keywords to scan + * + * @return {bool} true if to be static, otherwise false + */ +function keywordStatic( keywords ) +{ + return ( keywords[ 'static' ] || keywords[ 'const' ] ) + ? true + : false + ; +} + + /** * Attaches __initProps() method to the class prototype * diff --git a/test/test-class_builder-const.js b/test/test-class_builder-const.js index 87817d8..59fd8de 100644 --- a/test/test-class_builder-const.js +++ b/test/test-class_builder-const.js @@ -84,3 +84,22 @@ var common = require( './common' ), assert.fail( "Should not be able to use static keyword with const" ); } )(); + +/** + * The const keyword should result in a static property. The rationale for this + * is that, if a value is constant, then instances do not make sense. + */ +( function testConstKeywordDeclaresPropertiesAsStatic() +{ + var val = 'baz', + Foo = builder.build( + { + 'const foo': val, + } ) + ; + + assert.equal( val, Foo.$('foo'), + "Const keyword should declare properties as static" + ); +} )(); +