From a10cf82a12dfb56df4c4d22d31aa3dfd19930cd5 Mon Sep 17 00:00:00 2001 From: Mike Gerwitz Date: Wed, 21 Dec 2011 20:12:05 -0500 Subject: [PATCH] Abstract member declaration parameter name restrictions now apply to all abstract member declarations, not just interfaces --- lib/interface.js | 17 ----------------- lib/util.js | 28 ++++++++++++++++++++++++++++ test/test-interface-name.js | 24 ------------------------ test/test-util-prop-parse.js | 25 +++++++++++++++++++++++++ 4 files changed, 53 insertions(+), 41 deletions(-) diff --git a/lib/interface.js b/lib/interface.js index 35efa7d..0f2b6ae 100644 --- a/lib/interface.js +++ b/lib/interface.js @@ -235,23 +235,6 @@ var extend = ( function( extending ) ); } - var dfn = value.definition, - i = dfn.length; - - // only permit valid names for argument list (in the future, - // we may add additional functionality, so it's important to - // restrict this as much as possible for the time being) - while ( i-- ) - { - if ( dfn[ i ].match( /^[a-z][a-z0-9]*$/i ) === null ) - { - throw SyntaxError( - iname + " member " + name + - " contains invalid argument: '" + name + "'" - ); - } - } - member_builder.buildMethod( members, null, name, value, keywords ); diff --git a/lib/util.js b/lib/util.js index 04f5cd4..5b6e5fb 100644 --- a/lib/util.js +++ b/lib/util.js @@ -313,6 +313,7 @@ exports.propParse = function( data, options ) ); } + verifyAbstractNames( name, value ); value = exports.createAbstractMethod.apply( this, value ); } @@ -349,6 +350,33 @@ exports.propParse = function( data, options ) }; +/** + * Only permit valid names for parameter list + * + * In the future, we may add additional functionality, so it's important to + * restrict this as much as possible for the time being. + * + * @param {string} name name of abstract member (for error) + * @param {Object} params parameter list to check + * + * @return {undefined} + */ +function verifyAbstractNames( name, params ) +{ + var i = params.length; + while ( i-- ) + { + if ( params[ i ].match( /^[a-z][a-z0-9]*$/i ) === null ) + { + throw SyntaxError( + "Member " + name + " contains invalid parameter: '" + + params[ i ] + "'" + ); + } + } +} + + /** * Creates an abstract method * diff --git a/test/test-interface-name.js b/test/test-interface-name.js index 5a21efe..ba1bd09 100644 --- a/test/test-interface-name.js +++ b/test/test-interface-name.js @@ -243,27 +243,3 @@ var common = require( './common' ), } } )(); - -/** - * At this point in time, we are unsure what we will allow within interface - * definitions in the future (e.g. possible type hinting). As such, we will - * simply allow only valid variable names for now (like a function definition). - */ -( function testTriggersErrorIfInvalidVarNamesAreUsedAsArgumentNames() -{ - assert['throws']( function() - { - Interface( { foo: [ 'invalid name' ] } ); - }, SyntaxError, 'Only var names should be permitted in interface dfns' ); - - assert['throws']( function() - { - Interface( { foo: [ '1invalid' ] } ); - }, SyntaxError, 'Only var names should be permitted in interface dfns: 2' ); - - assert.doesNotThrow( function() - { - Interface( { foo: [ 'valid' ] } ); - }, SyntaxError, 'Valid var names as args should not throw exceptions' ); -} )(); - diff --git a/test/test-util-prop-parse.js b/test/test-util-prop-parse.js index dc9cc02..73009d2 100644 --- a/test/test-util-prop-parse.js +++ b/test/test-util-prop-parse.js @@ -184,3 +184,28 @@ assert.equal( "propParse should ignore prototype properties of instances" ); + +/** + * At this point in time, we are unsure what we will allow within abstract + * member declarations in the future (e.g. possible type hinting). As such, we + * will simply allow only valid variable names for now (like a function + * definition). + */ +( function testTriggersErrorIfInvalidVarNamesAreUsedAsParameterNames() +{ + assert['throws']( function() + { + util.propParse( { 'abstract foo': [ 'invalid name' ] }, {} ); + }, SyntaxError, 'Only var names should be permitted in interface dfns' ); + + assert['throws']( function() + { + util.propParse( { 'abstract foo': [ '1invalid' ] }, {} ); + }, SyntaxError, 'Only var names should be permitted in interface dfns: 2' ); + + assert.doesNotThrow( function() + { + util.propParse( { 'abstract foo': [ 'valid' ] }, {} ); + }, SyntaxError, 'Valid var names as args should not throw exceptions' ); +} )(); +