fix CompileError constructor

pull/1296/head
Rich-Harris 7 years ago
parent ae25641f8b
commit f6dd6edc9d

@ -8,19 +8,7 @@ import reservedNames from '../utils/reservedNames';
import fullCharCodeAt from '../utils/fullCharCodeAt';
import hash from '../utils/hash';
import { Node, Parsed } from '../interfaces';
import CompileError from '../utils/CompileError';
class ParseError extends CompileError {
constructor(
message: string,
template: string,
index: number,
filename: string
) {
super(message, template, index, filename);
this.name = 'ParseError';
}
}
import error from '../utils/error';
interface ParserOptions {
filename?: string;
@ -109,7 +97,12 @@ export class Parser {
}
error(message: string, index = this.index) {
throw new ParseError(message, this.template, index, this.filename);
error(message, {
name: 'ParseError',
source: this.template,
start: index,
filename: this.filename
});
}
eat(str: string, required?: boolean, message?: string) {

@ -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;
}

@ -2,22 +2,10 @@ import validateJs from './js/index';
import validateHtml from './html/index';
import { getLocator, Location } from 'locate-character';
import getCodeFrame from '../utils/getCodeFrame';
import CompileError from '../utils/CompileError';
import error from '../utils/error';
import Stylesheet from '../css/Stylesheet';
import { Node, Parsed, CompileOptions, Warning } from '../interfaces';
class ValidationError extends CompileError {
constructor(
message: string,
template: string,
pos: { start: number, end: number },
filename: string,
) {
super(message, template, pos.start, filename, pos.end);
this.name = 'ValidationError';
}
}
export class Validator {
readonly source: string;
readonly filename: string;
@ -71,7 +59,13 @@ export class Validator {
}
error(message: string, pos: { start: number, end: number }) {
throw new ValidationError(message, this.source, pos, this.filename);
error(message, {
name: 'ValidationError',
source: this.source,
start: pos.start,
end: pos.end,
filename: this.filename
});
}
warn(message: string, pos: { start: number, end: number }) {

Loading…
Cancel
Save