sourceMappingVisitor.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. "use strict";
  2. var __extends = (this && this.__extends) || (function () {
  3. var extendStatics = function (d, b) {
  4. extendStatics = Object.setPrototypeOf ||
  5. ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
  6. function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
  7. return extendStatics(d, b);
  8. };
  9. return function (d, b) {
  10. extendStatics(d, b);
  11. function __() { this.constructor = d; }
  12. d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  13. };
  14. })();
  15. Object.defineProperty(exports, "__esModule", { value: true });
  16. var source_map_1 = require("source-map");
  17. var tslint_1 = require("tslint");
  18. var LineFeed = 0x0a;
  19. var CarriageReturn = 0x0d;
  20. var MaxAsciiCharacter = 0x7f;
  21. var LineSeparator = 0x2028;
  22. var ParagraphSeparator = 0x2029;
  23. function isLineBreak(ch) {
  24. return ch === LineFeed || ch === CarriageReturn || ch === LineSeparator || ch === ParagraphSeparator;
  25. }
  26. exports.isLineBreak = isLineBreak;
  27. function binarySearch(array, value, comparer, offset) {
  28. if (!array || array.length === 0) {
  29. return -1;
  30. }
  31. var low = offset || 0;
  32. var high = array.length - 1;
  33. comparer = comparer !== undefined ? comparer : function (v1, v2) { return (v1 < v2 ? -1 : v1 > v2 ? 1 : 0); };
  34. while (low <= high) {
  35. var middle = low + ((high - low) >> 1);
  36. var midValue = array[middle];
  37. if (comparer(midValue, value) === 0) {
  38. return middle;
  39. }
  40. else if (comparer(midValue, value) > 0) {
  41. high = middle - 1;
  42. }
  43. else {
  44. low = middle + 1;
  45. }
  46. }
  47. return ~low;
  48. }
  49. function getLineAndCharacterOfPosition(sourceFile, position) {
  50. return computeLineAndCharacterOfPosition(computeLineStarts(sourceFile), position);
  51. }
  52. function getPositionOfLineAndCharacter(sourceFile, line, character) {
  53. return computePositionOfLineAndCharacter(computeLineStarts(sourceFile), line, character);
  54. }
  55. function computePositionOfLineAndCharacter(lineStarts, line, character) {
  56. return lineStarts[line] + character;
  57. }
  58. function computeLineAndCharacterOfPosition(lineStarts, position) {
  59. var lineNumber = binarySearch(lineStarts, position);
  60. if (lineNumber < 0) {
  61. lineNumber = ~lineNumber - 1;
  62. }
  63. return {
  64. character: position - lineStarts[lineNumber],
  65. line: lineNumber
  66. };
  67. }
  68. function computeLineStarts(text) {
  69. var result = [];
  70. var pos = 0;
  71. var lineStart = 0;
  72. while (pos < text.length) {
  73. var ch = text.charCodeAt(pos);
  74. pos++;
  75. switch (ch) {
  76. case CarriageReturn:
  77. if (text.charCodeAt(pos) === LineFeed) {
  78. pos++;
  79. }
  80. case LineFeed:
  81. result.push(lineStart);
  82. lineStart = pos;
  83. break;
  84. default:
  85. if (ch > MaxAsciiCharacter && isLineBreak(ch)) {
  86. result.push(lineStart);
  87. lineStart = pos;
  88. }
  89. break;
  90. }
  91. }
  92. result.push(lineStart);
  93. return result;
  94. }
  95. var SourceMappingVisitor = (function (_super) {
  96. __extends(SourceMappingVisitor, _super);
  97. function SourceMappingVisitor(sourceFile, options, codeWithMap, basePosition) {
  98. var _this = _super.call(this, sourceFile, options) || this;
  99. _this.codeWithMap = codeWithMap;
  100. _this.basePosition = basePosition;
  101. if (_this.codeWithMap.map) {
  102. _this.consumer = new source_map_1.SourceMapConsumer(_this.codeWithMap.map);
  103. }
  104. return _this;
  105. }
  106. SourceMappingVisitor.prototype.createFailure = function (s, l, message, fix) {
  107. var _a = this.getMappedInterval(s, l), start = _a.start, length = _a.length;
  108. return _super.prototype.createFailure.call(this, start, length, message, fix);
  109. };
  110. SourceMappingVisitor.prototype.createReplacement = function (s, l, replacement) {
  111. var _a = this.getMappedInterval(s, l), start = _a.start, length = _a.length;
  112. return _super.prototype.createReplacement.call(this, start, length, replacement);
  113. };
  114. SourceMappingVisitor.prototype.getSourcePosition = function (pos) {
  115. if (this.consumer) {
  116. try {
  117. var absPos = getLineAndCharacterOfPosition(this.codeWithMap.code, pos);
  118. var result = this.consumer.originalPositionFor({ line: absPos.line + 1, column: absPos.character + 1 });
  119. absPos = { line: result.line - 1, character: result.column - 1 };
  120. pos = getPositionOfLineAndCharacter(this.codeWithMap.source, absPos.line, absPos.character);
  121. }
  122. catch (e) {
  123. console.error(e);
  124. }
  125. }
  126. if (this.parentAST && this.parentAST.templateName) {
  127. pos = pos - this.parentAST.value.ast.span.start;
  128. }
  129. return pos + this.basePosition;
  130. };
  131. SourceMappingVisitor.prototype.addParentAST = function (parentAST) {
  132. this.parentAST = parentAST;
  133. return this;
  134. };
  135. SourceMappingVisitor.prototype.getMappedInterval = function (start, length) {
  136. var end = start + length;
  137. start = this.getSourcePosition(start);
  138. end = this.getSourcePosition(end);
  139. return { start: start, length: end - start };
  140. };
  141. return SourceMappingVisitor;
  142. }(tslint_1.RuleWalker));
  143. exports.SourceMappingVisitor = SourceMappingVisitor;