Skip to content

Commit e016b20

Browse files
committed
include filename in error/warning objects
1 parent 7928c9c commit e016b20

File tree

4 files changed

+13
-9
lines changed

4 files changed

+13
-9
lines changed

src/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ export function compile ( source, _options ) {
3030
let parsed;
3131

3232
try {
33-
parsed = parse( source );
33+
parsed = parse( source, options );
3434
} catch ( err ) {
3535
options.onerror( err );
3636
return;

src/parse/index.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { trimStart, trimEnd } from './utils/trim.js';
55
import getCodeFrame from '../utils/getCodeFrame.js';
66
import hash from './utils/hash.js';
77

8-
function ParseError ( message, template, index ) {
8+
function ParseError ( message, template, index, filename ) {
99
const { line, column } = locate( template, index );
1010

1111
this.name = 'ParseError';
@@ -14,13 +14,14 @@ function ParseError ( message, template, index ) {
1414

1515
this.loc = { line: line + 1, column };
1616
this.pos = index;
17+
this.filename = filename;
1718
}
1819

1920
ParseError.prototype.toString = function () {
2021
return `${this.message} (${this.loc.line}:${this.loc.column})\n${this.frame}`;
2122
};
2223

23-
export default function parse ( template ) {
24+
export default function parse ( template, options = {} ) {
2425
if ( typeof template !== 'string' ) {
2526
throw new TypeError( 'Template must be a string' );
2627
}
@@ -41,7 +42,7 @@ export default function parse ( template ) {
4142
},
4243

4344
error ( message, index = this.index ) {
44-
throw new ParseError( message, this.template, index );
45+
throw new ParseError( message, this.template, index, options.filename );
4546
},
4647

4748
eat ( str, required ) {

src/validate/index.js

+6-4
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import validateHtml from './html/index.js';
33
import { getLocator } from 'locate-character';
44
import getCodeFrame from '../utils/getCodeFrame.js';
55

6-
export default function validate ( parsed, source, options ) {
6+
export default function validate ( parsed, source, { onerror, onwarn, filename } ) {
77
const locator = getLocator( source );
88

99
const validator = {
@@ -14,11 +14,12 @@ export default function validate ( parsed, source, options ) {
1414
error.frame = getCodeFrame( source, line, column );
1515
error.loc = { line: line + 1, column };
1616
error.pos = pos;
17+
error.filename = filename;
1718

1819
error.toString = () => `${error.message} (${error.loc.line}:${error.loc.column})\n${error.frame}`;
1920

20-
if ( options.onerror ) {
21-
options.onerror( error );
21+
if ( onerror ) {
22+
onerror( error );
2223
} else {
2324
throw error;
2425
}
@@ -29,11 +30,12 @@ export default function validate ( parsed, source, options ) {
2930

3031
const frame = getCodeFrame( source, line, column );
3132

32-
options.onwarn({
33+
onwarn({
3334
message,
3435
frame,
3536
loc: { line: line + 1, column },
3637
pos,
38+
filename,
3739
toString: () => `${message} (${line + 1}:${column})\n${frame}`
3840
});
3941
},

test/validate.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ describe( 'validate', () => {
99
const solo = exists( `test/validator/${dir}/solo` );
1010

1111
( solo ? it.only : it )( dir, () => {
12-
const input = fs.readFileSync( `test/validator/${dir}/input.html`, 'utf-8' ).replace( /\s+$/, '' );
12+
const filename = `test/validator/${dir}/input.html`;
13+
const input = fs.readFileSync( filename, 'utf-8' ).replace( /\s+$/, '' );
1314

1415
try {
1516
const parsed = svelte.parse( input );

0 commit comments

Comments
 (0)