UI for Zipcoin Blue

lines.js 6.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. "use strict";
  2. /*
  3. * @license
  4. * Copyright 2016 Palantir Technologies, Inc.
  5. *
  6. * Licensed under the Apache License, Version 2.0 (the "License");
  7. * you may not use this file except in compliance with the License.
  8. * You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. Object.defineProperty(exports, "__esModule", { value: true });
  19. var tslib_1 = require("tslib");
  20. // Use classes here instead of interfaces because we want runtime type data
  21. var Line = /** @class */ (function () {
  22. function Line() {
  23. }
  24. return Line;
  25. }());
  26. exports.Line = Line;
  27. var CodeLine = /** @class */ (function (_super) {
  28. tslib_1.__extends(CodeLine, _super);
  29. function CodeLine(contents) {
  30. var _this = _super.call(this) || this;
  31. _this.contents = contents;
  32. return _this;
  33. }
  34. return CodeLine;
  35. }(Line));
  36. exports.CodeLine = CodeLine;
  37. var MessageSubstitutionLine = /** @class */ (function (_super) {
  38. tslib_1.__extends(MessageSubstitutionLine, _super);
  39. function MessageSubstitutionLine(key, message) {
  40. var _this = _super.call(this) || this;
  41. _this.key = key;
  42. _this.message = message;
  43. return _this;
  44. }
  45. return MessageSubstitutionLine;
  46. }(Line));
  47. exports.MessageSubstitutionLine = MessageSubstitutionLine;
  48. var ErrorLine = /** @class */ (function (_super) {
  49. tslib_1.__extends(ErrorLine, _super);
  50. function ErrorLine(startCol) {
  51. var _this = _super.call(this) || this;
  52. _this.startCol = startCol;
  53. return _this;
  54. }
  55. return ErrorLine;
  56. }(Line));
  57. exports.ErrorLine = ErrorLine;
  58. var MultilineErrorLine = /** @class */ (function (_super) {
  59. tslib_1.__extends(MultilineErrorLine, _super);
  60. function MultilineErrorLine(startCol) {
  61. return _super.call(this, startCol) || this;
  62. }
  63. return MultilineErrorLine;
  64. }(ErrorLine));
  65. exports.MultilineErrorLine = MultilineErrorLine;
  66. var EndErrorLine = /** @class */ (function (_super) {
  67. tslib_1.__extends(EndErrorLine, _super);
  68. function EndErrorLine(startCol, endCol, message) {
  69. var _this = _super.call(this, startCol) || this;
  70. _this.endCol = endCol;
  71. _this.message = message;
  72. return _this;
  73. }
  74. return EndErrorLine;
  75. }(ErrorLine));
  76. exports.EndErrorLine = EndErrorLine;
  77. // example matches (between the quotes):
  78. // " ~~~~~~~~"
  79. var multilineErrorRegex = /^\s*(~+|~nil)$/;
  80. // " ~~~~~~~~~ [some error message]"
  81. var endErrorRegex = /^\s*(~+|~nil)\s*\[(.+)\]\s*$/;
  82. // "[shortcut]: full messages goes here!! "
  83. var messageSubstitutionRegex = /^\[([-\w]+?)]: \s*(.+?)\s*$/;
  84. exports.ZERO_LENGTH_ERROR = "~nil";
  85. /**
  86. * Maps a line of text from a .lint file to an appropriate Line object
  87. */
  88. function parseLine(text) {
  89. var multilineErrorMatch = text.match(multilineErrorRegex);
  90. if (multilineErrorMatch !== null) {
  91. var startErrorCol = text.indexOf("~");
  92. return new MultilineErrorLine(startErrorCol);
  93. }
  94. var endErrorMatch = text.match(endErrorRegex);
  95. if (endErrorMatch !== null) {
  96. var squiggles = endErrorMatch[1], message = endErrorMatch[2];
  97. var startErrorCol = text.indexOf("~");
  98. var zeroLengthError = (squiggles === exports.ZERO_LENGTH_ERROR);
  99. var endErrorCol = zeroLengthError ? startErrorCol : text.lastIndexOf("~") + 1;
  100. return new EndErrorLine(startErrorCol, endErrorCol, message);
  101. }
  102. var messageSubstitutionMatch = text.match(messageSubstitutionRegex);
  103. if (messageSubstitutionMatch !== null) {
  104. var key = messageSubstitutionMatch[1], message = messageSubstitutionMatch[2];
  105. return new MessageSubstitutionLine(key, message);
  106. }
  107. // line doesn't match any syntax for error markup, so it's a line of code to be linted
  108. return new CodeLine(text);
  109. }
  110. exports.parseLine = parseLine;
  111. /**
  112. * Maps a Line object to a matching line of text that could be in a .lint file.
  113. * This is almost the inverse of parseLine.
  114. * If you ran `printLine(parseLine(someText), code)`, the whitespace in the result may be different than in someText
  115. * @param line - A Line object to convert to text
  116. * @param code - If line represents error markup, this is the line of code preceding the markup.
  117. * Otherwise, this parameter is not required.
  118. */
  119. function printLine(line, code) {
  120. if (line instanceof ErrorLine) {
  121. if (code === undefined) {
  122. throw new Error("Must supply argument for code parameter when line is an ErrorLine");
  123. }
  124. var leadingSpaces = " ".repeat(line.startCol);
  125. if (line instanceof MultilineErrorLine) {
  126. // special case for when the line of code is simply a newline.
  127. // use "~nil" to indicate the error continues on that line
  128. if (code.length === 0 && line.startCol === 0) {
  129. return exports.ZERO_LENGTH_ERROR;
  130. }
  131. var tildes = "~".repeat(code.length - leadingSpaces.length);
  132. return "" + leadingSpaces + tildes;
  133. }
  134. else if (line instanceof EndErrorLine) {
  135. var tildes = "~".repeat(line.endCol - line.startCol);
  136. if (code.length < line.endCol) {
  137. // Better than crashing in String.repeat
  138. throw new Error("Bad error marker at " + JSON.stringify(line));
  139. }
  140. var endSpaces = " ".repeat(code.length - line.endCol);
  141. if (tildes.length === 0) {
  142. tildes = exports.ZERO_LENGTH_ERROR;
  143. // because we add "~nil" we need four less spaces than normal at the end
  144. // always make sure we have at least one space though
  145. endSpaces = endSpaces.substring(0, Math.max(endSpaces.length - 4, 1));
  146. }
  147. return "" + leadingSpaces + tildes + endSpaces + " [" + line.message + "]";
  148. }
  149. }
  150. else if (line instanceof MessageSubstitutionLine) {
  151. return "[" + line.key + "]: " + line.message;
  152. }
  153. else if (line instanceof CodeLine) {
  154. return line.contents;
  155. }
  156. return undefined;
  157. }
  158. exports.printLine = printLine;