mirror of https://github.com/sveltejs/svelte
commit
5d02b9e090
@ -1,35 +0,0 @@
|
||||
import { locate } from 'locate-character';
|
||||
import getCodeFrame from '../utils/getCodeFrame';
|
||||
|
||||
export default class CompileError extends Error {
|
||||
frame: string;
|
||||
loc: { line: number; column: number };
|
||||
end: { line: number; column: number };
|
||||
pos: number;
|
||||
filename: string;
|
||||
|
||||
constructor(
|
||||
message: string,
|
||||
template: string,
|
||||
startPos: number,
|
||||
filename: string,
|
||||
endPos: number = startPos
|
||||
) {
|
||||
super(message);
|
||||
|
||||
const start = locate(template, startPos);
|
||||
const end = locate(template, endPos);
|
||||
|
||||
this.loc = { line: start.line + 1, column: start.column };
|
||||
this.end = { line: end.line + 1, column: end.column };
|
||||
this.pos = startPos;
|
||||
this.filename = filename;
|
||||
|
||||
this.frame = getCodeFrame(template, start.line, start.column);
|
||||
}
|
||||
|
||||
public toString = () => {
|
||||
return `${this.message} (${this.loc.line}:${this.loc.column})\n${this
|
||||
.frame}`;
|
||||
}
|
||||
}
|
@ -0,0 +1,37 @@
|
||||
import { locate } from 'locate-character';
|
||||
import getCodeFrame from '../utils/getCodeFrame';
|
||||
|
||||
class CompileError extends Error {
|
||||
loc: { line: number, column: number };
|
||||
end: { line: number, column: number };
|
||||
pos: number;
|
||||
filename: string;
|
||||
frame: string;
|
||||
|
||||
toString() {
|
||||
return `${this.message} (${this.loc.line}:${this.loc.column})\n${this.frame}`;
|
||||
}
|
||||
}
|
||||
|
||||
export default function error(message: string, props: {
|
||||
name: string,
|
||||
source: string,
|
||||
filename: string,
|
||||
start: number,
|
||||
end?: number
|
||||
}) {
|
||||
const error = new CompileError(message);
|
||||
error.name = props.name;
|
||||
|
||||
const start = locate(props.source, props.start);
|
||||
const end = locate(props.source, props.end || props.start);
|
||||
|
||||
error.loc = { line: start.line + 1, column: start.column };
|
||||
error.end = { line: end.line + 1, column: end.column };
|
||||
error.pos = props.start;
|
||||
error.filename = props.filename;
|
||||
|
||||
error.frame = getCodeFrame(props.source, start.line, start.column);
|
||||
|
||||
throw error;
|
||||
}
|
Loading…
Reference in new issue