UNPKG

angular2

Version:

Angular 2 - a web framework for modern web apps

70 lines (69 loc) 2.15 kB
export class ParseLocation { constructor(file, offset, line, col) { this.file = file; this.offset = offset; this.line = line; this.col = col; } toString() { return `${this.file.url}@${this.line}:${this.col}`; } } export class ParseSourceFile { constructor(content, url) { this.content = content; this.url = url; } } export class ParseSourceSpan { constructor(start, end) { this.start = start; this.end = end; } toString() { return this.start.file.content.substring(this.start.offset, this.end.offset); } } export var ParseErrorLevel; (function (ParseErrorLevel) { ParseErrorLevel[ParseErrorLevel["WARNING"] = 0] = "WARNING"; ParseErrorLevel[ParseErrorLevel["FATAL"] = 1] = "FATAL"; })(ParseErrorLevel || (ParseErrorLevel = {})); export class ParseError { constructor(span, msg, level = ParseErrorLevel.FATAL) { this.span = span; this.msg = msg; this.level = level; } toString() { var source = this.span.start.file.content; var ctxStart = this.span.start.offset; if (ctxStart > source.length - 1) { ctxStart = source.length - 1; } var ctxEnd = ctxStart; var ctxLen = 0; var ctxLines = 0; while (ctxLen < 100 && ctxStart > 0) { ctxStart--; ctxLen++; if (source[ctxStart] == "\n") { if (++ctxLines == 3) { break; } } } ctxLen = 0; ctxLines = 0; while (ctxLen < 100 && ctxEnd < source.length - 1) { ctxEnd++; ctxLen++; if (source[ctxEnd] == "\n") { if (++ctxLines == 3) { break; } } } let context = source.substring(ctxStart, this.span.start.offset) + '[ERROR ->]' + source.substring(this.span.start.offset, ctxEnd + 1); return `${this.msg} ("${context}"): ${this.span.start}`; } }