UNPKG

934 BJavaScriptView Raw
1// https://github.com/tarruda/sourcemap-to-ast
2var SourceMapConsumer = require("source-map").SourceMapConsumer;
3var traverse = require("estraverse").traverse;
4
5module.exports = function sourceMapToAst(ast, _map) {
6 var map = new SourceMapConsumer(_map);
7
8 traverse(ast, {
9 enter: function(node) {
10 if (!(node.type && node.loc)) return;
11
12 var origStart = map.originalPositionFor(node.loc.start);
13
14 if (!origStart.line) {
15 delete node.loc;
16 return;
17 }
18
19 var origEnd = map.originalPositionFor(node.loc.end);
20
21 if (
22 origEnd.line &&
23 (origEnd.line < origStart.line ||
24 origEnd.column < origStart.column)
25 ) {
26 origEnd.line = null;
27 }
28
29 node.loc = {
30 start: {
31 line: origStart.line,
32 column: origStart.column
33 },
34 end: origEnd.line && {
35 line: origEnd.line,
36 column: origEnd.column
37 },
38 source: origStart.source,
39 name: origStart.name
40 };
41 }
42 });
43
44 return ast;
45};