forked from zhurui/management
219 lines
5.5 KiB
JavaScript
219 lines
5.5 KiB
JavaScript
"use strict";
|
|
|
|
Object.defineProperty(exports, "__esModule", {
|
|
value: true
|
|
});
|
|
exports.default = void 0;
|
|
|
|
var virtualTypes = _interopRequireWildcard(require("./lib/virtual-types"));
|
|
|
|
function _debug() {
|
|
const data = _interopRequireDefault(require("debug"));
|
|
|
|
_debug = function () {
|
|
return data;
|
|
};
|
|
|
|
return data;
|
|
}
|
|
|
|
var _index = _interopRequireDefault(require("../index"));
|
|
|
|
var _scope = _interopRequireDefault(require("../scope"));
|
|
|
|
function t() {
|
|
const data = _interopRequireWildcard(require("@babel/types"));
|
|
|
|
t = function () {
|
|
return data;
|
|
};
|
|
|
|
return data;
|
|
}
|
|
|
|
var _cache = require("../cache");
|
|
|
|
function _generator() {
|
|
const data = _interopRequireDefault(require("@babel/generator"));
|
|
|
|
_generator = function () {
|
|
return data;
|
|
};
|
|
|
|
return data;
|
|
}
|
|
|
|
var NodePath_ancestry = _interopRequireWildcard(require("./ancestry"));
|
|
|
|
var NodePath_inference = _interopRequireWildcard(require("./inference"));
|
|
|
|
var NodePath_replacement = _interopRequireWildcard(require("./replacement"));
|
|
|
|
var NodePath_evaluation = _interopRequireWildcard(require("./evaluation"));
|
|
|
|
var NodePath_conversion = _interopRequireWildcard(require("./conversion"));
|
|
|
|
var NodePath_introspection = _interopRequireWildcard(require("./introspection"));
|
|
|
|
var NodePath_context = _interopRequireWildcard(require("./context"));
|
|
|
|
var NodePath_removal = _interopRequireWildcard(require("./removal"));
|
|
|
|
var NodePath_modification = _interopRequireWildcard(require("./modification"));
|
|
|
|
var NodePath_family = _interopRequireWildcard(require("./family"));
|
|
|
|
var NodePath_comments = _interopRequireWildcard(require("./comments"));
|
|
|
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
|
|
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = Object.defineProperty && Object.getOwnPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : {}; if (desc.get || desc.set) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } } newObj.default = obj; return newObj; } }
|
|
|
|
const debug = (0, _debug().default)("babel");
|
|
|
|
class NodePath {
|
|
constructor(hub, parent) {
|
|
this.parent = parent;
|
|
this.hub = hub;
|
|
this.contexts = [];
|
|
this.data = Object.create(null);
|
|
this.shouldSkip = false;
|
|
this.shouldStop = false;
|
|
this.removed = false;
|
|
this.state = null;
|
|
this.opts = null;
|
|
this.skipKeys = null;
|
|
this.parentPath = null;
|
|
this.context = null;
|
|
this.container = null;
|
|
this.listKey = null;
|
|
this.inList = false;
|
|
this.parentKey = null;
|
|
this.key = null;
|
|
this.node = null;
|
|
this.scope = null;
|
|
this.type = null;
|
|
this.typeAnnotation = null;
|
|
}
|
|
|
|
static get({
|
|
hub,
|
|
parentPath,
|
|
parent,
|
|
container,
|
|
listKey,
|
|
key
|
|
}) {
|
|
if (!hub && parentPath) {
|
|
hub = parentPath.hub;
|
|
}
|
|
|
|
if (!parent) {
|
|
throw new Error("To get a node path the parent needs to exist");
|
|
}
|
|
|
|
const targetNode = container[key];
|
|
const paths = _cache.path.get(parent) || [];
|
|
|
|
if (!_cache.path.has(parent)) {
|
|
_cache.path.set(parent, paths);
|
|
}
|
|
|
|
let path;
|
|
|
|
for (let i = 0; i < paths.length; i++) {
|
|
const pathCheck = paths[i];
|
|
|
|
if (pathCheck.node === targetNode) {
|
|
path = pathCheck;
|
|
break;
|
|
}
|
|
}
|
|
|
|
if (!path) {
|
|
path = new NodePath(hub, parent);
|
|
paths.push(path);
|
|
}
|
|
|
|
path.setup(parentPath, container, listKey, key);
|
|
return path;
|
|
}
|
|
|
|
getScope(scope) {
|
|
return this.isScope() ? new _scope.default(this) : scope;
|
|
}
|
|
|
|
setData(key, val) {
|
|
return this.data[key] = val;
|
|
}
|
|
|
|
getData(key, def) {
|
|
let val = this.data[key];
|
|
if (val === undefined && def !== undefined) val = this.data[key] = def;
|
|
return val;
|
|
}
|
|
|
|
buildCodeFrameError(msg, Error = SyntaxError) {
|
|
return this.hub.buildError(this.node, msg, Error);
|
|
}
|
|
|
|
traverse(visitor, state) {
|
|
(0, _index.default)(this.node, visitor, this.scope, state, this);
|
|
}
|
|
|
|
set(key, node) {
|
|
t().validate(this.node, key, node);
|
|
this.node[key] = node;
|
|
}
|
|
|
|
getPathLocation() {
|
|
const parts = [];
|
|
let path = this;
|
|
|
|
do {
|
|
let key = path.key;
|
|
if (path.inList) key = `${path.listKey}[${key}]`;
|
|
parts.unshift(key);
|
|
} while (path = path.parentPath);
|
|
|
|
return parts.join(".");
|
|
}
|
|
|
|
debug(message) {
|
|
if (!debug.enabled) return;
|
|
debug(`${this.getPathLocation()} ${this.type}: ${message}`);
|
|
}
|
|
|
|
toString() {
|
|
return (0, _generator().default)(this.node).code;
|
|
}
|
|
|
|
}
|
|
|
|
exports.default = NodePath;
|
|
Object.assign(NodePath.prototype, NodePath_ancestry, NodePath_inference, NodePath_replacement, NodePath_evaluation, NodePath_conversion, NodePath_introspection, NodePath_context, NodePath_removal, NodePath_modification, NodePath_family, NodePath_comments);
|
|
|
|
for (const type of t().TYPES) {
|
|
const typeKey = `is${type}`;
|
|
const fn = t()[typeKey];
|
|
|
|
NodePath.prototype[typeKey] = function (opts) {
|
|
return fn(this.node, opts);
|
|
};
|
|
|
|
NodePath.prototype[`assert${type}`] = function (opts) {
|
|
if (!fn(this.node, opts)) {
|
|
throw new TypeError(`Expected node path of type ${type}`);
|
|
}
|
|
};
|
|
}
|
|
|
|
for (const type of Object.keys(virtualTypes)) {
|
|
if (type[0] === "_") continue;
|
|
if (t().TYPES.indexOf(type) < 0) t().TYPES.push(type);
|
|
const virtualType = virtualTypes[type];
|
|
|
|
NodePath.prototype[`is${type}`] = function (opts) {
|
|
return virtualType.checkPath(this, opts);
|
|
};
|
|
} |