Skip to content

Commit

Permalink
issue #254 - source map changes for esprima
Browse files Browse the repository at this point in the history
  • Loading branch information
bjouhier committed Mar 14, 2015
1 parent e2aff27 commit 85e44c6
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 19 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -387,7 +387,7 @@ Streamline also provides _stream wrappers_ that simplify stream programming. The

You can seamlessly debug streamline code thanks to [JavaScript source maps](http://www.html5rocks.com/en/tutorials/developertools/sourcemaps/). See [this video](https://www.youtube.com/watch?v=duC1Sqy66IE) for a quick demo.

To activate this feature, pass the `--source-map` options to `_node` or `_coffee`, or set the `sourcemap` option if you register via a loader.
To activate this feature, pass the `--source-map` options to `_node` or `_coffee`, or set the `sourceMap` option if you register via a loader.

<a name="flamegraph">
# Monitoring performance with flame graphs
Expand Down Expand Up @@ -460,7 +460,7 @@ For support and discussion, please join the [streamline.js mailing list](http://

See the [AUTHORS](https://github.com/Sage/streamlinejs/blob/master/AUTHORS) file.

Special thanks to Marcel Laverdet who contributed the _fibers_ implementation and to Geoffry Song who contributed sourcemap support.
Special thanks to Marcel Laverdet who contributed the _fibers_ implementation and to Geoffry Song who contributed source map support.

<a name="license">
# License
Expand Down
40 changes: 29 additions & 11 deletions lib/callbacks/transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,7 @@ if (typeof exports !== 'undefined') {
return child;
}, true);
node = _flatten(node);
node.loc = node.loc || loc;
return node;
}
}
Expand Down Expand Up @@ -1881,6 +1882,7 @@ if (typeof exports !== 'undefined') {
var node = esprima.parse(source, {
loc: true,
range: true,
source: options.sourceName || '<unknown>',
});
node = node.body[0].body;
if (node.type !== 'BlockStatement') throw new Error("source wrapper error: " + node.type);
Expand All @@ -1904,22 +1906,38 @@ if (typeof exports !== 'undefined') {
var used = {};
node = _simplify(node, options, used);
//fixRanges(node);
addNewlines(node);
var result = escodegen.generate(node, {
if (options.lines === "preserve") addNewlines(node);

// transform top node into Program to avoid extra curly braces
if (node.type === "BlockStatement") node.type = "Program";

var ecopts = options.sourceMap ? {
sourceMap: true,
sourceMapWithCode: true,
} : options.lines === "preserve" ? {
comment: true,
});
// remove curly braces around generated source
result = result[0] === '{' ? result.substring(1, result.length - 1) : result;
// turn comments into newlines
//result = result.replace(/\n\s*/g, ' ').replace(/\/\*undefined\*\//g, '\n');
result = result.split(/\/\*undefined\*\/\n/).map(function(s) {
return s.replace(/\n\s*/g, ' ');
}).join('\n');
} : {};
var result = escodegen.generate(node, ecopts);
if (options.sourceMap) {
// convert result into a SourceNode.
// would be much easier (and faster) if escodegen had an option to return SourceNode directly
var SourceNode = require('source-map').SourceNode;
var SourceMapConsumer = require('source-map').SourceMapConsumer;
result = SourceNode.fromStringWithSourceMap(result.code, new SourceMapConsumer(result.map.toString()));
}

if (options.lines === "preserve") {
// turn comments into newlines
//result = result.replace(/\n\s*/g, ' ').replace(/\/\*undefined\*\//g, '\n');
result = result.split(/\/\*undefined\*\/\n/).map(function(s) {
return s.replace(/\n\s*/g, ' ');
}).join('\n');
};

// add helpers at beginning so that __g is initialized before any other code
if (!options.noHelpers) {
var s = exports.helpersSource(options, used, strict);
if (options.lines == "sourcemap") {
if (options.sourceMap) {
result.prepend(s);
} else {
result = s + result;
Expand Down
11 changes: 6 additions & 5 deletions lib/compiler/compile._js
Original file line number Diff line number Diff line change
Expand Up @@ -113,12 +113,13 @@ function outputFile(_, inFile, options) {
}

function fixSourceMap(sourceMap) {
var keys = {};
var prev;
sourceMap._mappings._array = sourceMap._mappings._array.filter(function(mapping) {
if (!mapping.originalLine) return false;
var key = mapping.originalLine + '/' + mapping.originalColumn;
if (keys[key]) return false;
keys[key] = true;
if (prev && mapping.source === prev.source //
&& mapping.originalLine === prev.originalLine //
&& mapping.originalColumn === prev.originalColumn) return false;
prev = mapping;
return true;
});
}
Expand Down Expand Up @@ -315,7 +316,7 @@ function cachedTransform(_, content, path, transform, banner, options) {
}
// no luck in cache
if (options.verbose) console.log("streamline: transforming: " + path);
options.lines = options.lines || "sourcemap";
options.lines = options.lines || (options.sourceMap ? "sourcemap" : "preserve");
transformed = banner + _transform(transform, content, options);
if (options.cache && path.indexOf('/tmp--') < 0) fs.writeFile(f, transformed, "utf8", ~_);
return transformed;
Expand Down
2 changes: 1 addition & 1 deletion lib/compiler/compileSync.js
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ function cachedTransformSync(content, path, transform, banner, options, testOnly
r[k] = options[k];
return r;
}, {});
opts.lines = opts.lines || "sourcemap";
opts.lines = opts.lines || (opts.sourceMap ? "sourcemap" : "preserve");
transformed = banner + _transform(transform, content, opts);
if (options.cache && path.indexOf('/tmp--') < 0) fs.writeFileSync(f, transformed, "utf8");
return transformed;
Expand Down

0 comments on commit 85e44c6

Please sign in to comment.