Skip to content

Commit

Permalink
include filename in error/warning objects
Browse files Browse the repository at this point in the history
  • Loading branch information
Rich-Harris committed Dec 15, 2016
1 parent 7928c9c commit e016b20
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
7 changes: 4 additions & 3 deletions src/parse/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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' );
}
Expand All @@ -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 ) {
Expand Down
10 changes: 6 additions & 4 deletions src/validate/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand All @@ -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;
}
Expand All @@ -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}`
});
},
Expand Down
3 changes: 2 additions & 1 deletion test/validate.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand Down

0 comments on commit e016b20

Please sign in to comment.