CodeframeFormatter.js 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. "use strict";
  2. var __assign = (this && this.__assign) || function () {
  3. __assign = Object.assign || function(t) {
  4. for (var s, i = 1, n = arguments.length; i < n; i++) {
  5. s = arguments[i];
  6. for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
  7. t[p] = s[p];
  8. }
  9. return t;
  10. };
  11. return __assign.apply(this, arguments);
  12. };
  13. var __importStar = (this && this.__importStar) || function (mod) {
  14. if (mod && mod.__esModule) return mod;
  15. var result = {};
  16. if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
  17. result["default"] = mod;
  18. return result;
  19. };
  20. var __importDefault = (this && this.__importDefault) || function (mod) {
  21. return (mod && mod.__esModule) ? mod : { "default": mod };
  22. };
  23. Object.defineProperty(exports, "__esModule", { value: true });
  24. var os = __importStar(require("os"));
  25. var fs = __importStar(require("fs"));
  26. var chalk_1 = __importDefault(require("chalk"));
  27. var FsHelper_1 = require("../FsHelper");
  28. var issue_1 = require("../issue");
  29. var InternalFormatter_1 = require("./InternalFormatter");
  30. var code_frame_1 = require("@babel/code-frame");
  31. function createCodeframeFormatter(options) {
  32. return function codeframeFormatter(issue) {
  33. var color = {
  34. message: issue.severity === issue_1.IssueSeverity.WARNING
  35. ? chalk_1.default.bold.yellow
  36. : chalk_1.default.bold.red,
  37. position: chalk_1.default.dim
  38. };
  39. if (issue.origin === issue_1.IssueOrigin.INTERNAL) {
  40. return InternalFormatter_1.createInternalFormatter()(issue);
  41. }
  42. var file = issue.file;
  43. var source = file && FsHelper_1.fileExistsSync(file) && fs.readFileSync(file, 'utf-8');
  44. var frame = '';
  45. if (source) {
  46. frame = code_frame_1.codeFrameColumns(source, {
  47. start: {
  48. // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
  49. line: issue.line,
  50. column: issue.character
  51. }
  52. }, __assign({ highlightCode: true }, (options || {})))
  53. .split('\n')
  54. .map(function (line) { return ' ' + line; })
  55. .join(os.EOL);
  56. }
  57. var lines = [
  58. color.message(issue.severity.toUpperCase() + " in " + issue.file + "(" + issue.line + "," + issue.character + "):"),
  59. color.position(issue.line + ":" + issue.character + " " + issue.message)
  60. ];
  61. if (frame) {
  62. lines.push(frame);
  63. }
  64. return lines.join(os.EOL);
  65. };
  66. }
  67. exports.createCodeframeFormatter = createCodeframeFormatter;
  68. //# sourceMappingURL=CodeframeFormatter.js.map