diff --git a/lib/member_builder.js b/lib/member_builder.js index f4ea3b6..a8dbba1 100644 --- a/lib/member_builder.js +++ b/lib/member_builder.js @@ -90,6 +90,15 @@ exports.buildMethod = function( ); } + // const doesn't make sense for methods; they're always immutable + if ( keywords[ 'const' ] ) + { + throw TypeError( + "Cannot declare method '" + name + "' as constant; keyword is " + + "redundant" + ); + } + // search for any previous instances of this member if ( prev ) { diff --git a/test/test-class_builder-const.js b/test/test-class_builder-const.js new file mode 100644 index 0000000..95602af --- /dev/null +++ b/test/test-class_builder-const.js @@ -0,0 +1,58 @@ +/** + * Tests const keyword + * + * Copyright (C) 2010 Mike Gerwitz + * + * This file is part of ease.js. + * + * ease.js is free software: you can redistribute it and/or modify it under the + * terms of the GNU Lesser General Public License as published by the Free + * Software Foundation, either version 3 of the License, or (at your option) + * any later version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License + * for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program. If not, see . + * + * @author Mike Gerwitz + * @package test + */ + +var common = require( './common' ), + assert = require( 'assert' ), + builder = common.require( 'class_builder' ) +; + + +/** + * The `const' keyword does not make sense with methods, as they are always + * immutable. Methods of a class cannot be redefined after the class definition. + * They may only be overridden by subtypes. + */ +( function testConstKeywordCannotBeUsedWithMethods() +{ + try + { + // attempt to create a constant method (should fail) + builder.build( + { + 'const foo': function() {}, + } ); + } + catch ( e ) + { + assert.ok( + e.message.search( 'foo' ) !== -1, + "Const method error message should contain name of method" + ); + + return; + } + + assert.fail( "Should not be able to declare constant methods" ); +} )(); +