From 76bc7361d399749209cef779c590b55dc3d97f83 Mon Sep 17 00:00:00 2001 From: Mike Gerwitz Date: Sun, 22 May 2011 22:11:57 -0400 Subject: [PATCH] Implemented GH#2 - Keyword restrictions; throw exception when unknown keywords are used --- lib/prop_parser.js | 30 +++++++++++++++++-- test/test-prop_parser-parse-keywords.js | 39 +++++++++++++++++++++++++ 2 files changed, 67 insertions(+), 2 deletions(-) diff --git a/lib/prop_parser.js b/lib/prop_parser.js index 1940d2d..617d472 100644 --- a/lib/prop_parser.js +++ b/lib/prop_parser.js @@ -22,6 +22,20 @@ * @package core */ +/** + * Known (permitted) keywords + * @type {Object.} + */ +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; } } diff --git a/test/test-prop_parser-parse-keywords.js b/test/test-prop_parser-parse-keywords.js index 6bc4234..0c49fd8 100644 --- a/test/test-prop_parser-parse-keywords.js +++ b/test/test-prop_parser-parse-keywords.js @@ -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" ); +} )(); +