From a9262f45ea43662f2ea8e93729a06b9263be7289 Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Thu, 15 Dec 2016 12:26:21 -0500 Subject: [PATCH 1/3] intercept parse errors with options.onerror --- src/index.js | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/src/index.js b/src/index.js index 173f53203716..4c424e77ba94 100644 --- a/src/index.js +++ b/src/index.js @@ -16,13 +16,25 @@ function normalizeOptions ( options ) { } else { console.warn( warning.message ); // eslint-disable-line no-console } + }, + + onerror: error => { + throw error; } }, options ); } export function compile ( source, _options ) { const options = normalizeOptions( _options ); - const parsed = parse( source, options ); + + let parsed; + + try { + parsed = parse( source ); + } catch ( err ) { + options.onerror( err ); + return; + } const { names } = validate( parsed, source, options ); From c09a02caf2c9867684cd0c0612020f0823c9951b Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Thu, 15 Dec 2016 12:36:17 -0500 Subject: [PATCH 2/3] increase patch coverage --- test/parse.js | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/test/parse.js b/test/parse.js index 0be35e08f9a6..a4eb7cd133f9 100644 --- a/test/parse.js +++ b/test/parse.js @@ -33,4 +33,17 @@ describe( 'parse', () => { } }); }); + + it( 'handles errors with options.onerror', () => { + let errored = false; + + svelte.compile( `

unclosed`, { + onerror ( err ) { + errored = true; + assert.equal( err.message, `Unexpected end of input` ); + } + }); + + assert.ok( errored ); + }); }); From c453ae5dd0a0d749c7c70b7ef8e3e51e25368aca Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Thu, 15 Dec 2016 12:45:29 -0500 Subject: [PATCH 3/3] increase patch coverage, again --- test/parse.js | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/test/parse.js b/test/parse.js index a4eb7cd133f9..dc4d8f2e6adb 100644 --- a/test/parse.js +++ b/test/parse.js @@ -46,4 +46,10 @@ describe( 'parse', () => { assert.ok( errored ); }); + + it( 'throws without options.onerror', () => { + assert.throws( () => { + svelte.compile( `

unclosed` ); + }, /Unexpected end of input/ ); + }); });