UNPKG

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