From a82b2e9e05c3118a86271ebd23a614e43c857739 Mon Sep 17 00:00:00 2001 From: Rich-Harris Date: Mon, 21 Nov 2016 07:22:07 -0500 Subject: [PATCH] add parser.error coverage --- compiler/parse/index.js | 8 +++++++- .../error-unexpected-end-of-input/error.json | 8 ++++++++ .../error-unexpected-end-of-input/input.html | 1 + test/test.js | 17 ++++++++++++++--- 4 files changed, 30 insertions(+), 4 deletions(-) create mode 100644 test/parser/error-unexpected-end-of-input/error.json create mode 100644 test/parser/error-unexpected-end-of-input/input.html diff --git a/compiler/parse/index.js b/compiler/parse/index.js index b03067d38b..06c288e833 100644 --- a/compiler/parse/index.js +++ b/compiler/parse/index.js @@ -33,8 +33,10 @@ function ParseError ( message, template, index ) { }) .join( '\n' ); + this.name = 'ParseError'; this.message = `${message} (${line + 1}:${column})\n${frame}`; - this.loc = { line, column }; + this.loc = { line: line + 1, column }; + this.pos = index; this.shortMessage = message; } @@ -123,6 +125,10 @@ export default function parse ( template ) { state = state( parser ) || fragment; } + if ( state !== fragment || parser.stack.length > 1 ) { + parser.error( 'Unexpected end of input' ); + } + // trim unnecessary whitespace while ( parser.html.children.length ) { const firstChild = parser.html.children[0]; diff --git a/test/parser/error-unexpected-end-of-input/error.json b/test/parser/error-unexpected-end-of-input/error.json new file mode 100644 index 0000000000..e171d2a2e8 --- /dev/null +++ b/test/parser/error-unexpected-end-of-input/error.json @@ -0,0 +1,8 @@ +{ + "message": "Unexpected end of input", + "loc": { + "line": 1, + "column": 5 + }, + "pos": 5 +} diff --git a/test/parser/error-unexpected-end-of-input/input.html b/test/parser/error-unexpected-end-of-input/input.html new file mode 100644 index 0000000000..e54a273243 --- /dev/null +++ b/test/parser/error-unexpected-end-of-input/input.html @@ -0,0 +1 @@ +
diff --git a/test/test.js b/test/test.js index e57b20c8a1..d3cfa957b8 100644 --- a/test/test.js +++ b/test/test.js @@ -32,10 +32,21 @@ describe( 'svelte', () => { ( solo ? it.only : it )( dir, () => { const input = fs.readFileSync( `test/parser/${dir}/input.html`, 'utf-8' ).trim(); - const actual = parse( input ); - const expected = require( `./parser/${dir}/output.json` ); - assert.deepEqual( actual, expected ); + try { + const actual = parse( input ); + const expected = require( `./parser/${dir}/output.json` ); + + assert.deepEqual( actual, expected ); + } catch ( err ) { + if ( err.name !== 'ParseError' ) throw err; + + const expected = require( `./parser/${dir}/error.json` ); + + assert.equal( err.shortMessage, expected.message ); + assert.deepEqual( err.loc, expected.loc ); + assert.equal( err.pos, expected.pos ); + } }); }); });