stylishFormatter.js 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. "use strict";
  2. /**
  3. * @license
  4. * Copyright 2013 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. var abstractFormatter_1 = require("../language/formatter/abstractFormatter");
  21. var chalk_1 = require("chalk");
  22. var Utils = require("../utils");
  23. var Formatter = /** @class */ (function (_super) {
  24. tslib_1.__extends(Formatter, _super);
  25. function Formatter() {
  26. return _super !== null && _super.apply(this, arguments) || this;
  27. }
  28. /* tslint:enable:object-literal-sort-keys */
  29. Formatter.prototype.format = function (failures) {
  30. failures = this.sortFailures(failures);
  31. var outputLines = this.mapToMessages(failures);
  32. // Removes initial blank line
  33. if (outputLines[0] === "") {
  34. outputLines.shift();
  35. }
  36. return outputLines.join("\n") + "\n";
  37. };
  38. Formatter.prototype.mapToMessages = function (failures) {
  39. if (failures.length === 0) {
  40. return [];
  41. }
  42. var outputLines = [];
  43. var positionMaxSize = this.getPositionMaxSize(failures);
  44. var ruleMaxSize = this.getRuleMaxSize(failures);
  45. var currentFile;
  46. for (var _i = 0, failures_1 = failures; _i < failures_1.length; _i++) {
  47. var failure = failures_1[_i];
  48. var fileName = failure.getFileName();
  49. var lineAndCharacter = failure.getStartPosition().getLineAndCharacter();
  50. var positionTuple = lineAndCharacter.line + 1 + ":" + (lineAndCharacter.character + 1);
  51. // Output the name of each file once
  52. if (currentFile !== fileName) {
  53. outputLines.push("");
  54. outputLines.push("" + fileName + chalk_1.default.hidden(":" + positionTuple));
  55. currentFile = fileName;
  56. }
  57. var failureString = failure.getFailure();
  58. failureString = chalk_1.default.yellow(failureString);
  59. // Rule
  60. var ruleName = failure.getRuleName();
  61. ruleName = this.pad(ruleName, ruleMaxSize);
  62. ruleName = chalk_1.default.grey(ruleName);
  63. // Lines
  64. positionTuple = this.pad(positionTuple, positionMaxSize);
  65. positionTuple = failure.getRuleSeverity() === "warning"
  66. ? chalk_1.default.blue(failure.getRuleSeverity().toUpperCase() + ": " + positionTuple)
  67. : chalk_1.default.red(failure.getRuleSeverity().toUpperCase() + ": " + positionTuple);
  68. // Output
  69. var output = positionTuple + " " + ruleName + " " + failureString;
  70. outputLines.push(output);
  71. }
  72. return outputLines;
  73. };
  74. Formatter.prototype.pad = function (str, len) {
  75. var padder = Array(len + 1).join(" ");
  76. return (str + padder).substring(0, padder.length);
  77. };
  78. Formatter.prototype.getPositionMaxSize = function (failures) {
  79. var positionMaxSize = 0;
  80. for (var _i = 0, failures_2 = failures; _i < failures_2.length; _i++) {
  81. var failure = failures_2[_i];
  82. var lineAndCharacter = failure.getStartPosition().getLineAndCharacter();
  83. var positionSize = (lineAndCharacter.line + 1 + ":" + (lineAndCharacter.character + 1)).length;
  84. if (positionSize > positionMaxSize) {
  85. positionMaxSize = positionSize;
  86. }
  87. }
  88. return positionMaxSize;
  89. };
  90. Formatter.prototype.getRuleMaxSize = function (failures) {
  91. var ruleMaxSize = 0;
  92. for (var _i = 0, failures_3 = failures; _i < failures_3.length; _i++) {
  93. var failure = failures_3[_i];
  94. var ruleSize = failure.getRuleName().length;
  95. if (ruleSize > ruleMaxSize) {
  96. ruleMaxSize = ruleSize;
  97. }
  98. }
  99. return ruleMaxSize;
  100. };
  101. /* tslint:disable:object-literal-sort-keys */
  102. Formatter.metadata = {
  103. formatterName: "stylish",
  104. description: "Human-readable formatter which creates stylish messages.",
  105. descriptionDetails: Utils.dedent(templateObject_1 || (templateObject_1 = tslib_1.__makeTemplateObject(["\n The output matches what is produced by ESLint's stylish formatter.\n Its readability is enhanced through spacing and colouring."], ["\n The output matches what is produced by ESLint's stylish formatter.\n Its readability is enhanced through spacing and colouring."]))),
  106. sample: Utils.dedent(templateObject_2 || (templateObject_2 = tslib_1.__makeTemplateObject(["\n myFile.ts\n 1:14 semicolon Missing semicolon"], ["\n myFile.ts\n 1:14 semicolon Missing semicolon"]))),
  107. consumer: "human",
  108. };
  109. return Formatter;
  110. }(abstractFormatter_1.AbstractFormatter));
  111. exports.Formatter = Formatter;
  112. var templateObject_1, templateObject_2;