2017-12-24 03:19:47 +01:00
|
|
|
import {
|
|
|
|
Range
|
|
|
|
} from "./ast";
|
2017-09-28 13:08:25 +02:00
|
|
|
|
2017-12-24 03:19:47 +01:00
|
|
|
import {
|
|
|
|
DiagnosticCode,
|
|
|
|
diagnosticCodeToString
|
|
|
|
} from "./diagnosticMessages.generated";
|
|
|
|
|
|
|
|
import {
|
|
|
|
isLineBreak
|
|
|
|
} from "./util/charcode";
|
|
|
|
|
|
|
|
export {
|
|
|
|
DiagnosticCode,
|
|
|
|
diagnosticCodeToString
|
|
|
|
} from "./diagnosticMessages.generated";
|
2017-09-28 13:08:25 +02:00
|
|
|
|
|
|
|
export enum DiagnosticCategory {
|
|
|
|
INFO,
|
|
|
|
WARNING,
|
|
|
|
ERROR
|
|
|
|
}
|
|
|
|
|
|
|
|
export function diagnosticCategoryToString(category: DiagnosticCategory): string {
|
2017-11-17 14:33:51 +01:00
|
|
|
switch (category) {
|
|
|
|
case DiagnosticCategory.INFO: return "INFO";
|
|
|
|
case DiagnosticCategory.WARNING: return "WARNING";
|
|
|
|
case DiagnosticCategory.ERROR: return "ERROR";
|
|
|
|
default: return "";
|
|
|
|
}
|
2017-09-28 13:08:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const colorBlue: string = "\u001b[93m";
|
|
|
|
const colorYellow: string = "\u001b[93m";
|
|
|
|
const colorRed: string = "\u001b[91m";
|
|
|
|
const colorReset: string = "\u001b[0m";
|
|
|
|
|
|
|
|
export function diagnosticCategoryToColor(category: DiagnosticCategory): string {
|
2017-11-17 14:33:51 +01:00
|
|
|
switch (category) {
|
|
|
|
case DiagnosticCategory.INFO: return colorBlue;
|
|
|
|
case DiagnosticCategory.WARNING: return colorYellow;
|
|
|
|
case DiagnosticCategory.ERROR: return colorRed;
|
|
|
|
default: return "";
|
|
|
|
}
|
2017-09-28 13:08:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
export class DiagnosticMessage {
|
|
|
|
|
|
|
|
code: i32;
|
|
|
|
category: DiagnosticCategory;
|
|
|
|
message: string;
|
|
|
|
range: Range | null = null;
|
|
|
|
|
|
|
|
constructor(code: i32, category: DiagnosticCategory, message: string) {
|
|
|
|
this.code = code;
|
|
|
|
this.category = category;
|
|
|
|
this.message = message;
|
|
|
|
}
|
|
|
|
|
2018-02-25 00:13:39 +01:00
|
|
|
static create(
|
|
|
|
code: DiagnosticCode,
|
|
|
|
category: DiagnosticCategory,
|
|
|
|
arg0: string | null = null,
|
2018-02-28 18:38:42 +01:00
|
|
|
arg1: string | null = null,
|
|
|
|
arg2: string | null = null
|
2018-02-25 00:13:39 +01:00
|
|
|
): DiagnosticMessage {
|
2017-12-28 04:09:40 +01:00
|
|
|
var message = diagnosticCodeToString(code);
|
2018-02-25 00:13:39 +01:00
|
|
|
if (arg0 != null) message = message.replace("{0}", arg0);
|
|
|
|
if (arg1 != null) message = message.replace("{1}", arg1);
|
2018-02-28 18:38:42 +01:00
|
|
|
if (arg2 != null) message = message.replace("{2}", arg2);
|
2017-09-28 13:08:25 +02:00
|
|
|
return new DiagnosticMessage(code, category, message);
|
|
|
|
}
|
|
|
|
|
2018-02-25 00:13:39 +01:00
|
|
|
static createInfo(
|
|
|
|
code: DiagnosticCode,
|
|
|
|
arg0: string | null = null,
|
|
|
|
arg1: string | null = null
|
|
|
|
): DiagnosticMessage {
|
2017-09-28 13:08:25 +02:00
|
|
|
return DiagnosticMessage.create(code, DiagnosticCategory.INFO, arg0, arg1);
|
|
|
|
}
|
|
|
|
|
2018-02-25 00:13:39 +01:00
|
|
|
static createWarning(
|
|
|
|
code: DiagnosticCode,
|
|
|
|
arg0: string | null = null,
|
|
|
|
arg1: string | null = null
|
|
|
|
): DiagnosticMessage {
|
2017-09-28 13:08:25 +02:00
|
|
|
return DiagnosticMessage.create(code, DiagnosticCategory.WARNING, arg0, arg1);
|
|
|
|
}
|
|
|
|
|
2018-02-25 00:13:39 +01:00
|
|
|
static createError(
|
|
|
|
code: DiagnosticCode,
|
|
|
|
arg0: string | null = null,
|
|
|
|
arg1: string | null = null
|
|
|
|
): DiagnosticMessage {
|
2017-09-28 13:08:25 +02:00
|
|
|
return DiagnosticMessage.create(code, DiagnosticCategory.ERROR, arg0, arg1);
|
|
|
|
}
|
|
|
|
|
|
|
|
withRange(range: Range): this {
|
|
|
|
this.range = range;
|
|
|
|
return this;
|
|
|
|
}
|
2017-12-14 11:55:35 +01:00
|
|
|
|
|
|
|
toString(): string {
|
2018-02-25 00:13:39 +01:00
|
|
|
if (this.range) {
|
|
|
|
return (
|
|
|
|
diagnosticCategoryToString(this.category) +
|
|
|
|
" " +
|
|
|
|
this.code.toString(10) +
|
|
|
|
": \"" +
|
|
|
|
this.message +
|
|
|
|
"\" in " +
|
|
|
|
this.range.source.normalizedPath +
|
|
|
|
" @ " +
|
|
|
|
this.range.start.toString(10) +
|
|
|
|
"," +
|
|
|
|
this.range.end.toString(10)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
return (
|
|
|
|
diagnosticCategoryToString(this.category) +
|
|
|
|
" " +
|
|
|
|
this.code.toString(10) +
|
|
|
|
": " +
|
|
|
|
this.message
|
|
|
|
);
|
2017-12-14 11:55:35 +01:00
|
|
|
}
|
2017-09-28 13:08:25 +02:00
|
|
|
}
|
|
|
|
|
2018-02-25 00:13:39 +01:00
|
|
|
export function formatDiagnosticMessage(
|
|
|
|
message: DiagnosticMessage,
|
|
|
|
useColors: bool = false,
|
|
|
|
showContext: bool = false
|
|
|
|
): string {
|
2017-09-28 13:08:25 +02:00
|
|
|
// format context first (uses same string builder)
|
2017-12-28 04:09:40 +01:00
|
|
|
var context = "";
|
2018-02-25 00:13:39 +01:00
|
|
|
if (message.range && showContext) {
|
2017-12-24 03:19:47 +01:00
|
|
|
context = formatDiagnosticContext(message.range, useColors);
|
2018-02-25 00:13:39 +01:00
|
|
|
}
|
2017-09-28 13:08:25 +02:00
|
|
|
|
|
|
|
// general information
|
2018-02-16 11:55:13 +01:00
|
|
|
var sb: string[] = [];
|
2017-09-28 13:08:25 +02:00
|
|
|
if (useColors) sb.push(diagnosticCategoryToColor(message.category));
|
|
|
|
sb.push(diagnosticCategoryToString(message.category));
|
|
|
|
if (useColors) sb.push(colorReset);
|
2018-01-28 06:18:27 +01:00
|
|
|
sb.push(message.code < 1000 ? " AS" : " TS");
|
2017-12-11 02:03:15 +01:00
|
|
|
sb.push(message.code.toString(10));
|
2017-09-28 13:08:25 +02:00
|
|
|
sb.push(": ");
|
|
|
|
sb.push(message.message);
|
|
|
|
|
|
|
|
// range information if available
|
|
|
|
if (message.range) {
|
2017-12-28 04:09:40 +01:00
|
|
|
var range = message.range;
|
2017-09-28 13:08:25 +02:00
|
|
|
if (showContext) {
|
|
|
|
sb.push("\n");
|
|
|
|
sb.push(context);
|
|
|
|
}
|
|
|
|
sb.push("\n");
|
|
|
|
sb.push(" in ");
|
2018-02-02 03:07:54 +01:00
|
|
|
sb.push(range.source.normalizedPath);
|
2017-09-28 13:08:25 +02:00
|
|
|
sb.push("(");
|
2018-01-27 16:23:00 +01:00
|
|
|
sb.push(range.line.toString(10));
|
2017-09-28 13:08:25 +02:00
|
|
|
sb.push(",");
|
2018-01-27 16:23:00 +01:00
|
|
|
sb.push(range.column.toString(10));
|
2017-09-28 13:08:25 +02:00
|
|
|
sb.push(")");
|
|
|
|
}
|
|
|
|
return sb.join("");
|
|
|
|
}
|
|
|
|
|
|
|
|
export function formatDiagnosticContext(range: Range, useColors: bool = false): string {
|
2017-12-28 04:09:40 +01:00
|
|
|
var text = range.source.text;
|
|
|
|
var len = text.length;
|
|
|
|
var start = range.start;
|
|
|
|
var end = range.end;
|
2018-02-25 00:13:39 +01:00
|
|
|
while (start > 0 && !isLineBreak(text.charCodeAt(start - 1))) {
|
2017-09-28 13:08:25 +02:00
|
|
|
start--;
|
2018-02-25 00:13:39 +01:00
|
|
|
}
|
|
|
|
while (end < len && !isLineBreak(text.charCodeAt(end))) {
|
2017-09-28 13:08:25 +02:00
|
|
|
end++;
|
2018-02-25 00:13:39 +01:00
|
|
|
}
|
2018-02-16 11:55:13 +01:00
|
|
|
var sb: string[] = [
|
|
|
|
"\n ",
|
|
|
|
text.substring(start, end),
|
|
|
|
"\n "
|
|
|
|
];
|
2017-09-28 13:08:25 +02:00
|
|
|
while (start < range.start) {
|
|
|
|
sb.push(" ");
|
|
|
|
start++;
|
|
|
|
}
|
|
|
|
if (useColors) sb.push(colorRed);
|
|
|
|
if (range.start == range.end) {
|
|
|
|
sb.push("^");
|
2018-02-25 00:13:39 +01:00
|
|
|
} else {
|
|
|
|
while (start++ < range.end) {
|
|
|
|
sb.push("~");
|
|
|
|
}
|
|
|
|
}
|
2017-09-28 13:08:25 +02:00
|
|
|
if (useColors) sb.push(colorReset);
|
|
|
|
return sb.join("");
|
|
|
|
}
|
|
|
|
|
|
|
|
export abstract class DiagnosticEmitter {
|
|
|
|
|
|
|
|
diagnostics: DiagnosticMessage[];
|
2018-02-04 07:51:40 +01:00
|
|
|
// silentDiagnostics: bool = false;
|
2017-09-28 13:08:25 +02:00
|
|
|
|
|
|
|
constructor(diagnostics: DiagnosticMessage[] | null = null) {
|
|
|
|
this.diagnostics = diagnostics ? <DiagnosticMessage[]>diagnostics : new Array();
|
|
|
|
}
|
|
|
|
|
2018-02-25 00:13:39 +01:00
|
|
|
emitDiagnostic(
|
|
|
|
code: DiagnosticCode,
|
|
|
|
category: DiagnosticCategory,
|
|
|
|
range: Range,
|
|
|
|
arg0: string | null = null,
|
2018-02-28 18:38:42 +01:00
|
|
|
arg1: string | null = null,
|
|
|
|
arg2: string | null = null
|
2018-02-25 00:13:39 +01:00
|
|
|
) {
|
2018-02-28 18:38:42 +01:00
|
|
|
var message = DiagnosticMessage.create(code, category, arg0, arg1, arg2).withRange(range);
|
2017-09-28 13:08:25 +02:00
|
|
|
this.diagnostics.push(message);
|
2018-02-09 02:31:48 +01:00
|
|
|
// console.log(formatDiagnosticMessage(message, true, true) + "\n"); // temporary
|
|
|
|
// console.log(<string>new Error("stack").stack);
|
2017-09-28 13:08:25 +02:00
|
|
|
}
|
|
|
|
|
2018-02-28 18:38:42 +01:00
|
|
|
error(
|
|
|
|
code: DiagnosticCode,
|
|
|
|
range: Range,
|
|
|
|
arg0: string | null = null,
|
|
|
|
arg1: string | null = null,
|
|
|
|
arg2: string | null = null
|
|
|
|
): void {
|
|
|
|
this.emitDiagnostic(code, DiagnosticCategory.ERROR, range, arg0, arg1, arg2);
|
2017-09-28 13:08:25 +02:00
|
|
|
}
|
|
|
|
|
2018-02-28 18:38:42 +01:00
|
|
|
info(
|
|
|
|
code: DiagnosticCode,
|
|
|
|
range: Range,
|
|
|
|
arg0: string | null = null,
|
|
|
|
arg1: string | null = null,
|
|
|
|
arg2: string | null = null
|
|
|
|
): void {
|
|
|
|
this.emitDiagnostic(code, DiagnosticCategory.INFO, range, arg0, arg1, arg2);
|
2017-09-28 13:08:25 +02:00
|
|
|
}
|
|
|
|
|
2018-02-28 18:38:42 +01:00
|
|
|
warning(
|
|
|
|
code: DiagnosticCode,
|
|
|
|
range: Range,
|
|
|
|
arg0: string | null = null,
|
|
|
|
arg1: string | null = null,
|
|
|
|
arg2: string | null = null
|
|
|
|
): void {
|
|
|
|
this.emitDiagnostic(code, DiagnosticCategory.WARNING, range, arg0, arg1, arg2);
|
2017-09-28 13:08:25 +02:00
|
|
|
}
|
|
|
|
}
|