From 87f31ed488955cece84bd4f96a116fa9e7fce8b5 Mon Sep 17 00:00:00 2001 From: Arnavion Date: Tue, 4 Aug 2015 10:59:14 -0700 Subject: [PATCH 1/2] Added support for multiple input source maps to SourceMap object. The addInput method on the object can be used to add sourcemaps for individual input files. --- lib/sourcemap.js | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/lib/sourcemap.js b/lib/sourcemap.js index a67011f0..bfffe8f8 100644 --- a/lib/sourcemap.js +++ b/lib/sourcemap.js @@ -53,19 +53,26 @@ function SourceMap(options) { orig_line_diff : 0, dest_line_diff : 0, }); + var orig_maps = Object.create(null); var orig_map = options.orig && new MOZ_SourceMap.SourceMapConsumer(options.orig); var generator; if (orig_map) { - generator = MOZ_SourceMap.SourceMapGenerator.fromSourceMap(orig_map); + generator = MOZ_SourceMap.SourceMapGenerator.fromSourceMap(orig_map); } else { generator = new MOZ_SourceMap.SourceMapGenerator({ file : options.file, sourceRoot : options.root }); } + function addInput(rawSourceMap) { + var consumer = new MOZ_SourceMap.SourceMapConsumer(rawSourceMap); + orig_maps[consumer.file] = consumer; + } function add(source, gen_line, gen_col, orig_line, orig_col, name) { - if (orig_map) { - var info = orig_map.originalPositionFor({ + var originalMap = orig_maps[source] || orig_map; + + if (originalMap) { + var info = originalMap.originalPositionFor({ line: orig_line, column: orig_col }); @@ -77,6 +84,7 @@ function SourceMap(options) { orig_col = info.column; name = info.name || name; } + generator.addMapping({ generated : { line: gen_line + options.dest_line_diff, column: gen_col }, original : { line: orig_line + options.orig_line_diff, column: orig_col }, @@ -85,6 +93,7 @@ function SourceMap(options) { }); } return { + addInput : addInput, add : add, get : function() { return generator }, toString : function() { return JSON.stringify(generator.toJSON()); } From e47ffc6e577e5420b22633cb2be8e741a20194d0 Mon Sep 17 00:00:00 2001 From: Arnavion Date: Tue, 4 Aug 2015 10:59:15 -0700 Subject: [PATCH 2/2] Don't search orig_maps if the source filename is unknown. When the AST is not associated with a filename, only orig_map can be queried to get the original filename. If that wasn't provided, then the best we can do is add a mapping literally for "?" --- lib/output.js | 2 +- lib/sourcemap.js | 9 ++++++++- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/lib/output.js b/lib/output.js index 396c6a29..40493fcf 100644 --- a/lib/output.js +++ b/lib/output.js @@ -303,7 +303,7 @@ function OutputStream(options) { var add_mapping = options.source_map ? function(token, name) { try { if (token) options.source_map.add( - token.file || "?", + token.file, current_line, current_col, token.line, token.col, (!name && token.type == "name") ? token.value : name diff --git a/lib/sourcemap.js b/lib/sourcemap.js index bfffe8f8..f62b43e1 100644 --- a/lib/sourcemap.js +++ b/lib/sourcemap.js @@ -69,7 +69,14 @@ function SourceMap(options) { orig_maps[consumer.file] = consumer; } function add(source, gen_line, gen_col, orig_line, orig_col, name) { - var originalMap = orig_maps[source] || orig_map; + var originalMap; + if (source) { + originalMap = orig_maps[source] || orig_map; + } + else { + source = "?"; + originalMap = orig_map; + } if (originalMap) { var info = originalMap.originalPositionFor({