forked from mgechev/codelyzer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparseUtil.ts
More file actions
78 lines (68 loc) · 1.95 KB
/
parseUtil.ts
File metadata and controls
78 lines (68 loc) · 1.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
export class ParseLocation {
constructor(
public file: ParseSourceFile, public offset: number, public line: number,
public col: number) {}
toString(): string {
return this.offset ? `${this.file.url}@${this.line}:${this.col}` : this.file.url;
}
}
export class ParseSourceFile {
constructor(public content: string, public url: string) {}
}
export class ParseSourceSpan {
constructor(
public start: ParseLocation, public end: ParseLocation, public details: string = null) {}
toString(): string {
return this.start.file.content.substring(this.start.offset, this.end.offset);
}
}
export enum ParseErrorLevel {
WARNING,
FATAL
}
export abstract class ParseError {
constructor(
public span: ParseSourceSpan, public msg: string,
public level: ParseErrorLevel = ParseErrorLevel.FATAL) {}
toString(): string {
var source = this.span.start.file.content;
var ctxStart = this.span.start.offset;
var contextStr = '';
var details = '';
if (ctxStart) {
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);
contextStr = ` ("${context}")`;
}
if (this.span.details) {
details = `, ${this.span.details}`;
}
return `${this.msg}${contextStr}: ${this.span.start}${details}`;
}
}