Skip to content

Instantly share code, notes, and snippets.

@pd
Created August 8, 2013 18:12
Show Gist options
  • Save pd/6187126 to your computer and use it in GitHub Desktop.
Save pd/6187126 to your computer and use it in GitHub Desktop.
let's call it an mvp
$ npm i esprima falafel
$ node sib.js | node
{ '4': [ [ 2, 'big' ], [ 2, 'big' ] ],
'8': [ [ 6, 'little' ] ],
'11': [ [ 11, 'big' ], [ 11, 'little' ] ] }
var x;
x = sth(
true
);
var y = sth(
false
);
function sth(big) {
return big ? 'big' : 'little';
}
var esprima = require('esprima'),
falafel = require('falafel');
var src = require('fs').readFileSync('./lol.js', 'utf8');
var transformed = falafel(src, { loc: true }, function(node) {
var loc = node.loc;
if (node.type.match(/Expression$/))
node.update('(__jsib.record(' + loc.end.line + ',' + loc.start.line + ',( ' + node.source() + ' )))');
});
var output =
"(function() {var __jsib={r:{}}; __jsib.record = function(end,start,val){(this.r[end] = this.r[end] || []).push([start, val]); return val;};\n\n" +
transformed + "\n\n" +
"console.dir(__jsib.r); return __jsib.r;})();";
console.log(output);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment