include filename in error/warning objects

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

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

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

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

@ -9,7 +9,8 @@ describe( 'validate', () => {
const solo = exists( `test/validator/${dir}/solo` ); const solo = exists( `test/validator/${dir}/solo` );
( solo ? it.only : it )( dir, () => { ( 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 { try {
const parsed = svelte.parse( input ); const parsed = svelte.parse( input );

Loading…
Cancel
Save