diff options
Diffstat (limited to 'lib')
-rw-r--r-- | lib/output.js | 69 |
1 files changed, 50 insertions, 19 deletions
diff --git a/lib/output.js b/lib/output.js index 40800559..e9765f7c 100644 --- a/lib/output.js +++ b/lib/output.js @@ -189,12 +189,43 @@ function OutputStream(options) { var might_need_semicolon = false; var might_add_newline = 0; var last = ""; + var mapping_token, mapping_name, mappings = options.source_map && []; + + var do_add_mapping = mappings ? function() { + mappings.forEach(function(mapping) { + try { + options.source_map.add( + mapping.token.file, + mapping.line, mapping.col, + mapping.token.line, mapping.token.col, + !mapping.name && mapping.token.type == "name" ? mapping.token.value : mapping.name + ); + } catch(ex) { + AST_Node.warn("Couldn't figure out mapping for {file}:{line},{col} → {cline},{ccol} [{name}]", { + file: mapping.token.file, + line: mapping.token.line, + col: mapping.token.col, + cline: mapping.line, + ccol: mapping.col, + name: mapping.name || "" + }) + } + }); + mappings = []; + } : noop; var ensure_line_len = options.max_line_len ? function() { if (current_col > options.max_line_len) { if (might_add_newline) { var left = OUTPUT.slice(0, might_add_newline); var right = OUTPUT.slice(might_add_newline); + if (mappings) { + var delta = right.length - current_col; + mappings.forEach(function(mapping) { + mapping.line++; + mapping.col += delta; + }); + } OUTPUT = left + "\n" + right; current_line++; current_pos++; @@ -204,7 +235,10 @@ function OutputStream(options) { AST_Node.warn("Output exceeds {max_line_len} characters", options); } } - might_add_newline = 0; + if (might_add_newline) { + might_add_newline = 0; + do_add_mapping(); + } } : noop; var requireSemicolonChars = makePredicate("( [ + * / - , ."); @@ -264,6 +298,18 @@ function OutputStream(options) { } might_need_space = false; } + + if (mapping_token) { + mappings.push({ + token: mapping_token, + name: mapping_name, + line: current_line, + col: current_col + }); + mapping_token = false; + if (!might_add_newline) do_add_mapping(); + } + OUTPUT += str; current_pos += str.length; var a = str.split(/\r?\n/), n = a.length - 1; @@ -358,24 +404,9 @@ function OutputStream(options) { space(); }; - var add_mapping = options.source_map ? function(token, name) { - try { - if (token) options.source_map.add( - token.file || "?", - current_line, current_col, - token.line, token.col, - (!name && token.type == "name") ? token.value : name - ); - } catch(ex) { - AST_Node.warn("Couldn't figure out mapping for {file}:{line},{col} → {cline},{ccol} [{name}]", { - file: token.file, - line: token.line, - col: token.col, - cline: current_line, - ccol: current_col, - name: name || "" - }) - } + var add_mapping = mappings ? function(token, name) { + mapping_token = token; + mapping_name = name; } : noop; function get() { |