ast-types
Advanced tools
Comparing version 0.1.4 to 0.2.0
@@ -224,22 +224,3 @@ var assert = require("assert"); | ||
value: new Type(function(value, deep) { | ||
assert.ok( | ||
self.finalized, | ||
"prematurely checking unfinalized type " + typeName); | ||
// In most cases we rely exclusively on self.typeName and | ||
// value.type to make O(1) subtyping determinations. This | ||
// is sufficient in most situations outside of unit tests, | ||
// since interface conformance is checked whenever new | ||
// instances are created using builder functions. The | ||
// exception is when deep is true; then, we recursively | ||
// check all fields. | ||
return isObject.check(value) | ||
&& value.hasOwnProperty("type") | ||
&& defCache.hasOwnProperty(value.type) | ||
&& defCache[value.type].allSupertypes.hasOwnProperty(typeName) | ||
&& (!deep || Object.keys(self.allFields).every(function(name) { | ||
var type = self.allFields[name].type; | ||
return value.hasOwnProperty(name) | ||
&& type.check(value[name], deep); | ||
})); | ||
return self.check(value, deep); | ||
}, typeName) | ||
@@ -250,4 +231,50 @@ } | ||
Def.fromValue = function(value) { | ||
if (isObject.check(value) && | ||
value.hasOwnProperty("type") && | ||
defCache.hasOwnProperty(value.type)) | ||
{ | ||
var vDef = defCache[value.type]; | ||
assert.strictEqual(vDef.finalized, true); | ||
return vDef; | ||
} | ||
}; | ||
var Dp = Def.prototype; | ||
Dp.isSupertypeOf = function(that) { | ||
assert.ok(that instanceof Def, that + " is not a Def"); | ||
assert.strictEqual(this.finalized, true); | ||
assert.strictEqual(that.finalized, true); | ||
return that.allSupertypes.hasOwnProperty(this.typeName); | ||
}; | ||
Dp.checkAllFields = function(value, deep) { | ||
var allFields = this.allFields | ||
assert.strictEqual(this.finalized, true); | ||
return Object.keys(allFields).every(function(name) { | ||
var type = allFields[name].type; | ||
return value.hasOwnProperty(name) | ||
&& type.check(value[name], deep); | ||
}); | ||
}; | ||
Dp.check = function(value, deep) { | ||
assert.strictEqual( | ||
this.finalized, true, | ||
"prematurely checking unfinalized type " + this.typeName); | ||
// In most cases we rely exclusively on isSupertypeOf to make O(1) | ||
// subtyping determinations. This suffices in most situations outside | ||
// of unit tests, since interface conformance is checked whenever new | ||
// instances are created using builder functions. The exception is | ||
// when deep is true; then, we recursively check all fields. | ||
var vDef = Def.fromValue(value); | ||
return !!vDef // Coerce to boolean. | ||
&& this.isSupertypeOf(vDef) | ||
&& (!deep || (vDef.checkAllFields(value, true) && | ||
(this === vDef || // Shallow-check strict supertype fields. | ||
this.checkAllFields(value, false)))) | ||
}; | ||
Dp.bases = function() { | ||
@@ -254,0 +281,0 @@ var bases = this.baseNames; |
@@ -21,3 +21,3 @@ { | ||
], | ||
"version": "0.1.4", | ||
"version": "0.2.0", | ||
"homepage": "http://github.com/benjamn/ast-types", | ||
@@ -24,0 +24,0 @@ "repository": { |
@@ -57,5 +57,5 @@ var types = require("../lib/types"); | ||
assert.ok(n.Node.check(decl, true)); | ||
assert.ok(n.Statement.check(decl, true)); | ||
assert.ok(n.Declaration.check(decl, true)); | ||
assert.ok(!n.Node.check(decl, true)); | ||
assert.ok(!n.Statement.check(decl, true)); | ||
assert.ok(!n.Declaration.check(decl, true)); | ||
@@ -81,7 +81,6 @@ // As foretold above. | ||
assert.ok(n.Node.check(fs, true)); | ||
assert.ok(n.Statement.check(fs, true)); | ||
// Not a true ForStatement because fs.init is not a true | ||
// VariableDeclaration. | ||
assert.ok(!n.Node.check(fs, true)); | ||
assert.ok(!n.Statement.check(fs, true)); | ||
assert.ok(!n.ForStatement.check(fs, true)); | ||
@@ -88,0 +87,0 @@ |
40379
985