Permalink
Browse files

async falafel.map example works but test fails

  • Loading branch information...
1 parent e6eaa1c commit f9cd84af065498346d74a0f1fedff8344c804969 @substack committed Jun 12, 2012
Showing with 93 additions and 1 deletion.
  1. +49 −0 example/prompt.js
  2. +3 −1 index.js
  3. +38 −0 lib/async.js
  4. +3 −0 test/async.js
View
@@ -0,0 +1,49 @@
+var falafel = require('../');
+var vm = require('vm');
+
+var termExps = [
+ 'Identifier',
+ 'CallExpression',
+ 'BinaryExpression',
+ 'UpdateExpression',
+ 'UnaryExpression'
+].reduce(function (acc, key) { acc[key] = true; return acc }, {});
+
+function terminated (node) {
+ for (var p = node; p.parent; p = p.parent) {
+ if (termExps[p.type]) return true;
+ }
+ return false;
+}
+
+var src = '{"a":[2,~9,prompt(":d")],"b":4,"c":prompt("beep"),"d":6}';
+
+var offsets = [];
+var out = falafel.map('(' + src + ')', function (node) {
+ var isLeaf = node.parent
+ && !terminated(node.parent) && terminated(node)
+ ;
+
+ if (isLeaf) {
+ var s = node.source();
+ var prompted = false;
+ var res = vm.runInNewContext('(' + s + ')', {
+ prompt : function (x) {
+ setTimeout(function () {
+ node.update(x.toUpperCase());
+ }, Math.random() * 50);
+ prompted = true;
+ }
+ });
+ if (!prompted) {
+ var s_ = JSON.stringify(res);
+ node.update(s_);
+ }
+ }
+});
+
+setTimeout(function () {
+ console.log(src);
+ console.log('---');
+ console.log(out.source);
+}, 200);
View
@@ -1,6 +1,6 @@
var parse = require('esprima').parse;
-module.exports = function (src, fn) {
+exports = module.exports = function (src, fn) {
var opts = {};
if (typeof src === 'object') {
opts = src;
@@ -56,3 +56,5 @@ module.exports = function (src, fn) {
return output.join('');
};
+
+exports.map = require('./lib/async');
View
@@ -0,0 +1,38 @@
+var falafel = require('../');
+
+module.exports = function (src, fn) {
+ var offsets = [];
+ var isNext = false;
+
+ var result = {};
+ result.source = falafel(src, function (node) {
+ var ix = offsets.length;
+ offsets.push(offsets[offsets.length - 1] || 0);
+
+ var update = node.update;
+ node.update = function (s_) {
+ if (!isNext) {
+ var delta = s_.length - (node.range[1] - node.range[0] + 1);
+ var prev = offsets[ix - 1] || 0;
+ offsets[ix] = prev + delta;
+ return update.call(this, s_);
+ }
+
+ var delta = s_.length - (node.range[1] - node.range[0] + 1);
+ for (var i = ix + 1; i < offsets.length; i++) {
+ offsets[i] += delta;
+ }
+ var offset = offsets[ix];
+ var xs = result.source.split('');
+
+ var len = node.range[1] - node.range[0] + 1;
+ xs.splice(node.range[0] + offset, len, s_);
+ result.source = xs.join('');
+ node.range[1] = node.range[0] + s_.length - 1;
+ };
+ fn(node);
+ });
+
+ isNext = true;
+ return result;
+};
View
@@ -30,6 +30,9 @@ test('array', function (t) {
];
function check () {
+console.log(src);
+console.log('------------');
+console.log(output.source);
vm.runInNewContext(output.source, {
fn : function (xs) {
t.same(arrays.shift(), xs);

0 comments on commit f9cd84a

Please sign in to comment.