include filename in error/warning objects

pull/201/head
Rich Harris 8 years ago
parent 7928c9c48d
commit e016b200ef

@ -30,7 +30,7 @@ export function compile ( source, _options ) {
let parsed;
try {
parsed = parse( source );
parsed = parse( source, options );
} catch ( err ) {
options.onerror( err );
return;

@ -5,7 +5,7 @@ import { trimStart, trimEnd } from './utils/trim.js';
import getCodeFrame from '../utils/getCodeFrame.js';
import hash from './utils/hash.js';
function ParseError ( message, template, index ) {
function ParseError ( message, template, index, filename ) {
const { line, column } = locate( template, index );
this.name = 'ParseError';
@ -14,13 +14,14 @@ function ParseError ( message, template, index ) {
this.loc = { line: line + 1, column };
this.pos = index;
this.filename = filename;
}
ParseError.prototype.toString = function () {
return `${this.message} (${this.loc.line}:${this.loc.column})\n${this.frame}`;
};
export default function parse ( template ) {
export default function parse ( template, options = {} ) {
if ( typeof template !== 'string' ) {
throw new TypeError( 'Template must be a string' );
}
@ -41,7 +42,7 @@ export default function parse ( template ) {
},
error ( message, index = this.index ) {
throw new ParseError( message, this.template, index );
throw new ParseError( message, this.template, index, options.filename );
},
eat ( str, required ) {

@ -3,7 +3,7 @@ import validateHtml from './html/index.js';
import { getLocator } from 'locate-character';
import getCodeFrame from '../utils/getCodeFrame.js';
export default function validate ( parsed, source, options ) {
export default function validate ( parsed, source, { onerror, onwarn, filename } ) {
const locator = getLocator( source );
const validator = {
@ -14,11 +14,12 @@ export default function validate ( parsed, source, options ) {
error.frame = getCodeFrame( source, line, column );
error.loc = { line: line + 1, column };
error.pos = pos;
error.filename = filename;
error.toString = () => `${error.message} (${error.loc.line}:${error.loc.column})\n${error.frame}`;
if ( options.onerror ) {
options.onerror( error );
if ( onerror ) {
onerror( error );
} else {
throw error;
}
@ -29,11 +30,12 @@ export default function validate ( parsed, source, options ) {
const frame = getCodeFrame( source, line, column );
options.onwarn({
onwarn({
message,
frame,
loc: { line: line + 1, column },
pos,
filename,
toString: () => `${message} (${line + 1}:${column})\n${frame}`
});
},

@ -9,7 +9,8 @@ describe( 'validate', () => {
const solo = exists( `test/validator/${dir}/solo` );
( solo ? it.only : it )( dir, () => {
const input = fs.readFileSync( `test/validator/${dir}/input.html`, 'utf-8' ).replace( /\s+$/, '' );
const filename = `test/validator/${dir}/input.html`;
const input = fs.readFileSync( filename, 'utf-8' ).replace( /\s+$/, '' );
try {
const parsed = svelte.parse( input );

Loading…
Cancel
Save