Skip to content

Commit

Permalink
issue #254 - reimplemented lines=preserve option with esprima
Browse files Browse the repository at this point in the history
  • Loading branch information
bjouhier committed Mar 3, 2015
1 parent 1df739c commit 25ef7e3
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 310 deletions.
70 changes: 28 additions & 42 deletions lib/callbacks/transform-esprima.js
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,7 @@ if (typeof exports !== 'undefined') {
// generates a parse tree from a template by substituting bindings.
this.generate = function(scopeNode, bindings) {
var scope = scopeNode._scope;
var loc = scopeNode.loc;
_assert(scope != null);
bindings = bindings || {};
var fn = null;
Expand Down Expand Up @@ -298,7 +299,9 @@ if (typeof exports !== 'undefined') {

_propagate(fn, _changeScope);
}
return isExpression ? result.argument : result;
result = isExpression ? result.argument : result;
result.loc = loc;
return result;
}
this.root = isExpression ? _root.argument : _root; // for simplify pass
}
Expand Down Expand Up @@ -1826,45 +1829,26 @@ if (typeof exports !== 'undefined') {
return JSON.stringify(fix(obj), null, ' ');
}

function fixRanges(node) {
function fix1(obj) {
if (!obj || typeof obj !== 'object') return null;
if (obj._visited === visit) return null;
obj._visited = visit;
function combine(r1, r2) {
if (!r1) return r2;
if (!r2) return r1;
return [Math.min(r1[0], r2[0]), Math.max(r1[1], r2[1])];
}
if (Array.isArray(obj)) {
return obj.reduce(function(r, o) {
return combine(r, fix1(o));
}, null);
}
if (!obj.type) return null;
return obj.range = Object.keys(obj).reduce(function(r, k) {
return combine(r, fix1(obj[k]));
}, null);
}
function fix2(obj, val) {
if (!obj || typeof obj !== 'object') return;
if (obj._visited === visit) return;
obj._visited = visit;
if (Array.isArray(obj)) {
obj.forEach(function(o) {
fix2(o, val);
});
function addNewlines(node) {
var line = 1, l = 1;
function doIt(obj, insideBlock) {
if (!obj) return;
if (obj.loc) l = Math.max(obj.loc.start.line, l);
if (obj.type && insideBlock && line < l) {
obj.leadingComments = new Array(l - line).join('#').split('#');
line = l;
}
if (Array.isArray(obj)) return obj.forEach(function(o) {
doIt(o, insideBlock);
});
if (!obj.type) return;
if (!obj.range) obj.range = [val, val];
var isBlock = obj.type === Syntax.BlockStatement || obj.type === Syntax.SwitchCase;
Object.keys(obj).forEach(function(k) {
fix2(obj[k], obj.range[0]);
var v = obj[k];
doIt(v, isBlock);
});
}
visit++;
fix1(node);
visit++;
fix2(node, 0);
doIt(node, false);
}

/// * `transformed = transform.transform(source, options)`
Expand Down Expand Up @@ -1919,15 +1903,17 @@ if (typeof exports !== 'undefined') {
var used = {};
node = _simplify(node, options, used);
//fixRanges(node);
addNewlines(node);
var result = escodegen.generate(node, {
//sourceCode: source,
format: {
//preserveBlankLines: true,
}
}); //, options.lines);
comment: true,
});
// remove curly braces around generated source
result = result[0] === '{' ? result.substring(1, result.length - 1) : result;

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');

// add helpers at beginning so that __g is initialized before any other code
if (!options.noHelpers) {
Expand Down
Loading

0 comments on commit 25ef7e3

Please sign in to comment.