UNPKG

6.71 kBJavaScriptView Raw
1/*
2 * Copyright 2016 Palantir Technologies, Inc.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16"use strict";
17Object.defineProperty(exports, "__esModule", { value: true });
18var lines_1 = require("./lines");
19var lintError_1 = require("./lintError");
20/**
21 * Takes the full text of a .lint file and returns the contents of the file
22 * with all error markup removed
23 */
24function 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}
30exports.removeErrorMarkup = removeErrorMarkup;
31/* tslint:disable:object-literal-sort-keys */
32/**
33 * Takes the full text of a .lint file and returns an array of LintErrors
34 * corresponding to the error markup in the file.
35 */
36function 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 // errorLineForCodeLine[5] contains all the ErrorLine objects associated with the 5th line of code, for example
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 // for each line of code...
58 errorLinesForCodeLines.forEach(function (errorLinesForLineOfCode, lineNo) {
59 // for each error marking on that line...
60 while (errorLinesForLineOfCode.length > 0) {
61 var errorLine = errorLinesForLineOfCode.shift();
62 var errorStartPos = { line: lineNo, col: errorLine.startCol };
63 // if the error starts and ends on this line, add it now to list of errors
64 if (errorLine instanceof lines_1.EndErrorLine) {
65 addError(errorLine, errorStartPos, lineNo);
66 // if the error is the start of a multiline error
67 }
68 else if (errorLine instanceof lines_1.MultilineErrorLine) {
69 // iterate through the MultilineErrorLines until we get to an EndErrorLine
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 // if end of multiline error, add it it list of errors
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}
89exports.parseErrorsFromMarkup = parseErrorsFromMarkup;
90function 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 // single line error
99 errorLinesForCodeText[startPos.line].push(new lines_1.EndErrorLine(startPos.col, endPos.col, message));
100 }
101 else {
102 // multiline error
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}
113exports.createMarkupFromErrors = createMarkupFromErrors;
114/* tslint:enable:object-literal-sort-keys */
115function 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}
123function 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}
136function isValidErrorMarkupContinuation(errorLinesForCodeLines, lineNo) {
137 return lineNo < errorLinesForCodeLines.length
138 && errorLinesForCodeLines[lineNo].length !== 0
139 && errorLinesForCodeLines[lineNo][0].startCol === 0;
140}