add parser.error coverage

pull/31/head
Rich-Harris 8 years ago
parent 615a0018fd
commit a82b2e9e05

@ -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];

@ -0,0 +1,8 @@
{
"message": "Unexpected end of input",
"loc": {
"line": 1,
"column": 5
},
"pos": 5
}

@ -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 );
}
});
});
});

Loading…
Cancel
Save