ast-types
Advanced tools
Comparing version 0.2.11 to 0.2.12
@@ -226,2 +226,14 @@ var assert = require("assert"); | ||
Fp.getValue = function(obj) { | ||
var value = obj[this.name]; | ||
if (!isUndefined.check(value)) | ||
return value; | ||
if (this.defaultFn) | ||
value = this.defaultFn.call(obj); | ||
return value; | ||
}; | ||
// Define a type whose name is registered in a namespace (the defCache) so | ||
@@ -285,3 +297,3 @@ // that future definitions will return the same type given the same name. | ||
Dp.checkAllFields = function(value, deep) { | ||
var allFields = this.allFields | ||
var allFields = this.allFields; | ||
assert.strictEqual(this.finalized, true); | ||
@@ -292,23 +304,3 @@ | ||
var type = field.type; | ||
var child = value[name]; | ||
// If a property with this name is defined on value, simply check | ||
// the type of the child value. | ||
if (!isUndefined.check(child)) | ||
return type.check(child, deep); | ||
// If there is no default function for this field, treat the | ||
// missing property as a type error. | ||
if (!field.defaultFn) | ||
return false; | ||
// If there is a default function for this field, and we are not | ||
// checking deeply, just assume that the default function would | ||
// return something that matches field.type. | ||
if (!deep) | ||
return true; | ||
// If we are checking deeply, go ahead and deeply check the | ||
// result returned by the default function. | ||
child = field.defaultFn.call(value); | ||
var child = field.getValue(value); | ||
var result = type.check(child, deep); | ||
@@ -513,2 +505,20 @@ if (result) { | ||
// Get the value of an object property, taking object.type and default | ||
// functions into account. | ||
Object.defineProperty(exports, "getFieldValue", { | ||
value: function(object, fieldName) { | ||
isObject.assert(object); | ||
var d = Def.fromValue(object); | ||
if (d) { | ||
var field = d.allFields[fieldName]; | ||
if (field) { | ||
return field.getValue(object); | ||
} | ||
} | ||
return object[fieldName]; | ||
} | ||
}); | ||
// This property will be overridden as true by individual Def instances | ||
@@ -515,0 +525,0 @@ // when they are finalized. |
@@ -18,2 +18,3 @@ var types = require("./lib/types"); | ||
exports.builders = types.builders; | ||
exports.getFieldValue = types.getFieldValue; | ||
exports.finalize = types.finalize; |
@@ -21,3 +21,3 @@ { | ||
], | ||
"version": "0.2.11", | ||
"version": "0.2.12", | ||
"homepage": "http://github.com/benjamn/ast-types", | ||
@@ -24,0 +24,0 @@ "repository": { |
@@ -161,1 +161,49 @@ var types = require("../main"); | ||
}; | ||
exports.testGetFieldValue = function(t, assert) { | ||
assert.strictEqual( | ||
types.getFieldValue({ | ||
type: "CatchClause" | ||
}, "guard"), | ||
null | ||
); | ||
assert.strictEqual( | ||
types.getFieldValue({ | ||
type: "CatchClause" | ||
}, "asdf"), | ||
void 0 | ||
); | ||
assert.strictEqual( | ||
types.getFieldValue({ | ||
type: "CatchClause" | ||
}, "type"), | ||
"CatchClause" | ||
); | ||
assert.strictEqual( | ||
types.getFieldValue({ | ||
type: "CatchClause", | ||
guard: b.identifier("test") | ||
}, "guard").name, | ||
"test" | ||
); | ||
assert.deepEqual( | ||
types.getFieldValue({ | ||
type: "TryStatement", | ||
}, "guardedHandlers"), | ||
[] | ||
); | ||
assert.deepEqual( | ||
types.getFieldValue({ | ||
type: "TryStatement", | ||
guardedHandlers: void 0 | ||
}, "guardedHandlers"), | ||
[] | ||
); | ||
t.finish(); | ||
}; |
10670
377410
14