| 1 | "use strict";
|
|---|
| 2 |
|
|---|
| 3 | Object.defineProperty(exports, "__esModule", {
|
|---|
| 4 | value: true
|
|---|
| 5 | });
|
|---|
| 6 | exports.default = exports.SHOULD_STOP = exports.SHOULD_SKIP = exports.REMOVED = void 0;
|
|---|
| 7 | var virtualTypes = require("./lib/virtual-types.js");
|
|---|
| 8 | var _debug = require("debug");
|
|---|
| 9 | var _index = require("../index.js");
|
|---|
| 10 | var _index2 = require("../scope/index.js");
|
|---|
| 11 | var _t = require("@babel/types");
|
|---|
| 12 | var t = _t;
|
|---|
| 13 | var cache = require("../cache.js");
|
|---|
| 14 | var _generator = require("@babel/generator");
|
|---|
| 15 | var NodePath_ancestry = require("./ancestry.js");
|
|---|
| 16 | var NodePath_inference = require("./inference/index.js");
|
|---|
| 17 | var NodePath_replacement = require("./replacement.js");
|
|---|
| 18 | var NodePath_evaluation = require("./evaluation.js");
|
|---|
| 19 | var NodePath_conversion = require("./conversion.js");
|
|---|
| 20 | var NodePath_introspection = require("./introspection.js");
|
|---|
| 21 | var _context = require("./context.js");
|
|---|
| 22 | var NodePath_context = _context;
|
|---|
| 23 | var NodePath_removal = require("./removal.js");
|
|---|
| 24 | var NodePath_modification = require("./modification.js");
|
|---|
| 25 | var NodePath_family = require("./family.js");
|
|---|
| 26 | var NodePath_comments = require("./comments.js");
|
|---|
| 27 | var NodePath_virtual_types_validator = require("./lib/virtual-types-validator.js");
|
|---|
| 28 | const {
|
|---|
| 29 | validate
|
|---|
| 30 | } = _t;
|
|---|
| 31 | const debug = _debug("babel");
|
|---|
| 32 | const REMOVED = exports.REMOVED = 1 << 0;
|
|---|
| 33 | const SHOULD_STOP = exports.SHOULD_STOP = 1 << 1;
|
|---|
| 34 | const SHOULD_SKIP = exports.SHOULD_SKIP = 1 << 2;
|
|---|
| 35 | const NodePath_Final = exports.default = class NodePath {
|
|---|
| 36 | constructor(hub, parent) {
|
|---|
| 37 | this.contexts = [];
|
|---|
| 38 | this.state = null;
|
|---|
| 39 | this._traverseFlags = 0;
|
|---|
| 40 | this.skipKeys = null;
|
|---|
| 41 | this.parentPath = null;
|
|---|
| 42 | this.container = null;
|
|---|
| 43 | this.listKey = null;
|
|---|
| 44 | this.key = null;
|
|---|
| 45 | this.node = null;
|
|---|
| 46 | this.type = null;
|
|---|
| 47 | this._store = null;
|
|---|
| 48 | this.parent = parent;
|
|---|
| 49 | this.hub = hub;
|
|---|
| 50 | this.data = null;
|
|---|
| 51 | this.context = null;
|
|---|
| 52 | this.scope = null;
|
|---|
| 53 | }
|
|---|
| 54 | get removed() {
|
|---|
| 55 | return (this._traverseFlags & 1) > 0;
|
|---|
| 56 | }
|
|---|
| 57 | set removed(v) {
|
|---|
| 58 | if (v) this._traverseFlags |= 1;else this._traverseFlags &= -2;
|
|---|
| 59 | }
|
|---|
| 60 | get shouldStop() {
|
|---|
| 61 | return (this._traverseFlags & 2) > 0;
|
|---|
| 62 | }
|
|---|
| 63 | set shouldStop(v) {
|
|---|
| 64 | if (v) this._traverseFlags |= 2;else this._traverseFlags &= -3;
|
|---|
| 65 | }
|
|---|
| 66 | get shouldSkip() {
|
|---|
| 67 | return (this._traverseFlags & 4) > 0;
|
|---|
| 68 | }
|
|---|
| 69 | set shouldSkip(v) {
|
|---|
| 70 | if (v) this._traverseFlags |= 4;else this._traverseFlags &= -5;
|
|---|
| 71 | }
|
|---|
| 72 | static get({
|
|---|
| 73 | hub,
|
|---|
| 74 | parentPath,
|
|---|
| 75 | parent,
|
|---|
| 76 | container,
|
|---|
| 77 | listKey,
|
|---|
| 78 | key
|
|---|
| 79 | }) {
|
|---|
| 80 | if (!hub && parentPath) {
|
|---|
| 81 | hub = parentPath.hub;
|
|---|
| 82 | }
|
|---|
| 83 | if (!parent) {
|
|---|
| 84 | throw new Error("To get a node path the parent needs to exist");
|
|---|
| 85 | }
|
|---|
| 86 | const targetNode = container[key];
|
|---|
| 87 | const paths = cache.getOrCreateCachedPaths(parent, parentPath);
|
|---|
| 88 | let path = paths.get(targetNode);
|
|---|
| 89 | if (!path) {
|
|---|
| 90 | path = new NodePath(hub, parent);
|
|---|
| 91 | if (targetNode) paths.set(targetNode, path);
|
|---|
| 92 | }
|
|---|
| 93 | _context.setup.call(path, parentPath, container, listKey, key);
|
|---|
| 94 | return path;
|
|---|
| 95 | }
|
|---|
| 96 | getScope(scope) {
|
|---|
| 97 | return this.isScope() ? new _index2.default(this) : scope;
|
|---|
| 98 | }
|
|---|
| 99 | setData(key, val) {
|
|---|
| 100 | if (this.data == null) {
|
|---|
| 101 | this.data = Object.create(null);
|
|---|
| 102 | }
|
|---|
| 103 | return this.data[key] = val;
|
|---|
| 104 | }
|
|---|
| 105 | getData(key, def) {
|
|---|
| 106 | if (this.data == null) {
|
|---|
| 107 | this.data = Object.create(null);
|
|---|
| 108 | }
|
|---|
| 109 | let val = this.data[key];
|
|---|
| 110 | if (val === undefined && def !== undefined) val = this.data[key] = def;
|
|---|
| 111 | return val;
|
|---|
| 112 | }
|
|---|
| 113 | hasNode() {
|
|---|
| 114 | return this.node != null;
|
|---|
| 115 | }
|
|---|
| 116 | buildCodeFrameError(msg, Error = SyntaxError) {
|
|---|
| 117 | return this.hub.buildError(this.node, msg, Error);
|
|---|
| 118 | }
|
|---|
| 119 | traverse(visitor, state) {
|
|---|
| 120 | (0, _index.default)(this.node, visitor, this.scope, state, this);
|
|---|
| 121 | }
|
|---|
| 122 | set(key, node) {
|
|---|
| 123 | validate(this.node, key, node);
|
|---|
| 124 | this.node[key] = node;
|
|---|
| 125 | }
|
|---|
| 126 | getPathLocation() {
|
|---|
| 127 | const parts = [];
|
|---|
| 128 | let path = this;
|
|---|
| 129 | do {
|
|---|
| 130 | let key = path.key;
|
|---|
| 131 | if (path.inList) key = `${path.listKey}[${key}]`;
|
|---|
| 132 | parts.unshift(key);
|
|---|
| 133 | } while (path = path.parentPath);
|
|---|
| 134 | return parts.join(".");
|
|---|
| 135 | }
|
|---|
| 136 | debug(message) {
|
|---|
| 137 | if (!debug.enabled) return;
|
|---|
| 138 | debug(`${this.getPathLocation()} ${this.type}: ${message}`);
|
|---|
| 139 | }
|
|---|
| 140 | toString() {
|
|---|
| 141 | return (0, _generator.default)(this.node).code;
|
|---|
| 142 | }
|
|---|
| 143 | get inList() {
|
|---|
| 144 | return !!this.listKey;
|
|---|
| 145 | }
|
|---|
| 146 | set inList(inList) {
|
|---|
| 147 | if (!inList) {
|
|---|
| 148 | this.listKey = null;
|
|---|
| 149 | }
|
|---|
| 150 | }
|
|---|
| 151 | get parentKey() {
|
|---|
| 152 | return this.listKey || this.key;
|
|---|
| 153 | }
|
|---|
| 154 | };
|
|---|
| 155 | const methods = {
|
|---|
| 156 | findParent: NodePath_ancestry.findParent,
|
|---|
| 157 | find: NodePath_ancestry.find,
|
|---|
| 158 | getFunctionParent: NodePath_ancestry.getFunctionParent,
|
|---|
| 159 | getStatementParent: NodePath_ancestry.getStatementParent,
|
|---|
| 160 | getEarliestCommonAncestorFrom: NodePath_ancestry.getEarliestCommonAncestorFrom,
|
|---|
| 161 | getDeepestCommonAncestorFrom: NodePath_ancestry.getDeepestCommonAncestorFrom,
|
|---|
| 162 | getAncestry: NodePath_ancestry.getAncestry,
|
|---|
| 163 | isAncestor: NodePath_ancestry.isAncestor,
|
|---|
| 164 | isDescendant: NodePath_ancestry.isDescendant,
|
|---|
| 165 | inType: NodePath_ancestry.inType,
|
|---|
| 166 | getTypeAnnotation: NodePath_inference.getTypeAnnotation,
|
|---|
| 167 | isBaseType: NodePath_inference.isBaseType,
|
|---|
| 168 | couldBeBaseType: NodePath_inference.couldBeBaseType,
|
|---|
| 169 | baseTypeStrictlyMatches: NodePath_inference.baseTypeStrictlyMatches,
|
|---|
| 170 | isGenericType: NodePath_inference.isGenericType,
|
|---|
| 171 | replaceWithMultiple: NodePath_replacement.replaceWithMultiple,
|
|---|
| 172 | replaceWithSourceString: NodePath_replacement.replaceWithSourceString,
|
|---|
| 173 | replaceWith: NodePath_replacement.replaceWith,
|
|---|
| 174 | replaceExpressionWithStatements: NodePath_replacement.replaceExpressionWithStatements,
|
|---|
| 175 | replaceInline: NodePath_replacement.replaceInline,
|
|---|
| 176 | evaluateTruthy: NodePath_evaluation.evaluateTruthy,
|
|---|
| 177 | evaluate: NodePath_evaluation.evaluate,
|
|---|
| 178 | toComputedKey: NodePath_conversion.toComputedKey,
|
|---|
| 179 | ensureBlock: NodePath_conversion.ensureBlock,
|
|---|
| 180 | unwrapFunctionEnvironment: NodePath_conversion.unwrapFunctionEnvironment,
|
|---|
| 181 | arrowFunctionToExpression: NodePath_conversion.arrowFunctionToExpression,
|
|---|
| 182 | splitExportDeclaration: NodePath_conversion.splitExportDeclaration,
|
|---|
| 183 | ensureFunctionName: NodePath_conversion.ensureFunctionName,
|
|---|
| 184 | matchesPattern: NodePath_introspection.matchesPattern,
|
|---|
| 185 | isStatic: NodePath_introspection.isStatic,
|
|---|
| 186 | isNodeType: NodePath_introspection.isNodeType,
|
|---|
| 187 | canHaveVariableDeclarationOrExpression: NodePath_introspection.canHaveVariableDeclarationOrExpression,
|
|---|
| 188 | canSwapBetweenExpressionAndStatement: NodePath_introspection.canSwapBetweenExpressionAndStatement,
|
|---|
| 189 | isCompletionRecord: NodePath_introspection.isCompletionRecord,
|
|---|
| 190 | isStatementOrBlock: NodePath_introspection.isStatementOrBlock,
|
|---|
| 191 | referencesImport: NodePath_introspection.referencesImport,
|
|---|
| 192 | getSource: NodePath_introspection.getSource,
|
|---|
| 193 | willIMaybeExecuteBefore: NodePath_introspection.willIMaybeExecuteBefore,
|
|---|
| 194 | _guessExecutionStatusRelativeTo: NodePath_introspection._guessExecutionStatusRelativeTo,
|
|---|
| 195 | resolve: NodePath_introspection.resolve,
|
|---|
| 196 | isConstantExpression: NodePath_introspection.isConstantExpression,
|
|---|
| 197 | isInStrictMode: NodePath_introspection.isInStrictMode,
|
|---|
| 198 | isDenylisted: NodePath_context.isDenylisted,
|
|---|
| 199 | visit: NodePath_context.visit,
|
|---|
| 200 | skip: NodePath_context.skip,
|
|---|
| 201 | skipKey: NodePath_context.skipKey,
|
|---|
| 202 | stop: NodePath_context.stop,
|
|---|
| 203 | setContext: NodePath_context.setContext,
|
|---|
| 204 | requeue: NodePath_context.requeue,
|
|---|
| 205 | requeueComputedKeyAndDecorators: NodePath_context.requeueComputedKeyAndDecorators,
|
|---|
| 206 | remove: NodePath_removal.remove,
|
|---|
| 207 | insertBefore: NodePath_modification.insertBefore,
|
|---|
| 208 | insertAfter: NodePath_modification.insertAfter,
|
|---|
| 209 | unshiftContainer: NodePath_modification.unshiftContainer,
|
|---|
| 210 | pushContainer: NodePath_modification.pushContainer,
|
|---|
| 211 | getOpposite: NodePath_family.getOpposite,
|
|---|
| 212 | getCompletionRecords: NodePath_family.getCompletionRecords,
|
|---|
| 213 | getSibling: NodePath_family.getSibling,
|
|---|
| 214 | getPrevSibling: NodePath_family.getPrevSibling,
|
|---|
| 215 | getNextSibling: NodePath_family.getNextSibling,
|
|---|
| 216 | getAllNextSiblings: NodePath_family.getAllNextSiblings,
|
|---|
| 217 | getAllPrevSiblings: NodePath_family.getAllPrevSiblings,
|
|---|
| 218 | get: NodePath_family.get,
|
|---|
| 219 | getAssignmentIdentifiers: NodePath_family.getAssignmentIdentifiers,
|
|---|
| 220 | getBindingIdentifiers: NodePath_family.getBindingIdentifiers,
|
|---|
| 221 | getOuterBindingIdentifiers: NodePath_family.getOuterBindingIdentifiers,
|
|---|
| 222 | getBindingIdentifierPaths: NodePath_family.getBindingIdentifierPaths,
|
|---|
| 223 | getOuterBindingIdentifierPaths: NodePath_family.getOuterBindingIdentifierPaths,
|
|---|
| 224 | shareCommentsWithSiblings: NodePath_comments.shareCommentsWithSiblings,
|
|---|
| 225 | addComment: NodePath_comments.addComment,
|
|---|
| 226 | addComments: NodePath_comments.addComments
|
|---|
| 227 | };
|
|---|
| 228 | Object.assign(NodePath_Final.prototype, methods);
|
|---|
| 229 | NodePath_Final.prototype.arrowFunctionToShadowed = NodePath_conversion[String("arrowFunctionToShadowed")];
|
|---|
| 230 | Object.assign(NodePath_Final.prototype, {
|
|---|
| 231 | has: NodePath_introspection[String("has")],
|
|---|
| 232 | is: NodePath_introspection[String("is")],
|
|---|
| 233 | isnt: NodePath_introspection[String("isnt")],
|
|---|
| 234 | equals: NodePath_introspection[String("equals")],
|
|---|
| 235 | hoist: NodePath_modification[String("hoist")],
|
|---|
| 236 | updateSiblingKeys: NodePath_modification.updateSiblingKeys,
|
|---|
| 237 | call: NodePath_context.call,
|
|---|
| 238 | isBlacklisted: NodePath_context[String("isBlacklisted")],
|
|---|
| 239 | setScope: NodePath_context.setScope,
|
|---|
| 240 | resync: NodePath_context.resync,
|
|---|
| 241 | popContext: NodePath_context.popContext,
|
|---|
| 242 | pushContext: NodePath_context.pushContext,
|
|---|
| 243 | setup: NodePath_context.setup,
|
|---|
| 244 | setKey: NodePath_context.setKey
|
|---|
| 245 | });
|
|---|
| 246 | NodePath_Final.prototype._guessExecutionStatusRelativeToDifferentFunctions = NodePath_introspection._guessExecutionStatusRelativeTo;
|
|---|
| 247 | NodePath_Final.prototype._guessExecutionStatusRelativeToDifferentFunctions = NodePath_introspection._guessExecutionStatusRelativeTo;
|
|---|
| 248 | Object.assign(NodePath_Final.prototype, {
|
|---|
| 249 | _getTypeAnnotation: NodePath_inference._getTypeAnnotation,
|
|---|
| 250 | _replaceWith: NodePath_replacement._replaceWith,
|
|---|
| 251 | _resolve: NodePath_introspection._resolve,
|
|---|
| 252 | _call: NodePath_context._call,
|
|---|
| 253 | _resyncParent: NodePath_context._resyncParent,
|
|---|
| 254 | _resyncKey: NodePath_context._resyncKey,
|
|---|
| 255 | _resyncList: NodePath_context._resyncList,
|
|---|
| 256 | _resyncRemoved: NodePath_context._resyncRemoved,
|
|---|
| 257 | _getQueueContexts: NodePath_context._getQueueContexts,
|
|---|
| 258 | _removeFromScope: NodePath_removal._removeFromScope,
|
|---|
| 259 | _callRemovalHooks: NodePath_removal._callRemovalHooks,
|
|---|
| 260 | _remove: NodePath_removal._remove,
|
|---|
| 261 | _markRemoved: NodePath_removal._markRemoved,
|
|---|
| 262 | _assertUnremoved: NodePath_removal._assertUnremoved,
|
|---|
| 263 | _containerInsert: NodePath_modification._containerInsert,
|
|---|
| 264 | _containerInsertBefore: NodePath_modification._containerInsertBefore,
|
|---|
| 265 | _containerInsertAfter: NodePath_modification._containerInsertAfter,
|
|---|
| 266 | _verifyNodeList: NodePath_modification._verifyNodeList,
|
|---|
| 267 | _getKey: NodePath_family._getKey,
|
|---|
| 268 | _getPattern: NodePath_family._getPattern
|
|---|
| 269 | });
|
|---|
| 270 | for (const type of t.TYPES) {
|
|---|
| 271 | const typeKey = `is${type}`;
|
|---|
| 272 | const fn = t[typeKey];
|
|---|
| 273 | NodePath_Final.prototype[typeKey] = function (opts) {
|
|---|
| 274 | return fn(this.node, opts);
|
|---|
| 275 | };
|
|---|
| 276 | NodePath_Final.prototype[`assert${type}`] = function (opts) {
|
|---|
| 277 | if (!fn(this.node, opts)) {
|
|---|
| 278 | throw new TypeError(`Expected node path of type ${type}`);
|
|---|
| 279 | }
|
|---|
| 280 | };
|
|---|
| 281 | }
|
|---|
| 282 | Object.assign(NodePath_Final.prototype, NodePath_virtual_types_validator);
|
|---|
| 283 | for (const type of Object.keys(virtualTypes)) {
|
|---|
| 284 | if (type.startsWith("_")) continue;
|
|---|
| 285 | if (!t.TYPES.includes(type)) t.TYPES.push(type);
|
|---|
| 286 | }
|
|---|
| 287 |
|
|---|
| 288 | //# sourceMappingURL=index.js.map
|
|---|