input-source-map-tracker.js 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. var SourceMapConsumer = require('source-map').SourceMapConsumer;
  2. function inputSourceMapTracker() {
  3. var maps = {};
  4. return {
  5. all: all.bind(null, maps),
  6. isTracking: isTracking.bind(null, maps),
  7. originalPositionFor: originalPositionFor.bind(null, maps),
  8. track: track.bind(null, maps)
  9. };
  10. }
  11. function all(maps) {
  12. return maps;
  13. }
  14. function isTracking(maps, source) {
  15. return source in maps;
  16. }
  17. function originalPositionFor(maps, metadata, range, selectorFallbacks) {
  18. var line = metadata[0];
  19. var column = metadata[1];
  20. var source = metadata[2];
  21. var position = {
  22. line: line,
  23. column: column + range
  24. };
  25. var originalPosition;
  26. while (!originalPosition && position.column > column) {
  27. position.column--;
  28. originalPosition = maps[source].originalPositionFor(position);
  29. }
  30. if (!originalPosition || originalPosition.column < 0) {
  31. return metadata;
  32. }
  33. if (originalPosition.line === null && line > 1 && selectorFallbacks > 0) {
  34. return originalPositionFor(maps, [line - 1, column, source], range, selectorFallbacks - 1);
  35. }
  36. return originalPosition.line !== null ?
  37. toMetadata(originalPosition) :
  38. metadata;
  39. }
  40. function toMetadata(asHash) {
  41. return [asHash.line, asHash.column, asHash.source];
  42. }
  43. function track(maps, source, data) {
  44. maps[source] = new SourceMapConsumer(data);
  45. }
  46. module.exports = inputSourceMapTracker;