ast-types
Advanced tools
Comparing version 0.2.18 to 0.2.19
@@ -103,2 +103,40 @@ require("./core"); | ||
// Specifier and NamedSpecifier are non-standard types that I introduced | ||
// for definitional convenience. | ||
def("Specifier").bases("Node"); | ||
def("NamedSpecifier") | ||
.bases("Specifier") | ||
.field("id", def("Identifier")) | ||
.field("name", def("Identifier"), defaults.null); | ||
def("ExportSpecifier") | ||
.bases("NamedSpecifier") | ||
.build("id", "name"); | ||
def("ExportBatchSpecifier") | ||
.bases("Specifier") | ||
.build(); | ||
def("ImportSpecifier") | ||
.bases("NamedSpecifier") | ||
.build("id", "name"); | ||
def("ExportDeclaration") | ||
.bases("Declaration") | ||
.build("default", "declaration", "specifiers", "source") | ||
.field("default", isBoolean) | ||
.field("declaration", def("Statement")) | ||
.field("specifiers", [or( | ||
def("ExportSpecifier"), | ||
def("ExportBatchSpecifier") | ||
)]) | ||
.field("source", def("Expression")); | ||
def("ImportDeclaration") | ||
.bases("Declaration") | ||
.build("specifiers", "kind", "source") | ||
.field("specifiers", [def("ImportSpecifier")]) | ||
.field("kind", or("named", "default")) | ||
.field("source", def("Expression")); | ||
types.finalize(); |
@@ -12,3 +12,3 @@ var types = require("./lib/types"); | ||
require("./def/e4x"); | ||
require("./def/xjs"); | ||
require("./def/fb-harmony"); | ||
@@ -15,0 +15,0 @@ exports.Type = types.Type; |
@@ -21,3 +21,3 @@ { | ||
], | ||
"version": "0.2.18", | ||
"version": "0.2.19", | ||
"homepage": "http://github.com/benjamn/ast-types", | ||
@@ -24,0 +24,0 @@ "repository": { |
@@ -54,2 +54,56 @@ AST Types | ||
AST Traversal | ||
--- | ||
Because it understands the AST type system so thoroughly, this library | ||
is able to provide excellent node iteration and traversal mechanisms. | ||
Here's how you might iterate over the fields of an arbitrary AST node: | ||
```js | ||
var copy = {}; | ||
require("ast-types").eachField(node, function(name, value) { | ||
// Note that undefined fields will be visited too, according to | ||
// the rules associated with node.type, and default field values | ||
// will be substituted if appropriate. | ||
copy[name] = value; | ||
}) | ||
``` | ||
If you want to perform a depth-first traversal of the entire AST, | ||
that's also easy: | ||
```js | ||
var types = require("ast-types"); | ||
var namedTypes = types.namedTypes; | ||
var isString = types.builtInTypes.string; | ||
var thisProperties = {}; | ||
// Populate thisProperties with every property name accessed via | ||
// this.name or this["name"]. | ||
types.traverse(ast, function(node) { | ||
if (namedTypes.ThisExpression.check(node) && | ||
namedTypes.MemberExpression.check(this.parent.node) && | ||
this.parent.node.object === node) { | ||
var property = this.parent.node.property; | ||
if (namedTypes.Identifier.check(property)) { | ||
thisProperties[property.name] = true; | ||
} else if (namedTypes.Literal.check(property) && | ||
isString.check(property.value)) { | ||
thisProperties[property.value] = true; | ||
} | ||
} | ||
}); | ||
``` | ||
Within the callback function, `this` is always an instance of a simple | ||
`Path` type that has immutable `.node` and `.parent` properties. In | ||
general, `this.node` refers to the same node as the `node` parameter, | ||
`this.parent.node` refers to the nearest `Node` ancestor, | ||
`this.parent.parent.node` to the grandparent, and so on. These `Path` | ||
objects are created during the traversal without modifying the AST | ||
nodes themselves, so it's not a problem if the same node appears more | ||
than once in the AST, because it will be visited with a distict `Path` | ||
each time it appears. | ||
Custom AST Node Types | ||
@@ -56,0 +110,0 @@ --- |
@@ -132,14 +132,7 @@ var types = require("../main"); | ||
var todo = { | ||
ClassBody: true, | ||
ClassDeclaration: true, | ||
ClassExpression: true, | ||
ClassHeritage: true, | ||
ComprehensionBlock: true, | ||
ComprehensionExpression: true, | ||
ExportDeclaration: true, | ||
ExportSpecifier: true, | ||
ExportSpecifierSet: true, | ||
Glob: true, | ||
ImportDeclaration: true, | ||
ImportSpecifier: true, | ||
TaggedTemplateExpression: true, | ||
@@ -146,0 +139,0 @@ TemplateElement: true, |
385411
10850
166