UNPKG

6.51 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";
17var __extends = (this && this.__extends) || (function () {
18 var extendStatics = Object.setPrototypeOf ||
19 ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
20 function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
21 return function (d, b) {
22 extendStatics(d, b);
23 function __() { this.constructor = d; }
24 d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
25 };
26})();
27Object.defineProperty(exports, "__esModule", { value: true });
28// Use classes here instead of interfaces because we want runtime type data
29var Line = (function () {
30 function Line() {
31 }
32 return Line;
33}());
34exports.Line = Line;
35var CodeLine = (function (_super) {
36 __extends(CodeLine, _super);
37 function CodeLine(contents) {
38 var _this = _super.call(this) || this;
39 _this.contents = contents;
40 return _this;
41 }
42 return CodeLine;
43}(Line));
44exports.CodeLine = CodeLine;
45var MessageSubstitutionLine = (function (_super) {
46 __extends(MessageSubstitutionLine, _super);
47 function MessageSubstitutionLine(key, message) {
48 var _this = _super.call(this) || this;
49 _this.key = key;
50 _this.message = message;
51 return _this;
52 }
53 return MessageSubstitutionLine;
54}(Line));
55exports.MessageSubstitutionLine = MessageSubstitutionLine;
56var ErrorLine = (function (_super) {
57 __extends(ErrorLine, _super);
58 function ErrorLine(startCol) {
59 var _this = _super.call(this) || this;
60 _this.startCol = startCol;
61 return _this;
62 }
63 return ErrorLine;
64}(Line));
65exports.ErrorLine = ErrorLine;
66var MultilineErrorLine = (function (_super) {
67 __extends(MultilineErrorLine, _super);
68 function MultilineErrorLine(startCol) {
69 return _super.call(this, startCol) || this;
70 }
71 return MultilineErrorLine;
72}(ErrorLine));
73exports.MultilineErrorLine = MultilineErrorLine;
74var EndErrorLine = (function (_super) {
75 __extends(EndErrorLine, _super);
76 function EndErrorLine(startCol, endCol, message) {
77 var _this = _super.call(this, startCol) || this;
78 _this.endCol = endCol;
79 _this.message = message;
80 return _this;
81 }
82 return EndErrorLine;
83}(ErrorLine));
84exports.EndErrorLine = EndErrorLine;
85// example matches (between the quotes):
86// " ~~~~~~~~"
87var multilineErrorRegex = /^\s*(~+|~nil)$/;
88// " ~~~~~~~~~ [some error message]"
89var endErrorRegex = /^\s*(~+|~nil)\s*\[(.+)\]\s*$/;
90// "[shortcut]: full messages goes here!! "
91var messageSubstitutionRegex = /^\[([\w\-\_]+?)]: \s*(.+?)\s*$/;
92exports.ZERO_LENGTH_ERROR = "~nil";
93/**
94 * Maps a line of text from a .lint file to an appropriate Line object
95 */
96function parseLine(text) {
97 var multilineErrorMatch = text.match(multilineErrorRegex);
98 if (multilineErrorMatch != null) {
99 var startErrorCol = text.indexOf("~");
100 return new MultilineErrorLine(startErrorCol);
101 }
102 var endErrorMatch = text.match(endErrorRegex);
103 if (endErrorMatch != null) {
104 var squiggles = endErrorMatch[1], message = endErrorMatch[2];
105 var startErrorCol = text.indexOf("~");
106 var zeroLengthError = (squiggles === exports.ZERO_LENGTH_ERROR);
107 var endErrorCol = zeroLengthError ? startErrorCol : text.lastIndexOf("~") + 1;
108 return new EndErrorLine(startErrorCol, endErrorCol, message);
109 }
110 var messageSubstitutionMatch = text.match(messageSubstitutionRegex);
111 if (messageSubstitutionMatch != null) {
112 var key = messageSubstitutionMatch[1], message = messageSubstitutionMatch[2];
113 return new MessageSubstitutionLine(key, message);
114 }
115 // line doesn't match any syntax for error markup, so it's a line of code to be linted
116 return new CodeLine(text);
117}
118exports.parseLine = parseLine;
119/**
120 * Maps a Line object to a matching line of text that could be in a .lint file.
121 * This is almost the inverse of parseLine.
122 * If you ran `printLine(parseLine(someText), code)`, the whitespace in the result may be different than in someText
123 * @param line - A Line object to convert to text
124 * @param code - If line represents error markup, this is the line of code preceding the markup.
125 * Otherwise, this parameter is not required.
126 */
127function printLine(line, code) {
128 if (line instanceof ErrorLine) {
129 if (code == null) {
130 throw new Error("Must supply argument for code parameter when line is an ErrorLine");
131 }
132 var leadingSpaces = " ".repeat(line.startCol);
133 if (line instanceof MultilineErrorLine) {
134 // special case for when the line of code is simply a newline.
135 // use "~nil" to indicate the error continues on that line
136 if (code.length === 0 && line.startCol === 0) {
137 return exports.ZERO_LENGTH_ERROR;
138 }
139 var tildes = "~".repeat(code.length - leadingSpaces.length);
140 return "" + leadingSpaces + tildes;
141 }
142 else if (line instanceof EndErrorLine) {
143 var tildes = "~".repeat(line.endCol - line.startCol);
144 var endSpaces = " ".repeat(code.length - line.endCol);
145 if (tildes.length === 0) {
146 tildes = exports.ZERO_LENGTH_ERROR;
147 // because we add "~nil" we need four less spaces than normal at the end
148 // always make sure we have at least one space though
149 endSpaces = endSpaces.substring(0, Math.max(endSpaces.length - 4, 1));
150 }
151 return "" + leadingSpaces + tildes + endSpaces + " [" + line.message + "]";
152 }
153 }
154 else if (line instanceof MessageSubstitutionLine) {
155 return "[" + line.key + "]: " + line.message;
156 }
157 else if (line instanceof CodeLine) {
158 return line.contents;
159 }
160 return null;
161}
162exports.printLine = printLine;