Software zum Installieren eines Smart-Mirror Frameworks , zum Nutzen von hochschulrelevanten Informationen, auf einem Raspberry-Pi.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

get-mapping.js 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /*
  2. Copyright 2015, Yahoo Inc.
  3. Copyrights licensed under the New BSD License. See the accompanying LICENSE file for terms.
  4. */
  5. 'use strict';
  6. const pathutils = require('./pathutils');
  7. const {
  8. GREATEST_LOWER_BOUND,
  9. LEAST_UPPER_BOUND
  10. } = require('source-map').SourceMapConsumer;
  11. /**
  12. * AST ranges are inclusive for start positions and exclusive for end positions.
  13. * Source maps are also logically ranges over text, though interacting with
  14. * them is generally achieved by working with explicit positions.
  15. *
  16. * When finding the _end_ location of an AST item, the range behavior is
  17. * important because what we're asking for is the _end_ of whatever range
  18. * corresponds to the end location we seek.
  19. *
  20. * This boils down to the following steps, conceptually, though the source-map
  21. * library doesn't expose primitives to do this nicely:
  22. *
  23. * 1. Find the range on the generated file that ends at, or exclusively
  24. * contains the end position of the AST node.
  25. * 2. Find the range on the original file that corresponds to
  26. * that generated range.
  27. * 3. Find the _end_ location of that original range.
  28. */
  29. function originalEndPositionFor(sourceMap, generatedEnd) {
  30. // Given the generated location, find the original location of the mapping
  31. // that corresponds to a range on the generated file that overlaps the
  32. // generated file end location. Note however that this position on its
  33. // own is not useful because it is the position of the _start_ of the range
  34. // on the original file, and we want the _end_ of the range.
  35. const beforeEndMapping = originalPositionTryBoth(
  36. sourceMap,
  37. generatedEnd.line,
  38. generatedEnd.column - 1
  39. );
  40. if (beforeEndMapping.source === null) {
  41. return null;
  42. }
  43. // Convert that original position back to a generated one, with a bump
  44. // to the right, and a rightward bias. Since 'generatedPositionFor' searches
  45. // for mappings in the original-order sorted list, this will find the
  46. // mapping that corresponds to the one immediately after the
  47. // beforeEndMapping mapping.
  48. const afterEndMapping = sourceMap.generatedPositionFor({
  49. source: beforeEndMapping.source,
  50. line: beforeEndMapping.line,
  51. column: beforeEndMapping.column + 1,
  52. bias: LEAST_UPPER_BOUND
  53. });
  54. if (
  55. // If this is null, it means that we've hit the end of the file,
  56. // so we can use Infinity as the end column.
  57. afterEndMapping.line === null ||
  58. // If these don't match, it means that the call to
  59. // 'generatedPositionFor' didn't find any other original mappings on
  60. // the line we gave, so consider the binding to extend to infinity.
  61. sourceMap.originalPositionFor(afterEndMapping).line !==
  62. beforeEndMapping.line
  63. ) {
  64. return {
  65. source: beforeEndMapping.source,
  66. line: beforeEndMapping.line,
  67. column: Infinity
  68. };
  69. }
  70. // Convert the end mapping into the real original position.
  71. return sourceMap.originalPositionFor(afterEndMapping);
  72. }
  73. /**
  74. * Attempts to determine the original source position, first
  75. * returning the closest element to the left (GREATEST_LOWER_BOUND),
  76. * and next returning the closest element to the right (LEAST_UPPER_BOUND).
  77. */
  78. function originalPositionTryBoth(sourceMap, line, column) {
  79. const mapping = sourceMap.originalPositionFor({
  80. line,
  81. column,
  82. bias: GREATEST_LOWER_BOUND
  83. });
  84. if (mapping.source === null) {
  85. return sourceMap.originalPositionFor({
  86. line,
  87. column,
  88. bias: LEAST_UPPER_BOUND
  89. });
  90. } else {
  91. return mapping;
  92. }
  93. }
  94. function isInvalidPosition(pos) {
  95. return (
  96. !pos ||
  97. typeof pos.line !== 'number' ||
  98. typeof pos.column !== 'number' ||
  99. pos.line < 0 ||
  100. pos.column < 0
  101. );
  102. }
  103. /**
  104. * determines the original position for a given location
  105. * @param {SourceMapConsumer} sourceMap the source map
  106. * @param {Object} generatedLocation the original location Object
  107. * @returns {Object} the remapped location Object
  108. */
  109. function getMapping(sourceMap, generatedLocation, origFile) {
  110. if (!generatedLocation) {
  111. return null;
  112. }
  113. if (
  114. isInvalidPosition(generatedLocation.start) ||
  115. isInvalidPosition(generatedLocation.end)
  116. ) {
  117. return null;
  118. }
  119. const start = originalPositionTryBoth(
  120. sourceMap,
  121. generatedLocation.start.line,
  122. generatedLocation.start.column
  123. );
  124. let end = originalEndPositionFor(sourceMap, generatedLocation.end);
  125. /* istanbul ignore if: edge case too hard to test for */
  126. if (!(start && end)) {
  127. return null;
  128. }
  129. if (!(start.source && end.source)) {
  130. return null;
  131. }
  132. if (start.source !== end.source) {
  133. return null;
  134. }
  135. /* istanbul ignore if: edge case too hard to test for */
  136. if (start.line === null || start.column === null) {
  137. return null;
  138. }
  139. /* istanbul ignore if: edge case too hard to test for */
  140. if (end.line === null || end.column === null) {
  141. return null;
  142. }
  143. if (start.line === end.line && start.column === end.column) {
  144. end = sourceMap.originalPositionFor({
  145. line: generatedLocation.end.line,
  146. column: generatedLocation.end.column,
  147. bias: LEAST_UPPER_BOUND
  148. });
  149. end.column -= 1;
  150. }
  151. return {
  152. source: pathutils.relativeTo(start.source, origFile),
  153. loc: {
  154. start: {
  155. line: start.line,
  156. column: start.column
  157. },
  158. end: {
  159. line: end.line,
  160. column: end.column
  161. }
  162. }
  163. };
  164. }
  165. module.exports = getMapping;