Skip to content

Commit

Permalink
Merge pull request #319 from sveltejs/gh-258
Browse files Browse the repository at this point in the history
more informative error message when elements/blocks are left open
  • Loading branch information
Rich-Harris committed Mar 1, 2017
2 parents c2275f3 + f85e343 commit 25d68a3
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 6 deletions.
9 changes: 8 additions & 1 deletion src/parse/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,14 @@ export default function parse ( template, options = {} ) {
state = state( parser ) || fragment;
}

if ( state !== fragment || parser.stack.length > 1 ) {
if ( parser.stack.length > 1 ) {
const current = parser.current();

const type = current.type === 'Element' ? `<${current.name}>` : 'Block';
parser.error( `${type} was left open`, current.start );
}

if ( state !== fragment ) {
parser.error( 'Unexpected end of input' );
}

Expand Down
4 changes: 2 additions & 2 deletions test/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ describe( 'parse', () => {
svelte.compile( `<h1>unclosed`, {
onerror ( err ) {
errored = true;
assert.equal( err.message, `Unexpected end of input` );
assert.equal( err.message, `<h1> was left open` );
}
});

Expand All @@ -55,6 +55,6 @@ describe( 'parse', () => {
it( 'throws without options.onerror', () => {
assert.throws( () => {
svelte.compile( `<h1>unclosed` );
}, /Unexpected end of input/ );
}, /<h1> was left open/ );
});
});
8 changes: 8 additions & 0 deletions test/parser/error-unexpected-end-of-input-d/error.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"message": "Block was left open",
"loc": {
"line": 1,
"column": 0
},
"pos": 0
}
2 changes: 2 additions & 0 deletions test/parser/error-unexpected-end-of-input-d/input.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
{{#if foo}}
<p>foo</p>
6 changes: 3 additions & 3 deletions test/parser/error-unexpected-end-of-input/error.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"message": "Unexpected end of input",
"message": "<div> was left open",
"loc": {
"line": 1,
"column": 5
"column": 0
},
"pos": 5
"pos": 0
}

0 comments on commit 25d68a3

Please sign in to comment.