Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

modify falafel() to return a string-like object and then async update…

…s just seem to work
  • Loading branch information...
commit 72562132c57ffcc1d7a75cb45beff0c531737d28 1 parent f9cd84a
James Halliday authored
Showing with 18 additions and 54 deletions.
  1. +2 −2 example/prompt.js
  2. +13 −8 index.js
  3. +0 −38 lib/async.js
  4. +3 −6 test/async.js
4 example/prompt.js
View
@@ -19,7 +19,7 @@ function terminated (node) {
var src = '{"a":[2,~9,prompt(":d")],"b":4,"c":prompt("beep"),"d":6}';
var offsets = [];
-var out = falafel.map('(' + src + ')', function (node) {
+var output = falafel('(' + src + ')', function (node) {
var isLeaf = node.parent
&& !terminated(node.parent) && terminated(node)
;
@@ -45,5 +45,5 @@ var out = falafel.map('(' + src + ')', function (node) {
setTimeout(function () {
console.log(src);
console.log('---');
- console.log(out.source);
+ console.log(output);
}, 200);
21 index.js
View
@@ -1,6 +1,7 @@
var parse = require('esprima').parse;
+var json = typeof JSON === 'object' ? JSON : require('jsonify');
-exports = module.exports = function (src, fn) {
+module.exports = function (src, fn) {
var opts = {};
if (typeof src === 'object') {
opts = src;
@@ -12,7 +13,11 @@ exports = module.exports = function (src, fn) {
var ast = parse(src, opts);
- var output = src.split('');
+ var result = {
+ chunks : src.split(''),
+ toString : function () { return result.chunks.join('') },
+ inspect : function () { return result.toString() }
+ };
var index = 0;
function insertHelpers (node, parent) {
@@ -21,13 +26,15 @@ exports = module.exports = function (src, fn) {
node.parent = parent;
node.source = function () {
- return output.slice(node.range[0], node.range[1] + 1).join('');
+ return result.chunks.slice(
+ node.range[0], node.range[1] + 1
+ ).join('');
};
node.update = function (s) {
- output[node.range[0]] = s;
+ result.chunks[node.range[0]] = s;
for (var i = node.range[0] + 1; i < node.range[1] + 1; i++) {
- output[i] = '';
+ result.chunks[i] = '';
}
};
}
@@ -54,7 +61,5 @@ exports = module.exports = function (src, fn) {
fn(node);
})(ast, undefined);
- return output.join('');
+ return result;
};
-
-exports.map = require('./lib/async');
38 lib/async.js
View
@@ -1,38 +0,0 @@
-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;
-};
9 test/async.js
View
@@ -12,13 +12,13 @@ test('array', function (t) {
} + ')()';
var pending = 0;
- var output = falafel.map(src, function (node) {
+ var output = falafel(src, function (node) {
if (node.type === 'ArrayExpression') {
pending ++;
setTimeout(function () {
node.update('fn(' + node.source() + ')');
if (--pending === 0) check();
- }, Math.random() * 50);
+ }, 10);
}
});
@@ -30,10 +30,7 @@ test('array', function (t) {
];
function check () {
-console.log(src);
-console.log('------------');
-console.log(output.source);
- vm.runInNewContext(output.source, {
+ vm.runInNewContext(output, {
fn : function (xs) {
t.same(arrays.shift(), xs);
return xs;
Please sign in to comment.
Something went wrong with that request. Please try again.