Implemented GH#2 - Keyword restrictions; throw exception when unknown keywords are used
parent
cf344186fc
commit
76bc7361d3
|
@ -22,6 +22,20 @@
|
|||
* @package core
|
||||
*/
|
||||
|
||||
/**
|
||||
* Known (permitted) keywords
|
||||
* @type {Object.<string,boolean>}
|
||||
*/
|
||||
var _keywords = {
|
||||
'public': true,
|
||||
'protected': true,
|
||||
'private': true,
|
||||
'static': true,
|
||||
'final': true,
|
||||
'abstract': true,
|
||||
'const': true,
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* Parses property keywords
|
||||
|
@ -46,10 +60,22 @@ exports.parseKeywords = function ( prop )
|
|||
keywords = prop.split( /\s+/ );
|
||||
name = keywords.pop();
|
||||
|
||||
var i = keywords.length;
|
||||
var i = keywords.length,
|
||||
keyword = '';
|
||||
|
||||
while ( i-- )
|
||||
{
|
||||
keyword_obj[ keywords[ i ] ] = true;
|
||||
keyword = keywords[ i ];
|
||||
|
||||
// ensure the keyword is recognized
|
||||
if ( !_keywords[ keyword ] )
|
||||
{
|
||||
throw Error(
|
||||
"Unexpected keyword for '" + name + "': " + keyword
|
||||
);
|
||||
}
|
||||
|
||||
keyword_obj[ keyword ] = true;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -52,3 +52,42 @@ var common = require( './common' ),
|
|||
);
|
||||
} )();
|
||||
|
||||
|
||||
/**
|
||||
* In an effort to prevent unnecessary bugs, notify the user when they use a
|
||||
* keyword that is not recognized.
|
||||
*/
|
||||
( function testOnlyPermitsKnownKeywords()
|
||||
{
|
||||
assert.doesNotThrow( function()
|
||||
{
|
||||
// Odd seeing these all together, isn't it? Note that this is not at all
|
||||
// valid, but the prop parser doesn't care what appears together.
|
||||
parse( 'public protected private static final abstract const var' );
|
||||
}, Error, "Known keywords are permitted by the parser" );
|
||||
|
||||
var oddword = 'foobunny',
|
||||
oddname = 'moobunny';
|
||||
|
||||
try
|
||||
{
|
||||
// remember, the last part of the string is the var name and is not
|
||||
// considered to be a keyword
|
||||
parse( oddword + ' ' + oddname );
|
||||
}
|
||||
catch ( e )
|
||||
{
|
||||
assert.ok( e.message.search( oddword ) !== -1,
|
||||
"Error message contains unrecognized keyword"
|
||||
);
|
||||
|
||||
assert.ok( e.message.search( oddname ) !== -1,
|
||||
"Error message contains name"
|
||||
);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
assert.fail( "Should not permit unknown keywords" );
|
||||
} )();
|
||||
|
||||
|
|
Loading…
Reference in New Issue