Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 48 additions & 7 deletions lib/output.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 "\\\\";
Expand Down Expand Up @@ -278,12 +279,52 @@ 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;
var tokenChars;
var escapable = [
{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'}
];

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 escaped chars in keys of object literals
tokenChars = tokenName.split('');
for(var i=0, ii=tokenChars.length; i<ii; i++) {
for(var y=0, yy=escapable.length; y<yy; y++) {
if(tokenChars[i] === escapable[y].find && (i===0 || tokenChars[i-1] != '\\')) {
tokenChars[i] = escapable[y].replace;
}
}
}
tokenName = tokenChars.join('');

// Fixes offsets for quoted keys of object literals
remainder = token.endpos - token.pos - tokenName.length;
offset = remainder === 0 ? 0 : 1;
}

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,
Expand Down