Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Allow falafel to accept a parsed ast

  • Loading branch information...
commit 05dcb4a83072fcb8b5aa46d4f0e7f866ff859480 1 parent 3c2753d
Couto authored
Showing with 13 additions and 12 deletions.
  1. +13 −12 index.js
View
25 index.js
@@ -28,22 +28,23 @@ module.exports = function (src, opts, fn) {
src = src || opts.source;
opts.range = true;
if (typeof src !== 'string') src = String(src);
-
- var ast = parse(src, opts);
-
+
+ var ast = opts.ast || parse(src, opts);
+ delete opts.ast;
+
var result = {
chunks : src.split(''),
toString : function () { return result.chunks.join('') },
inspect : function () { return result.toString() }
};
var index = 0;
-
+
(function walk (node, parent) {
insertHelpers(node, parent, result.chunks);
-
+
forEach(objectKeys(node), function (key) {
if (key === 'parent') return;
-
+
var child = node[key];
if (isArray(child)) {
forEach(child, function (c) {
@@ -59,21 +60,21 @@ module.exports = function (src, opts, fn) {
});
fn(node);
})(ast, undefined);
-
+
return result;
};
-
+
function insertHelpers (node, parent, chunks) {
if (!node.range) return;
-
+
node.parent = parent;
-
+
node.source = function () {
return chunks.slice(
node.range[0], node.range[1]
).join('');
};
-
+
if (node.update && typeof node.update === 'object') {
var prev = node.update;
forEach(objectKeys(prev), function (key) {
@@ -84,7 +85,7 @@ function insertHelpers (node, parent, chunks) {
else {
node.update = update;
}
-
+
function update (s) {
chunks[node.range[0]] = s;
for (var i = node.range[0] + 1; i < node.range[1]; i++) {

1 comment on commit 05dcb4a

@inian

The AST passed in must have range attached to each node. There must be a check done to see if there is range attached to the passed in AST.

Please sign in to comment.
Something went wrong with that request. Please try again.