1
0
Fork 0

Methods cannot be declared as constant

closure/master
Mike Gerwitz 2011-05-19 07:54:51 -04:00
parent c29cf0e66d
commit f3c1d0f9af
2 changed files with 67 additions and 0 deletions

View File

@ -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 )
{

View File

@ -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 <http://www.gnu.org/licenses/>.
*
* @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" );
} )();