blob: 3bf673b3395abe4d9cf7bebc027ef887408a4e2e [file] [log] [blame]
Tests how SourceFormatter handles JS sources
Formatted:
function formatted1() {
var variable1 = 0;
}
function withComments() {
// comment
return "functionWithComments";
}
try {
onmessage = function(event) {
var source = event.data;
var formattedSource = beautify(source);
var mapping = buildMapping(source, formattedSource);
postMessage({
formattedSource: formattedSource,
mapping: mapping
})
}
;
function beautify(source) {
var ast = parse.parse(source);
var beautifyOptions = {
indent_level: 4,
indent_start: 0,
quote_keys: false,
space_colon: false
};
return process.gen_code(ast, beautifyOptions)
}
function buildMapping(source, formattedSource) {
var mapping = {
original: [],
formatted: []
};
var lastPosition = 0;
var regexp = /(^|[^\\])\b((?=\D)[\$\.\w]+)\b/g;
while (true) {
var match = regexp.exec(formattedSource);
if (!match)
break;
var position = source.indexOf(match[2], lastPosition);
if (position === -1)
throw "No match found in original source for " + match[2];
mapping.original.push(position);
mapping.formatted.push(match.index + match[1].length);
lastPosition = position + match[2].length
}
return mapping
}
function require() {
return parse
}
var exports = {};
importScripts("UglifyJS/parse-js.js");
var parse = exports;
var exports = {};
importScripts("UglifyJS/process.js");
var process = exports;
} catch (e) {}
function formatted2() {
var variable2 = 0;
}
Location mapping with formatted source:
1:0 -> 0:22 -> 1:0
6:0 -> 4:24 -> 6:0
11:3 -> 9:4 -> 11:3
11:29 -> 10:32 -> 11:29
11:150 -> 14:20 -> 11:150
11:227 -> 20:30 -> 11:227
12:0 -> 22:30 -> 12:0
12:157 -> 30:51 -> 12:157
12:170 -> 31:22 -> 12:170
13:0 -> 37:21 -> 13:0
14:71 -> 50:23 -> 14:71
14:97 -> 53:18 -> 14:97
14:168 -> 56:18 -> 14:168
14:237 -> 59:12 -> 14:237
17:0 -> 61:22 -> 17:0
Location mapping without formatted source:
1:0 -> 1:0 -> 1:0
6:0 -> 6:0 -> 6:0
11:3 -> 11:3 -> 11:3
11:29 -> 11:29 -> 11:29
11:150 -> 11:150 -> 11:150
11:227 -> 11:227 -> 11:227
12:0 -> 12:0 -> 12:0
12:157 -> 12:157 -> 12:157
12:170 -> 12:170 -> 12:170
13:0 -> 13:0 -> 13:0
14:71 -> 14:71 -> 14:71
14:97 -> 14:97 -> 14:97
14:168 -> 14:168 -> 14:168
14:237 -> 14:237 -> 14:237
17:0 -> 17:0 -> 17:0