node-puppetdbquery
Version:
a simple query language parser for PuppetDB v4 API
1,600 lines (1,303 loc) • 239 kB
JavaScript
(function(e, a) { for(var i in a) e[i] = a[i]; }(exports, /******/ (function(modules) { // webpackBootstrap
/******/ // The module cache
/******/ var installedModules = {};
/******/
/******/ // The require function
/******/ function __webpack_require__(moduleId) {
/******/
/******/ // Check if module is in cache
/******/ if(installedModules[moduleId])
/******/ return installedModules[moduleId].exports;
/******/
/******/ // Create a new module (and put it into the cache)
/******/ var module = installedModules[moduleId] = {
/******/ exports: {},
/******/ id: moduleId,
/******/ loaded: false
/******/ };
/******/
/******/ // Execute the module function
/******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
/******/
/******/ // Flag the module as loaded
/******/ module.loaded = true;
/******/
/******/ // Return the exports of the module
/******/ return module.exports;
/******/ }
/******/
/******/
/******/ // expose the modules object (__webpack_modules__)
/******/ __webpack_require__.m = modules;
/******/
/******/ // expose the module cache
/******/ __webpack_require__.c = installedModules;
/******/
/******/ // __webpack_public_path__
/******/ __webpack_require__.p = "";
/******/
/******/ // Load entry module and return exports
/******/ return __webpack_require__(0);
/******/ })
/************************************************************************/
/******/ ([
/* 0 */
/***/ (function(module, exports, __webpack_require__) {
module.exports = __webpack_require__(1);
/***/ }),
/* 1 */
/***/ (function(module, exports, __webpack_require__) {
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.parse = exports.parser = exports.evaluator = undefined;
var _ast = __webpack_require__(2);
var _ast2 = _interopRequireDefault(_ast);
__webpack_require__(9);
var _parser = __webpack_require__(10);
var _parser2 = _interopRequireDefault(_parser);
var _evaluator = __webpack_require__(13);
var _evaluator2 = _interopRequireDefault(_evaluator);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
var parse = function parse(query) {
var ast = _parser2.default.parse(query);
return (0, _evaluator2.default)(ast);
};
exports.evaluator = _evaluator2.default;
exports.parser = _parser2.default;
exports.parse = parse;
/***/ }),
/* 2 */
/***/ (function(module, exports, __webpack_require__) {
'use strict';
var _types = __webpack_require__(3);
var _types2 = _interopRequireDefault(_types);
var _shared = __webpack_require__(4);
var _shared2 = _interopRequireDefault(_shared);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
var def = _types2.default.Type.def;
var or = _types2.default.Type.or;
var builtin = _types2.default.builtInTypes;
var isString = builtin.string;
var isNumber = builtin.number;
var isBoolean = builtin.boolean;
var defaults = _shared2.default.defaults;
var geq = _shared2.default.geq;
def('Printable').field('loc', or(def('SourceLocation'), null), defaults.null, true);
def('Node').bases('Printable').field('type', isString);
def('SourceLocation').build('start', 'end', 'source').field('start', def('Position')).field('end', def('Position')).field('source', or(isString, null), defaults.null);
def('Position').build('line', 'column').field('line', geq(1)).field('column', geq(0));
def('Literal').bases('Node');
// Merge literals into Literal type?
def('Boolean').bases('Literal').build('value').field('value', isBoolean);
def('String').bases('Literal').build('value').field('value', isString);
def('Number').bases('Literal').build('value').field('value', isNumber);
def('Date').bases('Literal').build('value').field('value', isString);
def('Expression').bases('Node');
def('BinaryExpression').bases('Expression').field('left', def('Expression')).field('right', def('Expression'));
def('UnaryExpression').bases('Expression').field('expression', def('Expression'));
def('AndExpression').bases('BinaryExpression').build('left', 'right');
def('OrExpression').bases('BinaryExpression').build('left', 'right');
def('NotExpression').bases('UnaryExpression').build('expression');
def('ParentesizedExpression').bases('UnaryExpression').build('expression');
def('BlockExpression').bases('UnaryExpression').build('expression');
def('Comparison').bases('Expression').build('operator', 'left', 'right').field('operator', or('=', '>=', '<=', '<', '>', '!=', '~', '!~')).field('left', def('IdentifierPath')).field('right', def('Literal'));
def('Identifier').bases('Node').build('name').field('name', or(isString, isNumber));
def('RegexpIdentifier').bases('Identifier').build('name');
def('IdentifierPath').bases('Node').build('components', 'regexp').field('components', [or(def('Identifier'), null)]).field('regexp', isBoolean, defaults.false);
def('Query').bases('Node').build('expression').field('expression', or(def('Expression'), null), defaults.null);
def('Subquery').bases('Expression').build('endpoint', 'expression').field('endpoint', isString).field('expression', def('Expression'));
def('Resource').bases('Expression').build('res_type', 'title', 'exported', 'parameters').field('res_type', isString).field('title', def('Identifier')).field('exported', isBoolean).field('parameters', or(def('BlockExpression'), null), defaults.null);
def('RegexpNodeMatch').bases('Expression').build('value')
// TODO: his is a bit silly, should just have the string directly here
.field('value', def('IdentifierPath'));
_types2.default.finalize();
exports.Type = _types2.default.Type;
exports.builtInTypes = _types2.default.builtInTypes;
exports.namedTypes = _types2.default.namedTypes;
exports.builders = _types2.default.builders;
exports.defineMethod = _types2.default.defineMethod;
exports.getFieldNames = _types2.default.getFieldNames;
exports.getFieldValue = _types2.default.getFieldValue;
exports.eachField = _types2.default.eachField;
exports.someField = _types2.default.someField;
exports.getSupertypeNames = _types2.default.getSupertypeNames;
exports.finalize = _types2.default.finalize;
exports.NodePath = __webpack_require__(5);
exports.PathVisitor = __webpack_require__(8);
exports.visit = exports.PathVisitor.visit;
/***/ }),
/* 3 */
/***/ (function(module, exports) {
var Ap = Array.prototype;
var slice = Ap.slice;
var map = Ap.map;
var each = Ap.forEach;
var Op = Object.prototype;
var objToStr = Op.toString;
var funObjStr = objToStr.call(function(){});
var strObjStr = objToStr.call("");
var hasOwn = Op.hasOwnProperty;
// A type is an object with a .check method that takes a value and returns
// true or false according to whether the value matches the type.
function Type(check, name) {
var self = this;
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.
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);
if (!(nameObjStr === funObjStr ||
nameObjStr === strObjStr)) {
throw new Error(name + " is neither a function nor a string");
}
Object.defineProperties(self, {
name: { value: name },
check: {
value: function(value, deep) {
var result = check.call(self, value, deep);
if (!result && deep && objToStr.call(deep) === funObjStr)
deep(self, value);
return result;
}
}
});
}
var Tp = Type.prototype;
// Throughout this file we use Object.defineProperty to prevent
// redefinition of exported properties.
exports.Type = Type;
// Like .check, except that failure triggers an AssertionError.
Tp.assert = function(value, deep) {
if (!this.check(value, deep)) {
var str = shallowStringify(value);
throw new Error(str + " does not match type " + this);
}
return true;
};
function shallowStringify(value) {
if (isObject.check(value))
return "{" + Object.keys(value).map(function(key) {
return key + ": " + value[key];
}).join(", ") + "}";
if (isArray.check(value))
return "[" + value.map(shallowStringify).join(", ") + "]";
return JSON.stringify(value);
}
Tp.toString = function() {
var name = this.name;
if (isString.check(name))
return name;
if (isFunction.check(name))
return name.call(this) + "";
return name + " type";
};
var builtInCtorFns = [];
var builtInCtorTypes = [];
var builtInTypes = {};
exports.builtInTypes = builtInTypes;
function defBuiltInType(example, name) {
var objStr = objToStr.call(example);
var type = new Type(function(value) {
return objToStr.call(value) === objStr;
}, name);
builtInTypes[name] = type;
if (example && typeof example.constructor === "function") {
builtInCtorFns.push(example.constructor);
builtInCtorTypes.push(type);
}
return type;
}
// These types check the underlying [[Class]] attribute of the given
// value, rather than using the problematic typeof operator. Note however
// that no subtyping is considered; so, for instance, isObject.check
// returns false for [], /./, new Date, and null.
var isString = defBuiltInType("truthy", "string");
var isFunction = defBuiltInType(function(){}, "function");
var isArray = defBuiltInType([], "array");
var isObject = defBuiltInType({}, "object");
var isRegExp = defBuiltInType(/./, "RegExp");
var isDate = defBuiltInType(new Date, "Date");
var isNumber = defBuiltInType(3, "number");
var isBoolean = defBuiltInType(true, "boolean");
var isNull = defBuiltInType(null, "null");
var isUndefined = defBuiltInType(void 0, "undefined");
// There are a number of idiomatic ways of expressing types, so this
// function serves to coerce them all to actual Type objects. Note that
// providing the name argument is not necessary in most cases.
function toType(from, name) {
// The toType function should of course be idempotent.
if (from instanceof Type)
return from;
// The Def type is used as a helper for constructing compound
// interface types for AST nodes.
if (from instanceof Def)
return from.type;
// Support [ElemType] syntax.
if (isArray.check(from))
return Type.fromArray(from);
// Support { someField: FieldType, ... } syntax.
if (isObject.check(from))
return Type.fromObject(from);
if (isFunction.check(from)) {
var bicfIndex = builtInCtorFns.indexOf(from);
if (bicfIndex >= 0) {
return builtInCtorTypes[bicfIndex];
}
// If isFunction.check(from), and from is not a built-in
// constructor, assume from is a binary predicate function we can
// use to define the type.
return new Type(from, name);
}
// As a last resort, toType returns a type that matches any value that
// is === from. This is primarily useful for literal values like
// toType(null), but it has the additional advantage of allowing
// toType to be a total function.
return new Type(function(value) {
return value === from;
}, isUndefined.check(name) ? function() {
return from + "";
} : name);
}
// Returns a type that matches the given value iff any of type1, type2,
// etc. match the value.
Type.or = function(/* type1, type2, ... */) {
var types = [];
var len = arguments.length;
for (var i = 0; i < len; ++i)
types.push(toType(arguments[i]));
return new Type(function(value, deep) {
for (var i = 0; i < len; ++i)
if (types[i].check(value, deep))
return true;
return false;
}, function() {
return types.join(" | ");
});
};
Type.fromArray = function(arr) {
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();
};
Tp.arrayOf = function() {
var elemType = this;
return new Type(function(value, deep) {
return isArray.check(value) && value.every(function(elem) {
return elemType.check(elem, deep);
});
}, function() {
return "[" + elemType + "]";
});
};
Type.fromObject = function(obj) {
var fields = Object.keys(obj).map(function(name) {
return new Field(name, obj[name]);
});
return new Type(function(value, deep) {
return isObject.check(value) && fields.every(function(field) {
return field.type.check(value[field.name], deep);
});
}, function() {
return "{ " + fields.join(", ") + " }";
});
};
function Field(name, type, defaultFn, hidden) {
var self = this;
if (!(self instanceof Field)) {
throw new Error("Field constructor cannot be invoked without 'new'");
}
isString.assert(name);
type = toType(type);
var properties = {
name: { value: name },
type: { value: type },
hidden: { value: !!hidden }
};
if (isFunction.check(defaultFn)) {
properties.defaultFn = { value: defaultFn };
}
Object.defineProperties(self, properties);
}
var Fp = Field.prototype;
Fp.toString = function() {
return JSON.stringify(this.name) + ": " + this.type;
};
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
// that future definitions will return the same type given the same name.
// In particular, this system allows for circular and forward definitions.
// The Def object d returned from Type.def may be used to configure the
// type d.type by calling methods such as d.bases, d.build, and d.field.
Type.def = function(typeName) {
isString.assert(typeName);
return hasOwn.call(defCache, typeName)
? defCache[typeName]
: defCache[typeName] = new Def(typeName);
};
// In order to return the same Def instance every time Type.def is called
// with a particular name, those instances need to be stored in a cache.
var defCache = Object.create(null);
function Def(typeName) {
var self = this;
if (!(self instanceof Def)) {
throw new Error("Def constructor cannot be invoked without 'new'");
}
Object.defineProperties(self, {
typeName: { value: typeName },
baseNames: { value: [] },
ownFields: { value: Object.create(null) },
// These two are populated during finalization.
allSupertypes: { value: Object.create(null) }, // Includes own typeName.
supertypeList: { value: [] }, // Linear inheritance hierarchy.
allFields: { value: Object.create(null) }, // Includes inherited fields.
fieldNames: { value: [] }, // Non-hidden keys of allFields.
type: {
value: new Type(function(value, deep) {
return self.check(value, deep);
}, typeName)
}
});
}
Def.fromValue = function(value) {
if (value && typeof value === "object") {
var type = value.type;
if (typeof type === "string" &&
hasOwn.call(defCache, type)) {
var d = defCache[type];
if (d.finalized) {
return d;
}
}
}
return null;
};
var Dp = Def.prototype;
Dp.isSupertypeOf = function(that) {
if (that instanceof Def) {
if (this.finalized !== true ||
that.finalized !== true) {
throw new Error("");
}
return hasOwn.call(that.allSupertypes, this.typeName);
} else {
throw new Error(that + " is not a Def");
}
};
// Note that the list returned by this function is a copy of the internal
// supertypeList, *without* the typeName itself as the first element.
exports.getSupertypeNames = function(typeName) {
if (!hasOwn.call(defCache, typeName)) {
throw new Error("");
}
var d = defCache[typeName];
if (d.finalized !== true) {
throw new Error("");
}
return d.supertypeList.slice(1);
};
// Returns an object mapping from every known type in the defCache to the
// most specific supertype whose name is an own property of the candidates
// object.
exports.computeSupertypeLookupTable = function(candidates) {
var table = {};
var typeNames = Object.keys(defCache);
var typeNameCount = typeNames.length;
for (var i = 0; i < typeNameCount; ++i) {
var typeName = typeNames[i];
var d = defCache[typeName];
if (d.finalized !== true) {
throw new Error("" + typeName);
}
for (var j = 0; j < d.supertypeList.length; ++j) {
var superTypeName = d.supertypeList[j];
if (hasOwn.call(candidates, superTypeName)) {
table[typeName] = superTypeName;
break;
}
}
}
return table;
};
Dp.checkAllFields = function(value, deep) {
var allFields = this.allFields;
if (this.finalized !== true) {
throw new Error("" + this.typeName);
}
function checkFieldByName(name) {
var field = allFields[name];
var type = field.type;
var child = field.getValue(value);
return type.check(child, deep);
}
return isObject.check(value)
&& Object.keys(allFields).every(checkFieldByName);
};
Dp.check = function(value, deep) {
if (this.finalized !== true) {
throw new Error(
"prematurely checking unfinalized type " + this.typeName
);
}
// A Def type can only match an object value.
if (!isObject.check(value))
return false;
var vDef = Def.fromValue(value);
if (!vDef) {
// If we couldn't infer the Def associated with the given value,
// and we expected it to be a SourceLocation or a Position, it was
// probably just missing a "type" field (because Esprima does not
// assign a type property to such nodes). Be optimistic and let
// this.checkAllFields make the final decision.
if (this.typeName === "SourceLocation" ||
this.typeName === "Position") {
return this.checkAllFields(value, deep);
}
// Calling this.checkAllFields for any other type of node is both
// bad for performance and way too forgiving.
return false;
}
// If checking deeply and vDef === this, then we only need to call
// checkAllFields once. Calling checkAllFields is too strict when deep
// is false, because then we only care about this.isSupertypeOf(vDef).
if (deep && vDef === this)
return this.checkAllFields(value, deep);
// 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.
if (!this.isSupertypeOf(vDef))
return false;
// The exception is when deep is true; then, we recursively check all
// fields.
if (!deep)
return true;
// Use the more specific Def (vDef) to perform the deep check, but
// shallow-check fields defined by the less specific Def (this).
return vDef.checkAllFields(value, deep)
&& this.checkAllFields(value, false);
};
Dp.bases = function() {
var args = slice.call(arguments);
var bases = this.baseNames;
if (this.finalized) {
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;
}
args.forEach(function(baseName) {
isString.assert(baseName);
// This indexOf lookup may be O(n), but the typical number of base
// names is very small, and indexOf is a native Array method.
if (bases.indexOf(baseName) < 0)
bases.push(baseName);
});
return this; // For chaining.
};
// False by default until .build(...) is called on an instance.
Object.defineProperty(Dp, "buildable", { value: false });
var builders = {};
exports.builders = builders;
// This object is used as prototype for any node created by a builder.
var nodePrototype = {};
// Call this function to define a new method to be shared by all AST
// nodes. The replaced method (if any) is returned for easy wrapping.
exports.defineMethod = function(name, func) {
var old = nodePrototype[name];
// Pass undefined as func to delete nodePrototype[name].
if (isUndefined.check(func)) {
delete nodePrototype[name];
} else {
isFunction.assert(func);
Object.defineProperty(nodePrototype, name, {
enumerable: true, // For discoverability.
configurable: true, // For delete proto[name].
value: func
});
}
return old;
};
var isArrayOfString = isString.arrayOf();
// Calling the .build method of a Def simultaneously marks the type as
// buildable (by defining builders[getBuilderName(typeName)]) and
// specifies the order of arguments that should be passed to the builder
// function to create an instance of the type.
Dp.build = function(/* param1, param2, ... */) {
var self = this;
var newBuildParams = slice.call(arguments);
isArrayOfString.assert(newBuildParams);
// Calling Def.prototype.build multiple times has the effect of merely
// redefining this property.
Object.defineProperty(self, "buildParams", {
value: newBuildParams,
writable: false,
enumerable: false,
configurable: true
});
if (self.buildable) {
// If this Def is already buildable, update self.buildParams and
// continue using the old builder function.
return self;
}
// Every buildable type will have its "type" field filled in
// automatically. This includes types that are not subtypes of Node,
// like SourceLocation, but that seems harmless (TODO?).
self.field("type", String, function() { return self.typeName });
// Override Dp.buildable for this Def instance.
Object.defineProperty(self, "buildable", { value: true });
Object.defineProperty(builders, getBuilderName(self.typeName), {
enumerable: true,
value: function() {
var args = arguments;
var argc = args.length;
var built = Object.create(nodePrototype);
if (!self.finalized) {
throw new Error(
"attempting to instantiate unfinalized type " +
self.typeName
);
}
function add(param, i) {
if (hasOwn.call(built, param))
return;
var all = self.allFields;
if (!hasOwn.call(all, param)) {
throw new Error("" + param);
}
var field = all[param];
var type = field.type;
var value;
if (isNumber.check(i) && i < argc) {
value = args[i];
} else if (field.defaultFn) {
// Expose the partially-built object to the default
// function as its `this` object.
value = field.defaultFn.call(built);
} else {
var message = "no value or default function given for field " +
JSON.stringify(param) + " of " + self.typeName + "(" +
self.buildParams.map(function(name) {
return all[name];
}).join(", ") + ")";
throw new Error(message);
}
if (!type.check(value)) {
throw new Error(
shallowStringify(value) +
" does not match field " + field +
" of type " + self.typeName
);
}
// TODO Could attach getters and setters here to enforce
// dynamic type safety.
built[param] = value;
}
self.buildParams.forEach(function(param, i) {
add(param, i);
});
Object.keys(self.allFields).forEach(function(param) {
add(param); // Use the default value.
});
// Make sure that the "type" field was filled automatically.
if (built.type !== self.typeName) {
throw new Error("");
}
return built;
}
});
return self; // For chaining.
};
function getBuilderName(typeName) {
return typeName.replace(/^[A-Z]+/, function(upperCasePrefix) {
var len = upperCasePrefix.length;
switch (len) {
case 0: return "";
// If there's only one initial capital letter, just lower-case it.
case 1: return upperCasePrefix.toLowerCase();
default:
// If there's more than one initial capital letter, lower-case
// all but the last one, so that XMLDefaultDeclaration (for
// example) becomes xmlDefaultDeclaration.
return upperCasePrefix.slice(
0, len - 1).toLowerCase() +
upperCasePrefix.charAt(len - 1);
}
});
}
exports.getBuilderName = getBuilderName;
function getStatementBuilderName(typeName) {
typeName = getBuilderName(typeName);
return typeName.replace(/(Expression)?$/, "Statement");
}
exports.getStatementBuilderName = getStatementBuilderName;
// The reason fields are specified using .field(...) instead of an object
// literal syntax is somewhat subtle: the object literal syntax would
// support only one key and one value, but with .field(...) we can pass
// any number of arguments to specify the field.
Dp.field = function(name, type, defaultFn, hidden) {
if (this.finalized) {
console.error("Ignoring attempt to redefine field " +
JSON.stringify(name) + " of finalized type " +
JSON.stringify(this.typeName));
return this;
}
this.ownFields[name] = new Field(name, type, defaultFn, hidden);
return this; // For chaining.
};
var namedTypes = {};
exports.namedTypes = namedTypes;
// Like Object.keys, but aware of what fields each AST type should have.
function getFieldNames(object) {
var d = Def.fromValue(object);
if (d) {
return d.fieldNames.slice(0);
}
if ("type" in object) {
throw new Error(
"did not recognize object of type " +
JSON.stringify(object.type)
);
}
return Object.keys(object);
}
exports.getFieldNames = getFieldNames;
// Get the value of an object property, taking object.type and default
// functions into account.
function getFieldValue(object, fieldName) {
var d = Def.fromValue(object);
if (d) {
var field = d.allFields[fieldName];
if (field) {
return field.getValue(object);
}
}
return object[fieldName];
}
exports.getFieldValue = getFieldValue;
// 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.
exports.eachField = function(object, callback, context) {
getFieldNames(object).forEach(function(name) {
callback.call(this, name, getFieldValue(object, name));
}, context);
};
// Similar to eachField, except that iteration stops as soon as the
// callback returns a truthy value. Like Array.prototype.some, the final
// result is either true or false to indicates whether the callback
// returned true for any element or not.
exports.someField = function(object, callback, context) {
return getFieldNames(object).some(function(name) {
return callback.call(this, name, getFieldValue(object, name));
}, context);
};
// This property will be overridden as true by individual Def instances
// when they are finalized.
Object.defineProperty(Dp, "finalized", { value: false });
Dp.finalize = function() {
var self = this;
// It's not an error to finalize a type more than once, but only the
// first call to .finalize does anything.
if (!self.finalized) {
var allFields = self.allFields;
var allSupertypes = self.allSupertypes;
self.baseNames.forEach(function(name) {
var def = defCache[name];
if (def instanceof Def) {
def.finalize();
extend(allFields, def.allFields);
extend(allSupertypes, def.allSupertypes);
} else {
var message = "unknown supertype name " +
JSON.stringify(name) +
" for subtype " +
JSON.stringify(self.typeName);
throw new Error(message);
}
});
// TODO Warn if fields are overridden with incompatible types.
extend(allFields, self.ownFields);
allSupertypes[self.typeName] = self;
self.fieldNames.length = 0;
for (var fieldName in allFields) {
if (hasOwn.call(allFields, fieldName) &&
!allFields[fieldName].hidden) {
self.fieldNames.push(fieldName);
}
}
// Types are exported only once they have been finalized.
Object.defineProperty(namedTypes, self.typeName, {
enumerable: true,
value: self.type
});
Object.defineProperty(self, "finalized", { value: true });
// A linearization of the inheritance hierarchy.
populateSupertypeList(self.typeName, self.supertypeList);
if (self.buildable && self.supertypeList.lastIndexOf("Expression") >= 0) {
wrapExpressionBuilderWithStatement(self.typeName);
}
}
};
// Adds an additional builder for Expression subtypes
// that wraps the built Expression in an ExpressionStatements.
function wrapExpressionBuilderWithStatement(typeName) {
var wrapperName = getStatementBuilderName(typeName);
// skip if the builder already exists
if (builders[wrapperName]) return;
// the builder function to wrap with builders.ExpressionStatement
var wrapped = builders[getBuilderName(typeName)];
// skip if there is nothing to wrap
if (!wrapped) return;
builders[wrapperName] = function() {
return builders.expressionStatement(wrapped.apply(builders, arguments));
};
}
function populateSupertypeList(typeName, list) {
list.length = 0;
list.push(typeName);
var lastSeen = Object.create(null);
for (var pos = 0; pos < list.length; ++pos) {
typeName = list[pos];
var d = defCache[typeName];
if (d.finalized !== true) {
throw new Error("");
}
// If we saw typeName earlier in the breadth-first traversal,
// delete the last-seen occurrence.
if (hasOwn.call(lastSeen, typeName)) {
delete list[lastSeen[typeName]];
}
// Record the new index of the last-seen occurrence of typeName.
lastSeen[typeName] = pos;
// Enqueue the base names of this type.
list.push.apply(list, d.baseNames);
}
// Compaction loop to remove array holes.
for (var to = 0, from = to, len = list.length; from < len; ++from) {
if (hasOwn.call(list, from)) {
list[to++] = list[from];
}
}
list.length = to;
}
function extend(into, from) {
Object.keys(from).forEach(function(name) {
into[name] = from[name];
});
return into;
};
exports.finalize = function() {
Object.keys(defCache).forEach(function(name) {
defCache[name].finalize();
});
};
/***/ }),
/* 4 */
/***/ (function(module, exports, __webpack_require__) {
var types = __webpack_require__(3);
var Type = types.Type;
var builtin = types.builtInTypes;
var isNumber = builtin.number;
// An example of constructing a new type with arbitrary constraints from
// an existing type.
exports.geq = function(than) {
return new Type(function(value) {
return isNumber.check(value) && value >= than;
}, isNumber + " >= " + than);
};
// Default value-returning functions that may optionally be passed as a
// third argument to Def.prototype.field.
exports.defaults = {
// Functions were used because (among other reasons) that's the most
// elegant way to allow for the emptyArray one always to give a new
// array instance.
"null": function() { return null },
"emptyArray": function() { return [] },
"false": function() { return false },
"true": function() { return true },
"undefined": function() {}
};
var naiveIsPrimitive = Type.or(
builtin.string,
builtin.number,
builtin.boolean,
builtin.null,
builtin.undefined
);
exports.isPrimitive = new Type(function(value) {
if (value === null)
return true;
var type = typeof value;
return !(type === "object" ||
type === "function");
}, naiveIsPrimitive.toString());
/***/ }),
/* 5 */
/***/ (function(module, exports, __webpack_require__) {
var types = __webpack_require__(3);
var n = types.namedTypes;
var b = types.builders;
var isNumber = types.builtInTypes.number;
var isArray = types.builtInTypes.array;
var Path = __webpack_require__(6);
var Scope = __webpack_require__(7);
function NodePath(value, parentPath, name) {
if (!(this instanceof NodePath)) {
throw new Error("NodePath constructor cannot be invoked without 'new'");
}
Path.call(this, value, parentPath, name);
}
var NPp = NodePath.prototype = Object.create(Path.prototype, {
constructor: {
value: NodePath,
enumerable: false,
writable: true,
configurable: true
}
});
Object.defineProperties(NPp, {
node: {
get: function() {
Object.defineProperty(this, "node", {
configurable: true, // Enable deletion.
value: this._computeNode()
});
return this.node;
}
},
parent: {
get: function() {
Object.defineProperty(this, "parent", {
configurable: true, // Enable deletion.
value: this._computeParent()
});
return this.parent;
}
},
scope: {
get: function() {
Object.defineProperty(this, "scope", {
configurable: true, // Enable deletion.
value: this._computeScope()
});
return this.scope;
}
}
});
NPp.replace = function() {
delete this.node;
delete this.parent;
delete this.scope;
return Path.prototype.replace.apply(this, arguments);
};
NPp.prune = function() {
var remainingNodePath = this.parent;
this.replace();
return cleanUpNodesAfterPrune(remainingNodePath);
};
// The value of the first ancestor Path whose value is a Node.
NPp._computeNode = function() {
var value = this.value;
if (n.Node.check(value)) {
return value;
}
var pp = this.parentPath;
return pp && pp.node || null;
};
// The first ancestor Path whose value is a Node distinct from this.node.
NPp._computeParent = function() {
var value = this.value;
var pp = this.parentPath;
if (!n.Node.check(value)) {
while (pp && !n.Node.check(pp.value)) {
pp = pp.parentPath;
}
if (pp) {
pp = pp.parentPath;
}
}
while (pp && !n.Node.check(pp.value)) {
pp = pp.parentPath;
}
return pp || null;
};
// The closest enclosing scope that governs this node.
NPp._computeScope = function() {
var value = this.value;
var pp = this.parentPath;
var scope = pp && pp.scope;
if (n.Node.check(value) &&
Scope.isEstablishedBy(value)) {
scope = new Scope(this, scope);
}
return scope || null;
};
NPp.getValueProperty = function(name) {
return types.getFieldValue(this.value, name);
};
/**
* Determine whether this.node needs to be wrapped in parentheses in order
* for a parser to reproduce the same local AST structure.
*
* For instance, in the expression `(1 + 2) * 3`, the BinaryExpression
* whose operator is "+" needs parentheses, because `1 + 2 * 3` would
* parse differently.
*
* If assumeExpressionContext === true, we don't worry about edge cases
* like an anonymous FunctionExpression appearing lexically first in its
* enclosing statement and thus needing parentheses to avoid being parsed
* as a FunctionDeclaration with a missing name.
*/
NPp.needsParens = function(assumeExpressionContext) {
var pp = this.parentPath;
if (!pp) {
return false;
}
var node = this.value;
// Only expressions need parentheses.
if (!n.Expression.check(node)) {
return false;
}
// Identifiers never need parentheses.
if (node.type === "Identifier") {
return false;
}
while (!n.Node.check(pp.value)) {
pp = pp.parentPath;
if (!pp) {
return false;
}
}
var parent = pp.value;
switch (node.type) {
case "UnaryExpression":
case "SpreadElement":
case "SpreadProperty":
return parent.type === "MemberExpression"
&& this.name === "object"
&& parent.object === node;
case "BinaryExpression":
case "LogicalExpression":
switch (parent.type) {
case "CallExpression":
return this.name === "callee"
&& parent.callee === node;
case "UnaryExpression":
case "SpreadElement":
case "SpreadProperty":
return true;
case "MemberExpression":
return this.name === "object"
&& parent.object === node;
case "BinaryExpression":
case "LogicalExpression":
var po = parent.operator;
var pp = PRECEDENCE[po];
var no = node.operator;
var np = PRECEDENCE[no];
if (pp > np) {
return true;
}
if (pp === np && this.name === "right") {
if (parent.right !== node) {
throw new Error("Nodes must be equal");
}
return true;
}
default:
return false;
}
case "SequenceExpression":
switch (parent.type) {
case "ForStatement":
// Although parentheses wouldn't hurt around sequence
// expressions in the head of for loops, traditional style
// dictates that e.g. i++, j++ should not be wrapped with
// parentheses.
return false;
case "ExpressionStatement":
return this.name !== "expression";
default:
// Otherwise err on the side of overparenthesization, adding
// explicit exceptions above if this proves overzealous.
return true;
}
case "YieldExpression":
switch (parent.type) {
case "BinaryExpression":
case "LogicalExpression":
case "UnaryExpression":
case "SpreadElement":
case "SpreadProperty":
case "CallExpression":
case "MemberExpression":
case "NewExpression":
case "ConditionalExpression":
case "YieldExpression":
return true;
default:
return false;
}
case "Literal":
return parent.type === "MemberExpression"
&& isNumber.check(node.value)
&& this.name === "object"
&& parent.object === node;
case "AssignmentExpression":
case "ConditionalExpression":
switch (parent.type) {
case "UnaryExpression":
case "SpreadElement":
case "SpreadProperty":
case "BinaryExpression":
case "LogicalExpression":
return true;
case "CallExpression":
return this.name === "callee"
&& parent.callee === node;
case "ConditionalExpression":
return this.name === "test"
&& parent.test === node;
case "MemberExpression":
return this.name === "object"
&& parent.object === node;
default:
return false;
}
default:
if (parent.type === "NewExpression" &&
this.name === "callee" &&
parent.callee === node) {
return containsCallExpression(node);
}
}
if (assumeExpressionContext !== true &&
!this.canBeFirstInStatement() &&
this.firstInStatement())
return true;
return false;
};
function isBinary(node) {
return n.BinaryExpression.check(node)
|| n.LogicalExpression.check(node);
}
function isUnaryLike(node) {
return n.UnaryExpression.check(node)
// I considered making SpreadElement and SpreadProperty subtypes
// of UnaryExpression, but they're not really Expression nodes.
|| (n.SpreadElement && n.SpreadElement.check(node))
|| (n.SpreadProperty && n.SpreadProperty.check(node));
}
var PRECEDENCE = {};
[["||"],
["&&"],
["|"],
["^"],
["&"],
["==", "===", "!=", "!=="],
["<", ">", "<=", ">=", "in", "instanceof"],
[">>", "<<", ">>>"],
["+", "-"],
["*", "/", "%"]
].forEach(function(tier, i) {
tier.forEach(function(op) {
PRECEDENCE[op] = i;
});
});
function containsCallExpression(node) {
if (n.CallExpression.check(node)) {
return true;
}
if (isArray.check(node)) {
return node.some(containsCallExpression);
}
if (n.Node.check(node)) {
return types.someField(node, function(name, child) {
return containsCallExpression(child);
});
}
return false;
}
NPp.canBeFirstInStatement = function() {
var node = this.node;
return !n.FunctionExpression.check(node)
&& !n.ObjectExpression.check(node);
};
NPp.firstInStatement = function() {
return firstInStatement(this);
};
function firstInStatement(path) {
for (var node, parent; path.parent; path = path.parent) {
node = path.node;
parent = path.parent.node;
if (n.BlockStatement.check(parent) &&
path.parent.name === "body" &&
path.name === 0) {
if (parent.body[0] !== node) {
throw new Error("Nodes must be equal");
}
return true;
}
if (n.ExpressionStatement.check(parent) &&
path.name === "expression") {
if (parent.expression !== node) {
throw new Error("Nodes must be equal");
}
return true;
}
if (n.SequenceExpression.check(parent) &&
path.parent.name === "expressions" &&
path.name === 0) {
if (parent.expressions[0] !== node) {
throw new Error("Nodes must be equal");
}
continue;
}
if (n.CallExpression.check(parent) &&
path.name === "callee") {
if (parent.callee !== node) {
throw new Error("Nodes must be equal");
}
continue;
}
if (n.MemberExpression.check(parent) &&
path.name === "object") {
if (parent.object !== node) {
throw new Error("Nodes must be equal");
}
continue;
}
if (n.ConditionalExpression.check(parent) &&
path.name === "test") {
if (parent.test !== node) {
throw new Error("Nodes must be equal");
}
continue;
}
if (isBinary(parent) &&
path.name === "left") {
if (parent.left !== node) {
throw new Error("Nodes must be equal");
}
continue;
}
if (n.UnaryExpression.check(parent) &&
!parent.prefix &&
path.name === "argument") {
if (parent.argument !== node) {
throw new Error("Nodes must be equal");
}
continue;
}
return false;
}
return true;
}
/**
* Pruning certain nodes will result in empty or incomplete nodes, here we clean those nodes up.
*/
function cleanUpNodesAfterPrune(remainingNodePath) {
if (n.VariableDeclaration.check(remainingNodePath.node)) {
var declarations = remainingNodePath.get('declarations').value;
if (!declarations || declarations.length === 0) {
return remainingNodePath.prune();
}
} else if (n.ExpressionStatement.check(remainingNodePath.node)) {
if (!remainingNodePath.get('expression').value) {
return remainingNodePath.prune();
}
} else if (n.IfStatement.check(remainingNodePath.node)) {
cleanUpIfStatementAfterPrune(remainingNodePath);
}
return remainingNodePath;
}
function cleanUpIfStatementAfterPrune(ifStatement) {
var testExpression = ifStatement.get('test').value;
var alternate = ifStatement.get('alternate').value;
var consequent = ifStatement.get('consequent').value;
if (!consequent && !alternate) {
var testExpressionStatement = b.expressionStatement(testExpression);
ifStatement.replace(testExpressionStatement);
} else if (!consequent && alternate) {
var negatedTestExpression = b.unaryExpression('!', testExpression, true);
if (n.UnaryExpression.check(testExpression) && testExpression.operator === '!') {
negatedTestExpression = testExpression.argument;
}
ifStatement.get("test").replace(negatedTestExpression);
ifStatement.get("consequent").replace(alternate);
ifStatement.get("alternate").replace();
}
}
module.exports = NodePath;
/***/ }),
/* 6 */
/***/ (function(module, exports, __webpack_require__) {
var Op = Object.prototype;
var hasOwn = Op.hasOwnProperty;
var types = __webpack_require__(3);
var isArray = types.builtInTypes.array;
var isNumber = types.builtInTypes.number;
var Ap = Array.prototype;
var slice = Ap.slice;
var map = Ap.map;
function Path(value, parentPath, name) {
if (!(this instanceof Path)) {
throw new Error("Path constructor cannot be invoked without 'new'");
}
if (parentPath) {
if (!(parentPath instanceof Path)) {
throw new Error("");
}
} else {
parentPath = null;
name = null;
}
// The value encapsulated by this Path, generally equal to
// parentPath.value[name] if we have a parentPath.
this.value = value;
// The immediate parent Path of this Path.
this.parentPath = parentPath;
// The name of the property of parentPath.value through which this
// Path's value was reached.
this.name = name;
// Calling path.get("child") multiple times always returns the same
// child Path object, for both performance and consistency reasons.
this.__childCache = null;
}
var Pp = Path.prototype;
function getChildCache(path) {
// Lazily create the child cache. This also cheapens cache
// invalidation, since you can just reset path.__childCache to null.
return path.__childCache || (path.__childCache = Object.create(null));
}
function getChildPath(path, name) {
var cache = getChildCache(path);
var actualChildValue = path.getValueProperty(name);
var childPath = cache[name];
if (!hasOwn.call(cache, name) ||
// Ensure consistency between cache and reality.
childPath.value !== actualChildValue) {