codeFrameFormatter.js 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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 codeFrame = require("babel-code-frame");
  22. var chalk_1 = require("chalk");
  23. var Utils = require("../utils");
  24. var Formatter = /** @class */ (function (_super) {
  25. tslib_1.__extends(Formatter, _super);
  26. function Formatter() {
  27. return _super !== null && _super.apply(this, arguments) || this;
  28. }
  29. /* tslint:enable:object-literal-sort-keys */
  30. Formatter.prototype.format = function (failures) {
  31. if (typeof failures[0] === "undefined") {
  32. return "\n";
  33. }
  34. failures = this.sortFailures(failures);
  35. var outputLines = [];
  36. var currentFile;
  37. for (var _i = 0, failures_1 = failures; _i < failures_1.length; _i++) {
  38. var failure = failures_1[_i];
  39. var fileName = failure.getFileName();
  40. // Output the name of each file once
  41. if (currentFile !== fileName) {
  42. outputLines.push("");
  43. outputLines.push(fileName);
  44. currentFile = fileName;
  45. }
  46. var failureString = failure.getFailure();
  47. failureString = failure.getRuleSeverity() === "warning" ? chalk_1.default.yellow(failureString) : chalk_1.default.red(failureString);
  48. // Rule
  49. var ruleName = failure.getRuleName();
  50. ruleName = chalk_1.default.gray("(" + ruleName + ")");
  51. // Frame
  52. var lineAndCharacter = failure.getStartPosition().getLineAndCharacter();
  53. var frame = codeFrame(failure.getRawLines(), lineAndCharacter.line + 1, // babel-code-frame is 1 index
  54. lineAndCharacter.character, {
  55. forceColor: chalk_1.default.enabled,
  56. highlightCode: true,
  57. });
  58. // Ouput
  59. outputLines.push(failureString + " " + ruleName);
  60. outputLines.push(frame);
  61. outputLines.push("");
  62. }
  63. // Removes initial blank line
  64. if (outputLines[0] === "") {
  65. outputLines.shift();
  66. }
  67. return outputLines.join("\n") + "\n";
  68. };
  69. /* tslint:disable:object-literal-sort-keys */
  70. Formatter.metadata = {
  71. formatterName: "codeFrame",
  72. description: "Framed formatter which creates a frame of error code.",
  73. descriptionDetails: Utils.dedent(templateObject_1 || (templateObject_1 = tslib_1.__makeTemplateObject(["\n Prints syntax highlighted code in a frame with a pointer to where\n exactly lint error is happening."], ["\n Prints syntax highlighted code in a frame with a pointer to where\n exactly lint error is happening."]))),
  74. sample: Utils.dedent(templateObject_2 || (templateObject_2 = tslib_1.__makeTemplateObject(["\n src/components/Payment.tsx\n Parentheses are required around the parameters of an arrow function definition (arrow-parens)\n 21 | public componentDidMount() {\n 22 | this.input.focus();\n > 23 | loadStripe().then(Stripe => Stripe.pay());\n | ^\n 24 | }\n 25 |\n 26 | public render() {"], ["\n src/components/Payment.tsx\n Parentheses are required around the parameters of an arrow function definition (arrow-parens)\n 21 | public componentDidMount() {\n 22 | this.input.focus();\n > 23 | loadStripe().then(Stripe => Stripe.pay());\n | ^\n 24 | }\n 25 |\n 26 | public render() {"]))),
  75. consumer: "human",
  76. };
  77. return Formatter;
  78. }(abstractFormatter_1.AbstractFormatter));
  79. exports.Formatter = Formatter;
  80. var templateObject_1, templateObject_2;