From c85d4318b935968b7266136008cbcf55c0418772 Mon Sep 17 00:00:00 2001 From: Ben Ng Date: Tue, 13 Aug 2013 14:00:46 -0700 Subject: [PATCH 1/6] Fix sourcemapped column for a quoted key of object literal --- lib/output.js | 29 +++++++++++++++++++++++------ 1 file changed, 23 insertions(+), 6 deletions(-) diff --git a/lib/output.js b/lib/output.js index 90589a2d..2aa3e982 100644 --- a/lib/output.js +++ b/lib/output.js @@ -278,12 +278,29 @@ function OutputStream(options) { 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 - ); + if (token) { + var remainder = 0; + var offset = 0; + var tokenName; + + tokenName = (!name && token.type == "name") ? token.value : name; + + if(tokenName) { + // Cast to a string, it might be a number + tokenName = new String(tokenName); + + // Fixes offsets for quoted keys of object literals + remainder = token.endpos - token.pos - tokenName.length; + offset = remainder === 0 ? 0 : remainder/2; + } + + options.source_map.add( + token.file || "?", + current_line, current_col, + token.line, token.col + offset, + tokenName + ); + } } catch(ex) { AST_Node.warn("Couldn't figure out mapping for {file}:{line},{col} → {cline},{ccol} [{name}]", { file: token.file, From d051f29cacc4f47b71437b8f5afaa9ef7dc428f2 Mon Sep 17 00:00:00 2001 From: Ben Ng Date: Tue, 13 Aug 2013 14:02:52 -0700 Subject: [PATCH 2/6] Escape sourcemapped names that contain escapable characters --- lib/output.js | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/lib/output.js b/lib/output.js index 2aa3e982..a7b5e21c 100644 --- a/lib/output.js +++ b/lib/output.js @@ -282,6 +282,14 @@ function OutputStream(options) { var remainder = 0; var offset = 0; var tokenName; + var tokenChars; + var escapable = [ + {find:'\n', replace:'\\n'}, + {find:'\r', replace:'\\r'}, + {find:'\t', replace:'\\t'}, + {find:'\u2028', replace:'\\u2028'}, + {find:'\u2029', replace:'\\u2029'} + ]; tokenName = (!name && token.type == "name") ? token.value : name; @@ -289,6 +297,17 @@ function OutputStream(options) { // Cast to a string, it might be a number tokenName = new String(tokenName); + // Fixes offsets for escaped chars in keys of object literals + tokenChars = tokenName.split(''); + for(var i=0, ii=tokenChars.length; i Date: Thu, 15 Aug 2013 17:26:48 -0700 Subject: [PATCH 3/6] Add missing char to to_ascii --- lib/output.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/output.js b/lib/output.js index a7b5e21c..49c93c9a 100644 --- a/lib/output.js +++ b/lib/output.js @@ -80,11 +80,12 @@ function OutputStream(options) { while (code.length < 4) code = "0" + code; return "\\u" + code; } - }); + }).replace(/\x0B/g, "\\x0B"); }; function make_string(str) { var dq = 0, sq = 0; + str = str.replace(/[\\\b\f\n\r\t\x22\x27\u2028\u2029\0]/g, function(s){ switch (s) { case "\\": return "\\\\"; From fae0f14f4a5402d9ebbfe43648d2974962896a76 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gunnlaugur=20=C3=9E=C3=B3r=20Briem?= Date: Tue, 24 Sep 2013 18:13:18 +0000 Subject: [PATCH 4/6] Sourcemaps: handle more escapable characters in keys --- lib/output.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/lib/output.js b/lib/output.js index 49c93c9a..714ae4c0 100644 --- a/lib/output.js +++ b/lib/output.js @@ -288,6 +288,10 @@ function OutputStream(options) { {find:'\n', replace:'\\n'}, {find:'\r', replace:'\\r'}, {find:'\t', replace:'\\t'}, + {find:'\b', replace:'\\b'}, + {find:'\f', replace:'\\f'}, + {find:'\v', replace:'\\v'}, + {find:'\\', replace:'\\\\'}, {find:'\u2028', replace:'\\u2028'}, {find:'\u2029', replace:'\\u2029'} ]; From 9b38b16701806542ad3c540a702eaa8de854eb17 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gunnlaugur=20=C3=9E=C3=B3r=20Briem?= Date: Tue, 24 Sep 2013 18:13:34 +0000 Subject: [PATCH 5/6] Fix missing semicolon --- lib/output.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/output.js b/lib/output.js index 714ae4c0..73b55865 100644 --- a/lib/output.js +++ b/lib/output.js @@ -311,7 +311,7 @@ function OutputStream(options) { } } } - tokenName = tokenChars.join('') + tokenName = tokenChars.join(''); // Fixes offsets for quoted keys of object literals remainder = token.endpos - token.pos - tokenName.length; From fe819011e29562d80fc07f45ed7900cc0c55acda Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gunnlaugur=20=C3=9E=C3=B3r=20Briem?= Date: Tue, 24 Sep 2013 18:14:53 +0000 Subject: [PATCH 6/6] Just offset by the initial quote character ... dividing by two yields bogus offsets for multi-character adjustments, leading to invalid sourcemaps with negative column positions --- lib/output.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/output.js b/lib/output.js index 73b55865..70964125 100644 --- a/lib/output.js +++ b/lib/output.js @@ -315,7 +315,7 @@ function OutputStream(options) { // Fixes offsets for quoted keys of object literals remainder = token.endpos - token.pos - tokenName.length; - offset = remainder === 0 ? 0 : remainder/2; + offset = remainder === 0 ? 0 : 1; } options.source_map.add(