Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix CompileError constructor #1296

Merged
merged 1 commit into from
Apr 1, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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