ast-types
Advanced tools
Comparing version 0.2.13 to 0.2.14
@@ -521,2 +521,18 @@ var assert = require("assert"); | ||
// Iterate over all defined fields of an object, including those missing | ||
// or undefined, passing each field name and effective value (as returned | ||
// by getFieldValue) to the callback. If the object has no corresponding | ||
// Def, the callback will never be called. | ||
Object.defineProperty(exports, "eachField", { | ||
value: function(object, callback, context) { | ||
var d = Def.fromValue(object); | ||
if (d) { | ||
var all = d.allFields; | ||
Object.keys(all).forEach(function(name) { | ||
callback.call(this, name, all[name].getValue(object)); | ||
}, context); | ||
} | ||
} | ||
}); | ||
// This property will be overridden as true by individual Def instances | ||
@@ -523,0 +539,0 @@ // when they are finalized. |
@@ -19,2 +19,3 @@ var types = require("./lib/types"); | ||
exports.getFieldValue = types.getFieldValue; | ||
exports.eachField = types.eachField; | ||
exports.finalize = types.finalize; |
@@ -21,3 +21,3 @@ { | ||
], | ||
"version": "0.2.13", | ||
"version": "0.2.14", | ||
"homepage": "http://github.com/benjamn/ast-types", | ||
@@ -24,0 +24,0 @@ "repository": { |
@@ -209,1 +209,38 @@ var types = require("../main"); | ||
}; | ||
exports.testEachField = function(t, assert) { | ||
var context = {}; | ||
function check(node, names) { | ||
var seen = []; | ||
types.eachField(node, function(name, value) { | ||
assert.strictEqual(this, context); | ||
if (name === "type") | ||
assert.strictEqual(node.type, value); | ||
seen.push(name); | ||
}, context); | ||
assert.deepEqual(seen.sort(), names.sort()); | ||
} | ||
check({ type: "Expression" }, [ | ||
"type", "loc" | ||
]); | ||
check({ type: "SourceLocation" }, [ | ||
"type", "start", "end", "source" | ||
]); | ||
check({ type: "TryStatement" }, [ | ||
"type", "block", "handlers", "guardedHandlers", "finalizer", "loc" | ||
]); | ||
check({ type: "CatchClause" }, [ | ||
"type", "param", "guard", "body", "loc" | ||
]); | ||
check({ type: "asdf" }, []); | ||
t.finish(); | ||
}; |
378932
10715