1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 | "use strict";
|
17 | Object.defineProperty(exports, "__esModule", { value: true });
|
18 | var lines_1 = require("./lines");
|
19 | var lintError_1 = require("./lintError");
|
20 |
|
21 |
|
22 |
|
23 |
|
24 | function removeErrorMarkup(text) {
|
25 | var textWithMarkup = text.split("\n");
|
26 | var lines = textWithMarkup.map(lines_1.parseLine);
|
27 | var codeText = lines.filter(function (line) { return (line instanceof lines_1.CodeLine); }).map(function (line) { return line.contents; });
|
28 | return codeText.join("\n");
|
29 | }
|
30 | exports.removeErrorMarkup = removeErrorMarkup;
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 | function parseErrorsFromMarkup(text) {
|
37 | var textWithMarkup = text.split("\n");
|
38 | var lines = textWithMarkup.map(lines_1.parseLine);
|
39 | if (lines.length > 0 && !(lines[0] instanceof lines_1.CodeLine)) {
|
40 | throw lintError_1.lintSyntaxError("text cannot start with an error mark line.");
|
41 | }
|
42 | var messageSubstitutionLines = lines.filter(function (l) { return l instanceof lines_1.MessageSubstitutionLine; });
|
43 | var messageSubstitutions = new Map(messageSubstitutionLines.map(function (_a) {
|
44 | var key = _a.key, message = _a.message;
|
45 | return [key, message];
|
46 | }));
|
47 |
|
48 | var errorLinesForCodeLines = createCodeLineNoToErrorsMap(lines);
|
49 | var lintErrors = [];
|
50 | function addError(errorLine, errorStartPos, lineNo) {
|
51 | lintErrors.push({
|
52 | startPos: errorStartPos,
|
53 | endPos: { line: lineNo, col: errorLine.endCol },
|
54 | message: messageSubstitutions.get(errorLine.message) || errorLine.message,
|
55 | });
|
56 | }
|
57 |
|
58 | errorLinesForCodeLines.forEach(function (errorLinesForLineOfCode, lineNo) {
|
59 |
|
60 | while (errorLinesForLineOfCode.length > 0) {
|
61 | var errorLine = errorLinesForLineOfCode.shift();
|
62 | var errorStartPos = { line: lineNo, col: errorLine.startCol };
|
63 |
|
64 | if (errorLine instanceof lines_1.EndErrorLine) {
|
65 | addError(errorLine, errorStartPos, lineNo);
|
66 |
|
67 | }
|
68 | else if (errorLine instanceof lines_1.MultilineErrorLine) {
|
69 |
|
70 | for (var nextLineNo = lineNo + 1;; ++nextLineNo) {
|
71 | if (!isValidErrorMarkupContinuation(errorLinesForCodeLines, nextLineNo)) {
|
72 | throw lintError_1.lintSyntaxError("Error mark starting at " + errorStartPos.line + ":" + errorStartPos.col + " does not end correctly.");
|
73 | }
|
74 | else {
|
75 | var nextErrorLine = errorLinesForCodeLines[nextLineNo].shift();
|
76 |
|
77 | if (nextErrorLine instanceof lines_1.EndErrorLine) {
|
78 | addError(nextErrorLine, errorStartPos, nextLineNo);
|
79 | break;
|
80 | }
|
81 | }
|
82 | }
|
83 | }
|
84 | }
|
85 | });
|
86 | lintErrors.sort(lintError_1.errorComparator);
|
87 | return lintErrors;
|
88 | }
|
89 | exports.parseErrorsFromMarkup = parseErrorsFromMarkup;
|
90 | function createMarkupFromErrors(code, lintErrors) {
|
91 | lintErrors.sort(lintError_1.errorComparator);
|
92 | var codeText = code.split("\n");
|
93 | var errorLinesForCodeText = codeText.map(function () { return []; });
|
94 | for (var _i = 0, lintErrors_1 = lintErrors; _i < lintErrors_1.length; _i++) {
|
95 | var error = lintErrors_1[_i];
|
96 | var startPos = error.startPos, endPos = error.endPos, message = error.message;
|
97 | if (startPos.line === endPos.line) {
|
98 |
|
99 | errorLinesForCodeText[startPos.line].push(new lines_1.EndErrorLine(startPos.col, endPos.col, message));
|
100 | }
|
101 | else {
|
102 |
|
103 | errorLinesForCodeText[startPos.line].push(new lines_1.MultilineErrorLine(startPos.col));
|
104 | for (var lineNo = startPos.line + 1; lineNo < endPos.line; ++lineNo) {
|
105 | errorLinesForCodeText[lineNo].push(new lines_1.MultilineErrorLine(0));
|
106 | }
|
107 | errorLinesForCodeText[endPos.line].push(new lines_1.EndErrorLine(0, endPos.col, message));
|
108 | }
|
109 | }
|
110 | var finalText = combineCodeTextAndErrorLines(codeText, errorLinesForCodeText);
|
111 | return finalText.join("\n");
|
112 | }
|
113 | exports.createMarkupFromErrors = createMarkupFromErrors;
|
114 |
|
115 | function combineCodeTextAndErrorLines(codeText, errorLinesForCodeText) {
|
116 | return codeText.reduce(function (resultText, code, i) {
|
117 | resultText.push(code);
|
118 | var errorPrintLines = errorLinesForCodeText[i].map(function (line) { return lines_1.printLine(line, code); }).filter(function (line) { return line !== null; });
|
119 | resultText.push.apply(resultText, errorPrintLines);
|
120 | return resultText;
|
121 | }, []);
|
122 | }
|
123 | function createCodeLineNoToErrorsMap(lines) {
|
124 | var errorLinesForCodeLine = [];
|
125 | for (var _i = 0, lines_2 = lines; _i < lines_2.length; _i++) {
|
126 | var line = lines_2[_i];
|
127 | if (line instanceof lines_1.CodeLine) {
|
128 | errorLinesForCodeLine.push([]);
|
129 | }
|
130 | else if (line instanceof lines_1.ErrorLine) {
|
131 | errorLinesForCodeLine[errorLinesForCodeLine.length - 1].push(line);
|
132 | }
|
133 | }
|
134 | return errorLinesForCodeLine;
|
135 | }
|
136 | function isValidErrorMarkupContinuation(errorLinesForCodeLines, lineNo) {
|
137 | return lineNo < errorLinesForCodeLines.length
|
138 | && errorLinesForCodeLines[lineNo].length !== 0
|
139 | && errorLinesForCodeLines[lineNo][0].startCol === 0;
|
140 | }
|