ast-types
Advanced tools
Comparing version 0.2.24 to 0.2.25
@@ -1,45 +0,13 @@ | ||
var assert = require("assert"); | ||
var types = require("./types"); | ||
var namedTypes = types.namedTypes; | ||
var Node = namedTypes.Node; | ||
var Node = types.namedTypes.Node; | ||
var isArray = types.builtInTypes.array; | ||
var Scope = require("./scope"); | ||
var NodePath = require("./node-path"); | ||
function Path(node, parentPath) { | ||
assert.ok(this instanceof Path); | ||
Node.assert(node); | ||
if (parentPath) { | ||
assert.ok(parentPath instanceof Path); | ||
} else { | ||
parentPath = null; | ||
} | ||
var parentScope = parentPath && parentPath.scope; | ||
var scope = null; | ||
if (Scope.isEstablishedBy(node)) { | ||
scope = new Scope(node, parentScope); | ||
} else if (parentScope) { | ||
scope = parentScope; | ||
} | ||
Object.defineProperties(this, { | ||
node: { value: node }, | ||
parent: { value: parentPath }, | ||
scope: { value: scope } | ||
}); | ||
} | ||
module.exports = function(node, callback) { | ||
function traverse(node, parentPath) { | ||
if (isArray.check(node)) { | ||
node.map(function(child) { | ||
traverse(child, parentPath); | ||
}); | ||
function traverse(path) { | ||
if (isArray.check(path.value)) { | ||
path.each(traverse); | ||
} else if (Node.check(path.value)) { | ||
var node = path.value; | ||
} else if (Node.check(node)) { | ||
var path = new Path(node, parentPath); | ||
if (callback.call(path, node) === false) { | ||
@@ -50,3 +18,6 @@ return; | ||
types.eachField(node, function(name, child) { | ||
traverse(child, path); | ||
// Need to create the child path manually because child | ||
// might not actually be identical to path.value[name], | ||
// if for instance a default field value was substituted. | ||
traverse(new NodePath(child, path, name)); | ||
}); | ||
@@ -56,3 +27,3 @@ } | ||
traverse(node); | ||
traverse(new NodePath(node)); | ||
}; |
@@ -535,2 +535,26 @@ var assert = require("assert"); | ||
// Similar to eachField, except that iteration stops as soon as the | ||
// callback returns a truthy value. Like Array.prototype.some, the final | ||
// result is either true or false to indicates whether the callback | ||
// returned true for any element or not. | ||
Object.defineProperty(exports, "someField", { | ||
value: function(object, callback, context) { | ||
var d = Def.fromValue(object); | ||
if (d) { | ||
var all = d.allFields; | ||
return Object.keys(all).some(function(name) { | ||
var field = all[name]; | ||
if (!field.hidden) { | ||
var value = field.getValue(object); | ||
return callback.call(this, name, value); | ||
} | ||
}, context); | ||
} | ||
// If the object has no fields, then of course no field satisified | ||
// the predicate. | ||
return false; | ||
} | ||
}); | ||
// This property will be overridden as true by individual Def instances | ||
@@ -537,0 +561,0 @@ // when they are finalized. |
@@ -21,3 +21,3 @@ { | ||
], | ||
"version": "0.2.24", | ||
"version": "0.2.25", | ||
"homepage": "http://github.com/benjamn/ast-types", | ||
@@ -32,3 +32,5 @@ "repository": { | ||
}, | ||
"dependencies": {}, | ||
"dependencies": { | ||
"ast-path": "~0.1.0" | ||
}, | ||
"devDependencies": {}, | ||
@@ -35,0 +37,0 @@ "optionalDependencies": { |
400992
17
11266
3
+ Addedast-path@~0.1.0
+ Addedast-path@0.1.4(transitive)
+ Addedcommander@0.6.1(transitive)
+ Addeddebug@4.3.7(transitive)
+ Addeddiff@1.0.7(transitive)
+ Addedglob@3.2.3(transitive)
+ Addedgraceful-fs@2.0.3(transitive)
+ Addedgrowl@1.7.0(transitive)
+ Addedinherits@2.0.4(transitive)
+ Addedjade@0.26.3(transitive)
+ Addedlru-cache@2.7.3(transitive)
+ Addedminimatch@0.2.14(transitive)
+ Addedmkdirp@0.3.00.3.5(transitive)
+ Addedmocha@1.13.0(transitive)
+ Addedms@2.1.3(transitive)
+ Addedprivate@0.0.5(transitive)
+ Addedsigmund@1.0.1(transitive)