ast-types
Advanced tools
Comparing version 0.2.20 to 0.2.21
@@ -31,3 +31,7 @@ var assert = require("assert"); | ||
var path = new Path(node, parentPath); | ||
callback.call(path, node); | ||
if (callback.call(path, node) === false) { | ||
return; | ||
} | ||
types.eachField(node, function(name, child) { | ||
@@ -34,0 +38,0 @@ traverse(child, path); |
@@ -21,3 +21,3 @@ { | ||
], | ||
"version": "0.2.20", | ||
"version": "0.2.21", | ||
"homepage": "http://github.com/benjamn/ast-types", | ||
@@ -24,0 +24,0 @@ "repository": { |
@@ -75,2 +75,22 @@ AST Types | ||
var types = require("ast-types"); | ||
var Literal = types.namedTypes.Literal; | ||
var isString = types.builtInTypes.string; | ||
var stringCounts = {}; | ||
// Count the occurrences of all the string literals in this AST. | ||
require("ast-types").traverse(ast, function(node) { | ||
if (Literal.check(node) && isString.check(node.value)) { | ||
if (stringCounts.hasOwnProperty(node.value)) { | ||
stringCounts[node.value] += 1; | ||
} else { | ||
stringCounts[node.value] = 1; | ||
} | ||
} | ||
}); | ||
``` | ||
Here's an slightly deeper example demonstrating how to ignore certain | ||
subtrees and inspect the node's ancestors: | ||
```js | ||
var types = require("ast-types"); | ||
var namedTypes = types.namedTypes; | ||
@@ -81,4 +101,17 @@ var isString = types.builtInTypes.string; | ||
// Populate thisProperties with every property name accessed via | ||
// this.name or this["name"]. | ||
// this.name or this["name"] in the current scope. | ||
types.traverse(ast, function(node) { | ||
// Don't descend into new function scopes. | ||
if (namedTypes.FunctionExpression.check(node) || | ||
namedTypes.FunctionDeclaration.check(node)) { | ||
// Return false to stop traversing this subtree without aborting | ||
// the entire traversal. | ||
return false; | ||
} | ||
// If node is a ThisExpression that happens to be the .object of a | ||
// MemberExpression, then we're interested in the .property of the | ||
// MemberExpression. We could have inverted this test to find | ||
// MemberExpressions whose .object is a ThisExpression, but I wanted | ||
// to demonstrate the use of this.parent. | ||
if (namedTypes.ThisExpression.check(node) && | ||
@@ -91,2 +124,3 @@ namedTypes.MemberExpression.check(this.parent.node) && | ||
if (namedTypes.Identifier.check(property)) { | ||
// The this.name case. | ||
thisProperties[property.name] = true; | ||
@@ -96,2 +130,3 @@ | ||
isString.check(property.value)) { | ||
// The this["name"] case. | ||
thisProperties[property.value] = true; | ||
@@ -98,0 +133,0 @@ } |
@@ -294,3 +294,31 @@ var types = require("../main"); | ||
var ids = {}; | ||
traverse(ts, function(node) { | ||
if (n.MemberExpression.check(node)) { | ||
return false; | ||
} | ||
if (n.Identifier.check(node)) { | ||
ids[node.name] = true; | ||
} | ||
}); | ||
// Make sure all identifers beneath member expressions were skipped. | ||
assert.deepEqual(ids, { err: true }); | ||
traverse(ts, function(node) { | ||
if (n.Identifier.check(node)) { | ||
ids[node.name] = true; | ||
} | ||
}); | ||
// Now make sure those identifiers (foo and bar) were visited. | ||
assert.deepEqual(ids, { | ||
err: true, | ||
foo: true, | ||
bar: true | ||
}); | ||
t.finish(); | ||
}; |
387787
10881
201