Skip to content

Commit

Permalink
Merge pull request #1296 from sveltejs/fix-errors
Browse files Browse the repository at this point in the history
fix CompileError constructor
  • Loading branch information
Rich-Harris authored Apr 1, 2018
2 parents f27b29d + f6dd6ed commit 5d02b9e
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 63 deletions.
21 changes: 7 additions & 14 deletions src/parse/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down
35 changes: 0 additions & 35 deletions src/utils/CompileError.ts

This file was deleted.

37 changes: 37 additions & 0 deletions src/utils/error.ts
Original file line number Diff line number Diff line change
@@ -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;
}
22 changes: 8 additions & 14 deletions src/validate/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 }) {
Expand Down

0 comments on commit 5d02b9e

Please sign in to comment.