From 934d1e1133c74818421a953398c166f66e2583ef Mon Sep 17 00:00:00 2001 From: Brian Donovan Date: Sat, 5 Dec 2015 18:15:36 -0800 Subject: [PATCH 1/3] Prevent invalid keys from being passed in options. --- src/rollup.js | 24 ++++++++++++++++++++++++ src/utils/validateKeys.js | 11 +++++++++++ 2 files changed, 35 insertions(+) create mode 100644 src/utils/validateKeys.js diff --git a/src/rollup.js b/src/rollup.js index f7a6674..7b11dfb 100644 --- a/src/rollup.js +++ b/src/rollup.js @@ -2,11 +2,33 @@ import Promise from 'es6-promise/lib/es6-promise/promise.js'; import { basename } from './utils/path.js'; import { writeFile } from './utils/fs.js'; import { keys } from './utils/object.js'; +import validateKeys from './utils/validateKeys.js'; import SOURCEMAPPING_URL from './utils/sourceMappingURL.js'; import Bundle from './Bundle.js'; export const VERSION = '<@VERSION@>'; +const ALLOWED_KEYS = [ + 'entry', + 'dest', + 'plugins', + 'external', + 'onwarn', + 'indent', + 'format', + 'moduleName', + 'sourceMap', + 'intro', + 'outro', + 'banner', + 'footer', + 'globals', + 'transform', + 'load', + 'resolveId', + 'resolveExternal' +]; + export function rollup ( options ) { if ( !options || !options.entry ) { return Promise.reject( new Error( 'You must supply options.entry to rollup' ) ); @@ -16,6 +38,8 @@ export function rollup ( options ) { return Promise.reject( new Error( 'The `transform`, `load`, `resolveId` and `resolveExternal` options are deprecated in favour of a unified plugin API. See https://github.com/rollup/rollup/wiki/Plugins for details' ) ); } + validateKeys( options, ALLOWED_KEYS ); + const bundle = new Bundle( options ); return bundle.build().then( () => { diff --git a/src/utils/validateKeys.js b/src/utils/validateKeys.js new file mode 100644 index 0000000..26391fd --- /dev/null +++ b/src/utils/validateKeys.js @@ -0,0 +1,11 @@ +import { keys } from './object.js'; + +export default function validateKeys ( object, allowedKeys ) { + const actualKeys = keys( object ); + + actualKeys.forEach( key => { + if ( allowedKeys.indexOf( key ) < 0 ) { + throw new Error( `Unexpected key '${ key }' found, expected one of: ${ allowedKeys.join( ', ' ) }` ); + } + }); +} From dfd921e54f0f4d15cf1935a6f74dc23642d7d1ce Mon Sep 17 00:00:00 2001 From: Brian Donovan Date: Tue, 8 Dec 2015 22:14:53 -0800 Subject: [PATCH 2/3] Always fail `rollup` with a rejected promise, not a thrown error. --- src/rollup.js | 6 +++++- src/utils/validateKeys.js | 8 +++++--- test/test.js | 8 ++++++++ 3 files changed, 18 insertions(+), 4 deletions(-) diff --git a/src/rollup.js b/src/rollup.js index 7b11dfb..11f1c68 100644 --- a/src/rollup.js +++ b/src/rollup.js @@ -38,7 +38,11 @@ export function rollup ( options ) { return Promise.reject( new Error( 'The `transform`, `load`, `resolveId` and `resolveExternal` options are deprecated in favour of a unified plugin API. See https://github.com/rollup/rollup/wiki/Plugins for details' ) ); } - validateKeys( options, ALLOWED_KEYS ); + const error = validateKeys( options, ALLOWED_KEYS ); + + if ( error ) { + return Promise.reject( error ); + } const bundle = new Bundle( options ); diff --git a/src/utils/validateKeys.js b/src/utils/validateKeys.js index 26391fd..ec172f5 100644 --- a/src/utils/validateKeys.js +++ b/src/utils/validateKeys.js @@ -3,9 +3,11 @@ import { keys } from './object.js'; export default function validateKeys ( object, allowedKeys ) { const actualKeys = keys( object ); - actualKeys.forEach( key => { + for ( let key of actualKeys ) { if ( allowedKeys.indexOf( key ) < 0 ) { - throw new Error( `Unexpected key '${ key }' found, expected one of: ${ allowedKeys.join( ', ' ) }` ); + return new Error( + `Unexpected key '${ key }' found, expected one of: ${ allowedKeys.join( ', ' ) }` + ); } - }); + } } diff --git a/test/test.js b/test/test.js index bd76009..5ee72ba 100644 --- a/test/test.js +++ b/test/test.js @@ -69,6 +69,14 @@ describe( 'rollup', function () { assert.equal( 'You must supply options.entry to rollup', err.message ); }); }); + + it( 'fails with invalid keys', function () { + return rollup.rollup({ entry: 'x', plUgins: [] }).then( function () { + throw new Error( 'Missing expected error' ); + }, function ( err ) { + assert.equal( 'Unexpected key \'plUgins\' found, expected one of: entry, dest, plugins, external, onwarn, indent, format, moduleName, sourceMap, intro, outro, banner, footer, globals, transform, load, resolveId, resolveExternal', err.message ); + }); + }); }); describe( 'bundle.write()', function () { From 3c5d099cc7000fdf85995d382809a6fb774059a4 Mon Sep 17 00:00:00 2001 From: Rich-Harris Date: Tue, 22 Dec 2015 10:37:18 -0500 Subject: [PATCH 3/3] avoid for...of --- src/utils/validateKeys.js | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/utils/validateKeys.js b/src/utils/validateKeys.js index ec172f5..3ff51cd 100644 --- a/src/utils/validateKeys.js +++ b/src/utils/validateKeys.js @@ -3,8 +3,12 @@ import { keys } from './object.js'; export default function validateKeys ( object, allowedKeys ) { const actualKeys = keys( object ); - for ( let key of actualKeys ) { - if ( allowedKeys.indexOf( key ) < 0 ) { + let i = actualKeys.length; + + while ( i-- ) { + const key = actualKeys[i]; + + if ( allowedKeys.indexOf( key ) === -1 ) { return new Error( `Unexpected key '${ key }' found, expected one of: ${ allowedKeys.join( ', ' ) }` );