parse.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. "use strict";
  2. /*
  3. * Copyright 2016 Palantir Technologies, Inc.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. Object.defineProperty(exports, "__esModule", { value: true });
  18. var semver = require("semver");
  19. var ts = require("typescript");
  20. var util_1 = require("util");
  21. var utils_1 = require("../utils");
  22. var lines_1 = require("./lines");
  23. var lintError_1 = require("./lintError");
  24. var scanner;
  25. function getTypescriptVersionRequirement(text) {
  26. var lines = text.split(/\r?\n/);
  27. var firstLine = lines_1.parseLine(lines[0]);
  28. if (firstLine instanceof lines_1.MessageSubstitutionLine && firstLine.key === "typescript") {
  29. return firstLine.message;
  30. }
  31. return undefined;
  32. }
  33. exports.getTypescriptVersionRequirement = getTypescriptVersionRequirement;
  34. function getNormalizedTypescriptVersion() {
  35. var tsVersion = new semver.SemVer(ts.version);
  36. // remove prerelease suffix when matching to allow testing with nightly builds
  37. return tsVersion.major + "." + tsVersion.minor + "." + tsVersion.patch;
  38. }
  39. exports.getNormalizedTypescriptVersion = getNormalizedTypescriptVersion;
  40. function preprocessDirectives(text) {
  41. if (!/^#(?:if|else|endif)\b/m.test(text)) {
  42. return text; // If there are no directives, just return the input unchanged
  43. }
  44. var tsVersion = getNormalizedTypescriptVersion();
  45. var lines = text.split(/\n/);
  46. var result = [];
  47. var collecting = true;
  48. var state = 0 /* Initial */;
  49. for (var _i = 0, lines_2 = lines; _i < lines_2.length; _i++) {
  50. var line = lines_2[_i];
  51. if (line.startsWith("#if typescript")) {
  52. if (state !== 0 /* Initial */) {
  53. throw lintError_1.lintSyntaxError("#if directives cannot be nested");
  54. }
  55. state = 1 /* If */;
  56. collecting = semver.satisfies(tsVersion, line.slice("#if typescript".length).trim());
  57. }
  58. else if (/^#else\s*$/.test(line)) {
  59. if (state !== 1 /* If */) {
  60. throw lintError_1.lintSyntaxError("unexpected #else");
  61. }
  62. state = 2 /* Else */;
  63. collecting = !collecting;
  64. }
  65. else if (/^#endif\s*$/.test(line)) {
  66. if (state === 0 /* Initial */) {
  67. throw lintError_1.lintSyntaxError("unexpected #endif");
  68. }
  69. state = 0 /* Initial */;
  70. collecting = true;
  71. }
  72. else if (collecting) {
  73. result.push(line);
  74. }
  75. }
  76. if (state !== 0 /* Initial */) {
  77. throw lintError_1.lintSyntaxError("expected #endif");
  78. }
  79. return result.join("\n");
  80. }
  81. exports.preprocessDirectives = preprocessDirectives;
  82. /**
  83. * Takes the full text of a .lint file and returns the contents of the file
  84. * with all error markup removed
  85. */
  86. function removeErrorMarkup(text) {
  87. var textWithMarkup = text.split("\n");
  88. var lines = textWithMarkup.map(lines_1.parseLine);
  89. var codeText = lines.filter(function (line) { return (line instanceof lines_1.CodeLine); }).map(function (line) { return line.contents; });
  90. return codeText.join("\n");
  91. }
  92. exports.removeErrorMarkup = removeErrorMarkup;
  93. /* tslint:disable:object-literal-sort-keys */
  94. /**
  95. * Takes the full text of a .lint file and returns an array of LintErrors
  96. * corresponding to the error markup in the file.
  97. */
  98. function parseErrorsFromMarkup(text) {
  99. var textWithMarkup = text.split("\n");
  100. var lines = textWithMarkup.map(lines_1.parseLine);
  101. if (lines.length > 0 && !(lines[0] instanceof lines_1.CodeLine)) {
  102. throw lintError_1.lintSyntaxError("text cannot start with an error mark line.");
  103. }
  104. var messageSubstitutionLines = lines.filter(function (l) { return l instanceof lines_1.MessageSubstitutionLine; });
  105. var messageSubstitutions = new Map();
  106. for (var _i = 0, messageSubstitutionLines_1 = messageSubstitutionLines; _i < messageSubstitutionLines_1.length; _i++) {
  107. var _a = messageSubstitutionLines_1[_i], key = _a.key, message = _a.message;
  108. messageSubstitutions.set(key, formatMessage(messageSubstitutions, message));
  109. }
  110. // errorLineForCodeLine[5] contains all the ErrorLine objects associated with the 5th line of code, for example
  111. var errorLinesForCodeLines = createCodeLineNoToErrorsMap(lines);
  112. var lintErrors = [];
  113. function addError(errorLine, errorStartPos, lineNo) {
  114. lintErrors.push({
  115. startPos: errorStartPos,
  116. endPos: { line: lineNo, col: errorLine.endCol },
  117. message: substituteMessage(messageSubstitutions, errorLine.message),
  118. });
  119. }
  120. // for each line of code...
  121. errorLinesForCodeLines.forEach(function (errorLinesForLineOfCode, lineNo) {
  122. // for each error marking on that line...
  123. while (errorLinesForLineOfCode.length > 0) {
  124. var errorLine = errorLinesForLineOfCode.shift();
  125. var errorStartPos = { line: lineNo, col: errorLine.startCol };
  126. // if the error starts and ends on this line, add it now to list of errors
  127. if (errorLine instanceof lines_1.EndErrorLine) {
  128. addError(errorLine, errorStartPos, lineNo);
  129. // if the error is the start of a multiline error
  130. }
  131. else if (errorLine instanceof lines_1.MultilineErrorLine) {
  132. // iterate through the MultilineErrorLines until we get to an EndErrorLine
  133. for (var nextLineNo = lineNo + 1;; ++nextLineNo) {
  134. if (!isValidErrorMarkupContinuation(errorLinesForCodeLines, nextLineNo)) {
  135. throw lintError_1.lintSyntaxError("Error mark starting at " + errorStartPos.line + ":" + errorStartPos.col + " does not end correctly.");
  136. }
  137. else {
  138. var nextErrorLine = errorLinesForCodeLines[nextLineNo].shift();
  139. // if end of multiline error, add it it list of errors
  140. if (nextErrorLine instanceof lines_1.EndErrorLine) {
  141. addError(nextErrorLine, errorStartPos, nextLineNo);
  142. break;
  143. }
  144. }
  145. }
  146. }
  147. }
  148. });
  149. lintErrors.sort(lintError_1.errorComparator);
  150. return lintErrors;
  151. }
  152. exports.parseErrorsFromMarkup = parseErrorsFromMarkup;
  153. /**
  154. * Process `message` as follows:
  155. * - search `substitutions` for an exact match and return the substitution
  156. * - try to format the message when it looks like: name % ('substitution1' [, "substitution2" [, ...]])
  157. * - or return it unchanged
  158. */
  159. function substituteMessage(templates, message) {
  160. var substitution = templates.get(message);
  161. if (substitution !== undefined) {
  162. return substitution;
  163. }
  164. return formatMessage(templates, message);
  165. }
  166. /**
  167. * Tries to format the message when it has the correct format or returns it unchanged: name % ('substitution1' [, "substitution2" [, ...]])
  168. * Where `name` is the name of a message substitution that is used as template.
  169. * If `name` is not found in `templates`, `message` is returned unchanged.
  170. */
  171. function formatMessage(templates, message) {
  172. var formatMatch = /^([-\w]+) % \((.+)\)$/.exec(message);
  173. if (formatMatch !== null) {
  174. var template = templates.get(formatMatch[1]);
  175. if (template !== undefined) {
  176. var formatArgs = parseFormatArguments(formatMatch[2]);
  177. if (formatArgs !== undefined) {
  178. message = util_1.format.apply(void 0, [template].concat(formatArgs));
  179. }
  180. }
  181. }
  182. return message;
  183. }
  184. /**
  185. * Parse a list of comma separated string literals.
  186. * This function bails out if it sees something unexpected.
  187. * Whitespace between tokens is ignored.
  188. * Trailing comma is allowed.
  189. */
  190. function parseFormatArguments(text) {
  191. if (scanner === undefined) {
  192. // once the scanner is created, it is cached for subsequent calls
  193. scanner = ts.createScanner(ts.ScriptTarget.Latest, false);
  194. }
  195. scanner.setText(text);
  196. var result = [];
  197. var expectValue = true;
  198. for (var token = scanner.scan(); token !== ts.SyntaxKind.EndOfFileToken; token = scanner.scan()) {
  199. if (token === ts.SyntaxKind.StringLiteral) {
  200. if (!expectValue) {
  201. return undefined;
  202. }
  203. result.push(scanner.getTokenValue());
  204. expectValue = false;
  205. }
  206. else if (token === ts.SyntaxKind.CommaToken) {
  207. if (expectValue) {
  208. return undefined;
  209. }
  210. expectValue = true;
  211. }
  212. else if (token !== ts.SyntaxKind.WhitespaceTrivia) {
  213. // only ignore whitespace, other trivia like comments makes this function bail out
  214. return undefined;
  215. }
  216. }
  217. return result.length === 0 ? undefined : result;
  218. }
  219. function createMarkupFromErrors(code, lintErrors) {
  220. lintErrors.sort(lintError_1.errorComparator);
  221. var codeText = code.split("\n");
  222. var errorLinesForCodeText = codeText.map(function () { return []; });
  223. for (var _i = 0, lintErrors_1 = lintErrors; _i < lintErrors_1.length; _i++) {
  224. var error = lintErrors_1[_i];
  225. var startPos = error.startPos, endPos = error.endPos, message = error.message;
  226. if (startPos.line === endPos.line) {
  227. // single line error
  228. errorLinesForCodeText[startPos.line].push(new lines_1.EndErrorLine(startPos.col, endPos.col, message));
  229. }
  230. else {
  231. // multiline error
  232. errorLinesForCodeText[startPos.line].push(new lines_1.MultilineErrorLine(startPos.col));
  233. for (var lineNo = startPos.line + 1; lineNo < endPos.line; ++lineNo) {
  234. errorLinesForCodeText[lineNo].push(new lines_1.MultilineErrorLine(0));
  235. }
  236. errorLinesForCodeText[endPos.line].push(new lines_1.EndErrorLine(0, endPos.col, message));
  237. }
  238. }
  239. return utils_1.flatMap(codeText, function (line, i) { return [line].concat(utils_1.mapDefined(errorLinesForCodeText[i], function (err) { return lines_1.printLine(err, line); })); }).join("\n");
  240. }
  241. exports.createMarkupFromErrors = createMarkupFromErrors;
  242. /* tslint:enable:object-literal-sort-keys */
  243. function createCodeLineNoToErrorsMap(lines) {
  244. var errorLinesForCodeLine = [];
  245. for (var _i = 0, lines_3 = lines; _i < lines_3.length; _i++) {
  246. var line = lines_3[_i];
  247. if (line instanceof lines_1.CodeLine) {
  248. errorLinesForCodeLine.push([]);
  249. }
  250. else if (line instanceof lines_1.ErrorLine) {
  251. errorLinesForCodeLine[errorLinesForCodeLine.length - 1].push(line);
  252. }
  253. }
  254. return errorLinesForCodeLine;
  255. }
  256. function isValidErrorMarkupContinuation(errorLinesForCodeLines, lineNo) {
  257. return lineNo < errorLinesForCodeLines.length
  258. && errorLinesForCodeLines[lineNo].length !== 0
  259. && errorLinesForCodeLines[lineNo][0].startCol === 0;
  260. }