started support for generating source maps (WIP)

plugged in @fitzgen's source-map library
This commit is contained in:
Mihai Bazon
2012-08-29 19:39:19 +03:00
parent 48440dc250
commit 52bcca288f
4 changed files with 173 additions and 13 deletions

View File

@@ -53,7 +53,8 @@ function OutputStream(options) {
width : 80,
max_line_len : 32000,
ie_proof : true,
beautify : true
beautify : true,
source_map : null
});
var indentation = 0;
@@ -152,12 +153,12 @@ function OutputStream(options) {
might_need_space = false;
maybe_newline();
}
var a = str.split(/\r?\n/), n = a.length;
var a = str.split(/\r?\n/), n = a.length - 1;
current_line += n;
if (n == 1) {
current_col += a[n - 1].length;
if (n == 0) {
current_col += a[n].length;
} else {
current_col = a[n - 1].length;
current_col = a[n].length;
}
current_pos += str.length;
last = str;
@@ -243,9 +244,22 @@ function OutputStream(options) {
if (options.space_colon) space();
};
var add_mapping = options.source_map ? function(token, name) {
options.source_map.add(
current_line, current_col,
token.line, token.col,
(!name && token.type == "name") ? token.value : name
);
} : noop;
function get() {
return OUTPUT;
};
var stack = [];
return {
get : function() { return OUTPUT },
get : get,
toString : get,
indent : indent,
newline : newline,
print : print,
@@ -261,6 +275,7 @@ function OutputStream(options) {
with_block : with_block,
with_parens : with_parens,
with_square : with_square,
add_mapping : add_mapping,
option : function(opt) { return options[opt] },
line : function() { return current_line },
col : function() { return current_col },
@@ -287,9 +302,11 @@ function OutputStream(options) {
stream.push_node(self);
if (self.needs_parens(stream)) {
stream.with_parens(function(){
self.add_source_map(stream);
generator(self, stream);
});
} else {
self.add_source_map(stream);
generator(self, stream);
}
stream.pop_node();
@@ -868,11 +885,7 @@ function OutputStream(options) {
});
DEFPRINT(AST_SymbolRef, function(self, output){
var def = self.symbol;
if (def) {
def.print(output);
} else {
output.print_name(self.name);
}
output.print_name(def ? def.mangled_name || def.name : self.name);
});
DEFPRINT(AST_This, function(self, output){
output.print("this");
@@ -973,4 +986,32 @@ function OutputStream(options) {
});
};
/* -----[ source map generators ]----- */
function DEFMAP(nodetype, generator) {
nodetype.DEFMETHOD("add_source_map", function(stream){
generator(this, stream);
});
};
// We could easily add info for ALL nodes, but it seems to me that
// would be quite wasteful, hence this noop in the base class.
DEFMAP(AST_Node, noop);
function basic_sourcemap_gen(self, output) {
output.add_mapping(self.start);
};
// XXX: I'm not exactly sure if we need it for all of these nodes,
// or if we should add even more.
DEFMAP(AST_Directive, basic_sourcemap_gen);
DEFMAP(AST_Debugger, basic_sourcemap_gen);
DEFMAP(AST_Symbol, basic_sourcemap_gen);
DEFMAP(AST_Jump, basic_sourcemap_gen);
DEFMAP(AST_PropAccess, basic_sourcemap_gen);
DEFMAP(AST_ObjectProperty, function(self, output){
output.add_mapping(self.start, self.key);
});
})();