From 400ccce081d1370ce2695c457a580ed5bf7a78bf Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Thu, 15 Dec 2016 14:52:37 -0500 Subject: [PATCH] include filename in error/warning objects --- src/index.js | 2 +- src/parse/index.js | 7 ++++--- src/validate/index.js | 10 ++++++---- test/validate.js | 3 ++- 4 files changed, 13 insertions(+), 9 deletions(-) diff --git a/src/index.js b/src/index.js index 4c424e77ba..79a5665dce 100644 --- a/src/index.js +++ b/src/index.js @@ -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; diff --git a/src/parse/index.js b/src/parse/index.js index 60ac377371..30b88ae531 100644 --- a/src/parse/index.js +++ b/src/parse/index.js @@ -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 ) { diff --git a/src/validate/index.js b/src/validate/index.js index 47f440cd4a..dbd2c0de8b 100644 --- a/src/validate/index.js +++ b/src/validate/index.js @@ -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}` }); }, diff --git a/test/validate.js b/test/validate.js index 00f93c9c72..a9e68560f6 100644 --- a/test/validate.js +++ b/test/validate.js @@ -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 );