ast-types
Advanced tools
Comparing version 0.8.11 to 0.8.12
@@ -1,2 +0,1 @@ | ||
var assert = require("assert"); | ||
var types = require("../main"); | ||
@@ -25,6 +24,7 @@ var getFieldNames = types.getFieldNames; | ||
if (problemPath.length === 0) { | ||
assert.strictEqual(a, b); | ||
if (a !== b) { | ||
throw new Error("Nodes must be equal"); | ||
} | ||
} else { | ||
assert.ok( | ||
false, | ||
throw new Error( | ||
"Nodes differ in the following path: " + | ||
@@ -98,3 +98,6 @@ problemPath.map(subscriptForProperty).join("") | ||
if (problemPath) { | ||
assert.strictEqual(problemPath.pop(), i); | ||
var problemPathTail = problemPath.pop(); | ||
if (problemPathTail !== i) { | ||
throw new Error("" + problemPathTail); | ||
} | ||
} | ||
@@ -141,3 +144,6 @@ } | ||
if (problemPath) { | ||
assert.strictEqual(problemPath.pop(), name); | ||
var problemPathTail = problemPath.pop(); | ||
if (problemPathTail !== name) { | ||
throw new Error("" + problemPathTail); | ||
} | ||
} | ||
@@ -144,0 +150,0 @@ } |
@@ -1,2 +0,1 @@ | ||
var assert = require("assert"); | ||
var types = require("./types"); | ||
@@ -11,8 +10,16 @@ var n = types.namedTypes; | ||
function NodePath(value, parentPath, name) { | ||
assert.ok(this instanceof NodePath); | ||
if (!(this instanceof NodePath)) { | ||
throw new Error("NodePath constructor cannot be invoked without 'new'"); | ||
} | ||
Path.call(this, value, parentPath, name); | ||
} | ||
require("util").inherits(NodePath, Path); | ||
var NPp = NodePath.prototype; | ||
var NPp = NodePath.prototype = Object.create(Path.prototype, { | ||
constructor: { | ||
value: NodePath, | ||
enumerable: false, | ||
writable: true, | ||
configurable: true | ||
} | ||
}); | ||
@@ -196,3 +203,5 @@ Object.defineProperties(NPp, { | ||
if (pp === np && this.name === "right") { | ||
assert.strictEqual(parent.right, node); | ||
if (parent.right !== node) { | ||
throw new Error("Nodes must be equal"); | ||
} | ||
return true; | ||
@@ -355,3 +364,5 @@ } | ||
path.name === 0) { | ||
assert.strictEqual(parent.body[0], node); | ||
if (parent.body[0] !== node) { | ||
throw new Error("Nodes must be equal"); | ||
} | ||
return true; | ||
@@ -362,3 +373,5 @@ } | ||
path.name === "expression") { | ||
assert.strictEqual(parent.expression, node); | ||
if (parent.expression !== node) { | ||
throw new Error("Nodes must be equal"); | ||
} | ||
return true; | ||
@@ -370,3 +383,5 @@ } | ||
path.name === 0) { | ||
assert.strictEqual(parent.expressions[0], node); | ||
if (parent.expressions[0] !== node) { | ||
throw new Error("Nodes must be equal"); | ||
} | ||
continue; | ||
@@ -377,3 +392,5 @@ } | ||
path.name === "callee") { | ||
assert.strictEqual(parent.callee, node); | ||
if (parent.callee !== node) { | ||
throw new Error("Nodes must be equal"); | ||
} | ||
continue; | ||
@@ -384,3 +401,5 @@ } | ||
path.name === "object") { | ||
assert.strictEqual(parent.object, node); | ||
if (parent.object !== node) { | ||
throw new Error("Nodes must be equal"); | ||
} | ||
continue; | ||
@@ -391,3 +410,5 @@ } | ||
path.name === "test") { | ||
assert.strictEqual(parent.test, node); | ||
if (parent.test !== node) { | ||
throw new Error("Nodes must be equal"); | ||
} | ||
continue; | ||
@@ -398,3 +419,5 @@ } | ||
path.name === "left") { | ||
assert.strictEqual(parent.left, node); | ||
if (parent.left !== node) { | ||
throw new Error("Nodes must be equal"); | ||
} | ||
continue; | ||
@@ -406,3 +429,5 @@ } | ||
path.name === "argument") { | ||
assert.strictEqual(parent.argument, node); | ||
if (parent.argument !== node) { | ||
throw new Error("Nodes must be equal"); | ||
} | ||
continue; | ||
@@ -409,0 +434,0 @@ } |
@@ -1,2 +0,1 @@ | ||
var assert = require("assert"); | ||
var types = require("./types"); | ||
@@ -12,3 +11,7 @@ var NodePath = require("./node-path"); | ||
function PathVisitor() { | ||
assert.ok(this instanceof PathVisitor); | ||
if (!(this instanceof PathVisitor)) { | ||
throw new Error( | ||
"PathVisitor constructor cannot be invoked without 'new'" | ||
); | ||
} | ||
@@ -66,3 +69,7 @@ // Permanent state. | ||
function Visitor() { | ||
assert.ok(this instanceof Visitor); | ||
if (!(this instanceof Visitor)) { | ||
throw new Error( | ||
"Visitor constructor cannot be invoked without 'new'" | ||
); | ||
} | ||
PathVisitor.call(this); | ||
@@ -99,9 +106,9 @@ } | ||
var recursiveVisitWarning = [ | ||
"Recursively calling visitor.visit(path) resets visitor state.", | ||
"Try this.visit(path) or this.traverse(path) instead." | ||
].join(" "); | ||
PVp.visit = function() { | ||
assert.ok(!this._visiting, recursiveVisitWarning); | ||
if (this._visiting) { | ||
throw new Error( | ||
"Recursively calling visitor.visit(path) resets visitor state. " + | ||
"Try this.visit(path) or this.traverse(path) instead." | ||
); | ||
} | ||
@@ -176,3 +183,6 @@ // Private state that needs to be reset before every traversal. | ||
assert.ok(path instanceof NodePath); | ||
if (!(path instanceof NodePath)) { | ||
throw new Error(""); | ||
} | ||
var value = path.value; | ||
@@ -201,4 +211,8 @@ | ||
function visitChildren(path, visitor) { | ||
assert.ok(path instanceof NodePath); | ||
assert.ok(visitor instanceof PathVisitor); | ||
if (!(path instanceof NodePath)) { | ||
throw new Error(""); | ||
} | ||
if (!(visitor instanceof PathVisitor)) { | ||
throw new Error(""); | ||
} | ||
@@ -250,3 +264,5 @@ var value = path.value; | ||
PVp.releaseContext = function(context) { | ||
assert.ok(context instanceof this.Context); | ||
if (!(context instanceof this.Context)) { | ||
throw new Error(""); | ||
} | ||
this._reusableContextStack.push(context); | ||
@@ -266,5 +282,11 @@ context.currentPath = null; | ||
function Context(path) { | ||
assert.ok(this instanceof Context); | ||
assert.ok(this instanceof PathVisitor); | ||
assert.ok(path instanceof NodePath); | ||
if (!(this instanceof Context)) { | ||
throw new Error(""); | ||
} | ||
if (!(this instanceof PathVisitor)) { | ||
throw new Error(""); | ||
} | ||
if (!(path instanceof NodePath)) { | ||
throw new Error(""); | ||
} | ||
@@ -284,3 +306,5 @@ Object.defineProperty(this, "visitor", { | ||
assert.ok(visitor instanceof PathVisitor); | ||
if (!(visitor instanceof PathVisitor)) { | ||
throw new Error(""); | ||
} | ||
@@ -304,4 +328,8 @@ // Note that the visitor object is the prototype of Context.prototype, | ||
function reset(path) { | ||
assert.ok(this instanceof this.Context); | ||
assert.ok(path instanceof NodePath); | ||
if (!(this instanceof this.Context)) { | ||
throw new Error(""); | ||
} | ||
if (!(path instanceof NodePath)) { | ||
throw new Error(""); | ||
} | ||
@@ -316,4 +344,8 @@ this.currentPath = path; | ||
function invokeVisitorMethod(methodName) { | ||
assert.ok(this instanceof this.Context); | ||
assert.ok(this.currentPath instanceof NodePath); | ||
if (!(this instanceof this.Context)) { | ||
throw new Error(""); | ||
} | ||
if (!(this.currentPath instanceof NodePath)) { | ||
throw new Error(""); | ||
} | ||
@@ -340,6 +372,7 @@ var result = this.visitor[methodName].call(this, this.currentPath); | ||
assert.strictEqual( | ||
this.needToCallTraverse, false, | ||
"Must either call this.traverse or return false in " + methodName | ||
); | ||
if (this.needToCallTraverse !== false) { | ||
throw new Error( | ||
"Must either call this.traverse or return false in " + methodName | ||
); | ||
} | ||
@@ -352,5 +385,11 @@ var path = this.currentPath; | ||
function traverse(path, newVisitor) { | ||
assert.ok(this instanceof this.Context); | ||
assert.ok(path instanceof NodePath); | ||
assert.ok(this.currentPath instanceof NodePath); | ||
if (!(this instanceof this.Context)) { | ||
throw new Error(""); | ||
} | ||
if (!(path instanceof NodePath)) { | ||
throw new Error(""); | ||
} | ||
if (!(this.currentPath instanceof NodePath)) { | ||
throw new Error(""); | ||
} | ||
@@ -366,5 +405,11 @@ this.needToCallTraverse = false; | ||
function visit(path, newVisitor) { | ||
assert.ok(this instanceof this.Context); | ||
assert.ok(path instanceof NodePath); | ||
assert.ok(this.currentPath instanceof NodePath); | ||
if (!(this instanceof this.Context)) { | ||
throw new Error(""); | ||
} | ||
if (!(path instanceof NodePath)) { | ||
throw new Error(""); | ||
} | ||
if (!(this.currentPath instanceof NodePath)) { | ||
throw new Error(""); | ||
} | ||
@@ -371,0 +416,0 @@ this.needToCallTraverse = false; |
@@ -1,2 +0,1 @@ | ||
var assert = require("assert"); | ||
var Op = Object.prototype; | ||
@@ -12,6 +11,10 @@ var hasOwn = Op.hasOwnProperty; | ||
function Path(value, parentPath, name) { | ||
assert.ok(this instanceof Path); | ||
if (!(this instanceof Path)) { | ||
throw new Error("Path constructor cannot be invoked without 'new'"); | ||
} | ||
if (parentPath) { | ||
assert.ok(parentPath instanceof Path); | ||
if (!(parentPath instanceof Path)) { | ||
throw new Error(""); | ||
} | ||
} else { | ||
@@ -158,3 +161,5 @@ parentPath = null; | ||
var childPath = path.get(i); | ||
assert.strictEqual(childPath.name, i); | ||
if (childPath.name !== i) { | ||
throw new Error(""); | ||
} | ||
var newIndex = i + offset; | ||
@@ -172,3 +177,5 @@ childPath.name = newIndex; | ||
var childPath = moves[newIndex]; | ||
assert.strictEqual(childPath.name, +newIndex); | ||
if (childPath.name !== +newIndex) { | ||
throw new Error(""); | ||
} | ||
cache[newIndex] = childPath; | ||
@@ -247,3 +254,5 @@ path.value[newIndex] = childPath.value; | ||
function repairRelationshipWithParent(path) { | ||
assert.ok(path instanceof Path); | ||
if (!(path instanceof Path)) { | ||
throw new Error(""); | ||
} | ||
@@ -277,4 +286,8 @@ var pp = path.parentPath; | ||
assert.strictEqual(parentValue[path.name], path.value); | ||
assert.strictEqual(path.parentPath.get(path.name), path); | ||
if (parentValue[path.name] !== path.value) { | ||
throw new Error(""); | ||
} | ||
if (path.parentPath.get(path.name) !== path) { | ||
throw new Error(""); | ||
} | ||
@@ -303,7 +316,8 @@ return path; | ||
assert.strictEqual(splicedOut[0], this.value); | ||
assert.strictEqual( | ||
parentValue.length, | ||
originalLength - 1 + count | ||
); | ||
if (splicedOut[0] !== this.value) { | ||
throw new Error(""); | ||
} | ||
if (parentValue.length !== (originalLength - 1 + count)) { | ||
throw new Error(""); | ||
} | ||
@@ -318,3 +332,5 @@ move(); | ||
} else { | ||
assert.strictEqual(parentValue[this.name], replacement); | ||
if (parentValue[this.name] !== replacement) { | ||
throw new Error(""); | ||
} | ||
@@ -330,3 +346,5 @@ if (this.value !== replacement) { | ||
assert.strictEqual(results[0], this); | ||
if (results[0] !== this) { | ||
throw new Error(""); | ||
} | ||
} | ||
@@ -350,3 +368,3 @@ | ||
} else { | ||
assert.ok(false, "Could not replace path"); | ||
throw new Error("Could not replace path"); | ||
} | ||
@@ -353,0 +371,0 @@ |
@@ -1,2 +0,1 @@ | ||
var assert = require("assert"); | ||
var types = require("./types"); | ||
@@ -12,4 +11,8 @@ var Type = types.Type; | ||
function Scope(path, parentScope) { | ||
assert.ok(this instanceof Scope); | ||
assert.ok(path instanceof require("./node-path")); | ||
if (!(this instanceof Scope)) { | ||
throw new Error("Scope constructor cannot be invoked without 'new'"); | ||
} | ||
if (!(path instanceof require("./node-path"))) { | ||
throw new Error(""); | ||
} | ||
ScopeType.assert(path.value); | ||
@@ -20,3 +23,5 @@ | ||
if (parentScope) { | ||
assert.ok(parentScope instanceof Scope); | ||
if (!(parentScope instanceof Scope)) { | ||
throw new Error(""); | ||
} | ||
depth = parentScope.depth + 1; | ||
@@ -69,3 +74,5 @@ } else { | ||
if (prefix) { | ||
assert.ok(/^[a-z$_]/i.test(prefix), prefix); | ||
if (!/^[a-z$_]/i.test(prefix)) { | ||
throw new Error(""); | ||
} | ||
} else { | ||
@@ -183,3 +190,5 @@ prefix = "t$"; | ||
var childPath = path.get(name); | ||
assert.strictEqual(childPath.value, child); | ||
if (childPath.value !== child) { | ||
throw new Error(""); | ||
} | ||
recursiveScanChild(childPath, bindings); | ||
@@ -186,0 +195,0 @@ }); |
110
lib/types.js
@@ -1,2 +0,1 @@ | ||
var assert = require("assert"); | ||
var Ap = Array.prototype; | ||
@@ -17,14 +16,18 @@ var slice = Ap.slice; | ||
var self = this; | ||
assert.ok(self instanceof Type, self); | ||
if (!(self instanceof Type)) { | ||
throw new Error("Type constructor cannot be invoked without 'new'"); | ||
} | ||
// Unfortunately we can't elegantly reuse isFunction and isString, | ||
// here, because this code is executed while defining those types. | ||
assert.strictEqual(objToStr.call(check), funObjStr, | ||
check + " is not a function"); | ||
if (objToStr.call(check) !== funObjStr) { | ||
throw new Error(check + " is not a function"); | ||
} | ||
// The `name` parameter can be either a function or a string. | ||
var nameObjStr = objToStr.call(name); | ||
assert.ok(nameObjStr === funObjStr || | ||
nameObjStr === strObjStr, | ||
name + " is neither a function nor a string"); | ||
if (!(nameObjStr === funObjStr || | ||
nameObjStr === strObjStr)) { | ||
throw new Error(name + " is neither a function nor a string"); | ||
} | ||
@@ -54,4 +57,3 @@ Object.defineProperties(self, { | ||
var str = shallowStringify(value); | ||
assert.ok(false, str + " does not match type " + this); | ||
return false; | ||
throw new Error(str + " does not match type " + this); | ||
} | ||
@@ -185,6 +187,8 @@ return true; | ||
Type.fromArray = function(arr) { | ||
assert.ok(isArray.check(arr)); | ||
assert.strictEqual( | ||
arr.length, 1, | ||
"only one element type is permitted for typed arrays"); | ||
if (!isArray.check(arr)) { | ||
throw new Error(""); | ||
} | ||
if (arr.length !== 1) { | ||
throw new Error("only one element type is permitted for typed arrays"); | ||
} | ||
return toType(arr[0]).arrayOf(); | ||
@@ -221,3 +225,5 @@ }; | ||
assert.ok(self instanceof Field); | ||
if (!(self instanceof Field)) { | ||
throw new Error("Field constructor cannot be invoked without 'new'"); | ||
} | ||
isString.assert(name); | ||
@@ -276,3 +282,5 @@ | ||
var self = this; | ||
assert.ok(self instanceof Def); | ||
if (!(self instanceof Def)) { | ||
throw new Error("Def constructor cannot be invoked without 'new'"); | ||
} | ||
@@ -317,7 +325,9 @@ Object.defineProperties(self, { | ||
if (that instanceof Def) { | ||
assert.strictEqual(this.finalized, true); | ||
assert.strictEqual(that.finalized, true); | ||
if (this.finalized !== true || | ||
that.finalized !== true) { | ||
throw new Error(""); | ||
} | ||
return hasOwn.call(that.allSupertypes, this.typeName); | ||
} else { | ||
assert.ok(false, that + " is not a Def"); | ||
throw new Error(that + " is not a Def"); | ||
} | ||
@@ -329,5 +339,9 @@ }; | ||
exports.getSupertypeNames = function(typeName) { | ||
assert.ok(hasOwn.call(defCache, typeName)); | ||
if (!hasOwn.call(defCache, typeName)) { | ||
throw new Error(""); | ||
} | ||
var d = defCache[typeName]; | ||
assert.strictEqual(d.finalized, true); | ||
if (d.finalized !== true) { | ||
throw new Error(""); | ||
} | ||
return d.supertypeList.slice(1); | ||
@@ -347,3 +361,5 @@ }; | ||
var d = defCache[typeName]; | ||
assert.strictEqual(d.finalized, true, typeName); | ||
if (d.finalized !== true) { | ||
throw new Error("" + typeName); | ||
} | ||
for (var j = 0; j < d.supertypeList.length; ++j) { | ||
@@ -363,3 +379,5 @@ var superTypeName = d.supertypeList[j]; | ||
var allFields = this.allFields; | ||
assert.strictEqual(this.finalized, true, this.typeName); | ||
if (this.finalized !== true) { | ||
throw new Error("" + this.typeName); | ||
} | ||
@@ -378,5 +396,7 @@ function checkFieldByName(name) { | ||
Dp.check = function(value, deep) { | ||
assert.strictEqual( | ||
this.finalized, true, | ||
"prematurely checking unfinalized type " + this.typeName); | ||
if (this.finalized !== true) { | ||
throw new Error( | ||
"prematurely checking unfinalized type " + this.typeName | ||
); | ||
} | ||
@@ -433,3 +453,10 @@ // A Def type can only match an object value. | ||
if (this.finalized) { | ||
assert.deepEqual(args, bases); | ||
if (args.length !== bases.length) { | ||
throw new Error(""); | ||
} | ||
for (var i = 0; i < args.length; i++) { | ||
if (args[i] !== bases[i]) { | ||
throw new Error(""); | ||
} | ||
} | ||
return this; | ||
@@ -524,5 +551,8 @@ } | ||
assert.ok( | ||
self.finalized, | ||
"attempting to instantiate unfinalized type " + self.typeName); | ||
if (!self.finalized) { | ||
throw new Error( | ||
"attempting to instantiate unfinalized type " + | ||
self.typeName | ||
); | ||
} | ||
@@ -534,3 +564,5 @@ function add(param, i) { | ||
var all = self.allFields; | ||
assert.ok(hasOwn.call(all, param), param); | ||
if (!hasOwn.call(all, param)) { | ||
throw new Error("" + param); | ||
} | ||
@@ -553,8 +585,7 @@ var field = all[param]; | ||
}).join(", ") + ")"; | ||
assert.ok(false, message); | ||
throw new Error(message); | ||
} | ||
if (!type.check(value)) { | ||
assert.ok( | ||
false, | ||
throw new Error( | ||
shallowStringify(value) + | ||
@@ -580,3 +611,5 @@ " does not match field " + field + | ||
// Make sure that the "type" field was filled automatically. | ||
assert.strictEqual(built.type, self.typeName); | ||
if (built.type !== self.typeName) { | ||
throw new Error(""); | ||
} | ||
@@ -641,4 +674,3 @@ return built; | ||
if ("type" in object) { | ||
assert.ok( | ||
false, | ||
throw new Error( | ||
"did not recognize object of type " + | ||
@@ -712,3 +744,3 @@ JSON.stringify(object.type) | ||
JSON.stringify(self.typeName); | ||
assert.ok(false, message); | ||
throw new Error(message); | ||
} | ||
@@ -774,3 +806,5 @@ }); | ||
var d = defCache[typeName]; | ||
assert.strictEqual(d.finalized, true); | ||
if (d.finalized !== true) { | ||
throw new Error(""); | ||
} | ||
@@ -777,0 +811,0 @@ // If we saw typeName earlier in the breadth-first traversal, |
@@ -21,3 +21,3 @@ { | ||
], | ||
"version": "0.8.11", | ||
"version": "0.8.12", | ||
"homepage": "http://github.com/benjamn/ast-types", | ||
@@ -24,0 +24,0 @@ "repository": { |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
132330
3236