| 1 | "use strict";
|
|---|
| 2 |
|
|---|
| 3 | Object.defineProperty(exports, "__esModule", {
|
|---|
| 4 | value: true
|
|---|
| 5 | });
|
|---|
| 6 | exports._guessExecutionStatusRelativeTo = _guessExecutionStatusRelativeTo;
|
|---|
| 7 | exports._resolve = _resolve;
|
|---|
| 8 | exports.canHaveVariableDeclarationOrExpression = canHaveVariableDeclarationOrExpression;
|
|---|
| 9 | exports.canSwapBetweenExpressionAndStatement = canSwapBetweenExpressionAndStatement;
|
|---|
| 10 | exports.getSource = getSource;
|
|---|
| 11 | exports.isCompletionRecord = isCompletionRecord;
|
|---|
| 12 | exports.isConstantExpression = isConstantExpression;
|
|---|
| 13 | exports.isInStrictMode = isInStrictMode;
|
|---|
| 14 | exports.isNodeType = isNodeType;
|
|---|
| 15 | exports.isStatementOrBlock = isStatementOrBlock;
|
|---|
| 16 | exports.isStatic = isStatic;
|
|---|
| 17 | exports.matchesPattern = matchesPattern;
|
|---|
| 18 | exports.referencesImport = referencesImport;
|
|---|
| 19 | exports.resolve = resolve;
|
|---|
| 20 | exports.willIMaybeExecuteBefore = willIMaybeExecuteBefore;
|
|---|
| 21 | var _t = require("@babel/types");
|
|---|
| 22 | const {
|
|---|
| 23 | STATEMENT_OR_BLOCK_KEYS,
|
|---|
| 24 | VISITOR_KEYS,
|
|---|
| 25 | isBlockStatement,
|
|---|
| 26 | isExpression,
|
|---|
| 27 | isIdentifier,
|
|---|
| 28 | isLiteral,
|
|---|
| 29 | isStringLiteral,
|
|---|
| 30 | isType,
|
|---|
| 31 | matchesPattern: _matchesPattern
|
|---|
| 32 | } = _t;
|
|---|
| 33 | function matchesPattern(pattern, allowPartial) {
|
|---|
| 34 | return _matchesPattern(this.node, pattern, allowPartial);
|
|---|
| 35 | }
|
|---|
| 36 | exports.has = function has(key) {
|
|---|
| 37 | var _this$node;
|
|---|
| 38 | const val = (_this$node = this.node) == null ? void 0 : _this$node[key];
|
|---|
| 39 | if (val && Array.isArray(val)) {
|
|---|
| 40 | return !!val.length;
|
|---|
| 41 | } else {
|
|---|
| 42 | return !!val;
|
|---|
| 43 | }
|
|---|
| 44 | };
|
|---|
| 45 | function isStatic() {
|
|---|
| 46 | return this.scope.isStatic(this.node);
|
|---|
| 47 | }
|
|---|
| 48 | exports.is = exports.has;
|
|---|
| 49 | exports.isnt = function isnt(key) {
|
|---|
| 50 | return !this.has(key);
|
|---|
| 51 | };
|
|---|
| 52 | exports.equals = function equals(key, value) {
|
|---|
| 53 | return this.node[key] === value;
|
|---|
| 54 | };
|
|---|
| 55 | function isNodeType(type) {
|
|---|
| 56 | return isType(this.type, type);
|
|---|
| 57 | }
|
|---|
| 58 | function canHaveVariableDeclarationOrExpression() {
|
|---|
| 59 | return (this.key === "init" || this.key === "left") && this.parentPath.isFor();
|
|---|
| 60 | }
|
|---|
| 61 | function canSwapBetweenExpressionAndStatement(replacement) {
|
|---|
| 62 | if (this.key !== "body" || !this.parentPath.isArrowFunctionExpression()) {
|
|---|
| 63 | return false;
|
|---|
| 64 | }
|
|---|
| 65 | if (this.isExpression()) {
|
|---|
| 66 | return isBlockStatement(replacement);
|
|---|
| 67 | } else if (this.isBlockStatement()) {
|
|---|
| 68 | return isExpression(replacement);
|
|---|
| 69 | }
|
|---|
| 70 | return false;
|
|---|
| 71 | }
|
|---|
| 72 | function isCompletionRecord(allowInsideFunction) {
|
|---|
| 73 | let path = this;
|
|---|
| 74 | let first = true;
|
|---|
| 75 | do {
|
|---|
| 76 | const {
|
|---|
| 77 | type,
|
|---|
| 78 | container
|
|---|
| 79 | } = path;
|
|---|
| 80 | if (!first && (path.isFunction() || type === "StaticBlock")) {
|
|---|
| 81 | return !!allowInsideFunction;
|
|---|
| 82 | }
|
|---|
| 83 | first = false;
|
|---|
| 84 | if (Array.isArray(container) && path.key !== container.length - 1) {
|
|---|
| 85 | return false;
|
|---|
| 86 | }
|
|---|
| 87 | } while ((path = path.parentPath) && !path.isProgram() && !path.isDoExpression());
|
|---|
| 88 | return true;
|
|---|
| 89 | }
|
|---|
| 90 | function isStatementOrBlock() {
|
|---|
| 91 | if (this.parentPath.isLabeledStatement() || isBlockStatement(this.container)) {
|
|---|
| 92 | return false;
|
|---|
| 93 | } else {
|
|---|
| 94 | return STATEMENT_OR_BLOCK_KEYS.includes(this.key);
|
|---|
| 95 | }
|
|---|
| 96 | }
|
|---|
| 97 | function referencesImport(moduleSource, importName) {
|
|---|
| 98 | if (!this.isReferencedIdentifier()) {
|
|---|
| 99 | if (this.isJSXMemberExpression() && this.node.property.name === importName || (this.isMemberExpression() || this.isOptionalMemberExpression()) && (this.node.computed ? isStringLiteral(this.node.property, {
|
|---|
| 100 | value: importName
|
|---|
| 101 | }) : this.node.property.name === importName)) {
|
|---|
| 102 | const object = this.get("object");
|
|---|
| 103 | return object.isReferencedIdentifier() && object.referencesImport(moduleSource, "*");
|
|---|
| 104 | }
|
|---|
| 105 | return false;
|
|---|
| 106 | }
|
|---|
| 107 | const binding = this.scope.getBinding(this.node.name);
|
|---|
| 108 | if ((binding == null ? void 0 : binding.kind) !== "module") return false;
|
|---|
| 109 | const path = binding.path;
|
|---|
| 110 | const parent = path.parentPath;
|
|---|
| 111 | if (!parent.isImportDeclaration()) return false;
|
|---|
| 112 | if (parent.node.source.value === moduleSource) {
|
|---|
| 113 | if (!importName) return true;
|
|---|
| 114 | } else {
|
|---|
| 115 | return false;
|
|---|
| 116 | }
|
|---|
| 117 | if (path.isImportDefaultSpecifier() && importName === "default") {
|
|---|
| 118 | return true;
|
|---|
| 119 | }
|
|---|
| 120 | if (path.isImportNamespaceSpecifier() && importName === "*") {
|
|---|
| 121 | return true;
|
|---|
| 122 | }
|
|---|
| 123 | if (path.isImportSpecifier() && isIdentifier(path.node.imported, {
|
|---|
| 124 | name: importName
|
|---|
| 125 | })) {
|
|---|
| 126 | return true;
|
|---|
| 127 | }
|
|---|
| 128 | return false;
|
|---|
| 129 | }
|
|---|
| 130 | function getSource() {
|
|---|
| 131 | const node = this.node;
|
|---|
| 132 | if (node.end) {
|
|---|
| 133 | const code = this.hub.getCode();
|
|---|
| 134 | if (code) return code.slice(node.start, node.end);
|
|---|
| 135 | }
|
|---|
| 136 | return "";
|
|---|
| 137 | }
|
|---|
| 138 | function willIMaybeExecuteBefore(target) {
|
|---|
| 139 | return this._guessExecutionStatusRelativeTo(target) !== "after";
|
|---|
| 140 | }
|
|---|
| 141 | function getOuterFunction(path) {
|
|---|
| 142 | return path.isProgram() ? path : (path.parentPath.scope.getFunctionParent() || path.parentPath.scope.getProgramParent()).path;
|
|---|
| 143 | }
|
|---|
| 144 | function isExecutionUncertain(type, key) {
|
|---|
| 145 | switch (type) {
|
|---|
| 146 | case "LogicalExpression":
|
|---|
| 147 | return key === "right";
|
|---|
| 148 | case "ConditionalExpression":
|
|---|
| 149 | case "IfStatement":
|
|---|
| 150 | return key === "consequent" || key === "alternate";
|
|---|
| 151 | case "WhileStatement":
|
|---|
| 152 | case "DoWhileStatement":
|
|---|
| 153 | case "ForInStatement":
|
|---|
| 154 | case "ForOfStatement":
|
|---|
| 155 | return key === "body";
|
|---|
| 156 | case "ForStatement":
|
|---|
| 157 | return key === "body" || key === "update";
|
|---|
| 158 | case "SwitchStatement":
|
|---|
| 159 | return key === "cases";
|
|---|
| 160 | case "TryStatement":
|
|---|
| 161 | return key === "handler";
|
|---|
| 162 | case "AssignmentPattern":
|
|---|
| 163 | return key === "right";
|
|---|
| 164 | case "OptionalMemberExpression":
|
|---|
| 165 | return key === "property";
|
|---|
| 166 | case "OptionalCallExpression":
|
|---|
| 167 | return key === "arguments";
|
|---|
| 168 | default:
|
|---|
| 169 | return false;
|
|---|
| 170 | }
|
|---|
| 171 | }
|
|---|
| 172 | function isExecutionUncertainInList(paths, maxIndex) {
|
|---|
| 173 | for (let i = 0; i < maxIndex; i++) {
|
|---|
| 174 | const path = paths[i];
|
|---|
| 175 | if (isExecutionUncertain(path.parent.type, path.parentKey)) {
|
|---|
| 176 | return true;
|
|---|
| 177 | }
|
|---|
| 178 | }
|
|---|
| 179 | return false;
|
|---|
| 180 | }
|
|---|
| 181 | const SYMBOL_CHECKING = Symbol();
|
|---|
| 182 | function _guessExecutionStatusRelativeTo(target) {
|
|---|
| 183 | return _guessExecutionStatusRelativeToCached(this, target, new Map());
|
|---|
| 184 | }
|
|---|
| 185 | function _guessExecutionStatusRelativeToCached(base, target, cache) {
|
|---|
| 186 | const funcParent = {
|
|---|
| 187 | this: getOuterFunction(base),
|
|---|
| 188 | target: getOuterFunction(target)
|
|---|
| 189 | };
|
|---|
| 190 | if (funcParent.target.node !== funcParent.this.node) {
|
|---|
| 191 | return _guessExecutionStatusRelativeToDifferentFunctionsCached(base, funcParent.target, cache);
|
|---|
| 192 | }
|
|---|
| 193 | const paths = {
|
|---|
| 194 | target: target.getAncestry(),
|
|---|
| 195 | this: base.getAncestry()
|
|---|
| 196 | };
|
|---|
| 197 | if (paths.target.includes(base)) return "after";
|
|---|
| 198 | if (paths.this.includes(target)) return "before";
|
|---|
| 199 | let commonPath;
|
|---|
| 200 | const commonIndex = {
|
|---|
| 201 | target: 0,
|
|---|
| 202 | this: 0
|
|---|
| 203 | };
|
|---|
| 204 | while (!commonPath && commonIndex.this < paths.this.length) {
|
|---|
| 205 | const path = paths.this[commonIndex.this];
|
|---|
| 206 | commonIndex.target = paths.target.indexOf(path);
|
|---|
| 207 | if (commonIndex.target >= 0) {
|
|---|
| 208 | commonPath = path;
|
|---|
| 209 | } else {
|
|---|
| 210 | commonIndex.this++;
|
|---|
| 211 | }
|
|---|
| 212 | }
|
|---|
| 213 | if (!commonPath) {
|
|---|
| 214 | throw new Error("Internal Babel error - The two compared nodes" + " don't appear to belong to the same program.");
|
|---|
| 215 | }
|
|---|
| 216 | if (isExecutionUncertainInList(paths.this, commonIndex.this - 1) || isExecutionUncertainInList(paths.target, commonIndex.target - 1)) {
|
|---|
| 217 | return "unknown";
|
|---|
| 218 | }
|
|---|
| 219 | const divergence = {
|
|---|
| 220 | this: paths.this[commonIndex.this - 1],
|
|---|
| 221 | target: paths.target[commonIndex.target - 1]
|
|---|
| 222 | };
|
|---|
| 223 | if (divergence.target.listKey && divergence.this.listKey && divergence.target.container === divergence.this.container) {
|
|---|
| 224 | return divergence.target.key > divergence.this.key ? "before" : "after";
|
|---|
| 225 | }
|
|---|
| 226 | const keys = VISITOR_KEYS[commonPath.type];
|
|---|
| 227 | const keyPosition = {
|
|---|
| 228 | this: keys.indexOf(divergence.this.parentKey),
|
|---|
| 229 | target: keys.indexOf(divergence.target.parentKey)
|
|---|
| 230 | };
|
|---|
| 231 | return keyPosition.target > keyPosition.this ? "before" : "after";
|
|---|
| 232 | }
|
|---|
| 233 | function _guessExecutionStatusRelativeToDifferentFunctionsInternal(base, target, cache) {
|
|---|
| 234 | if (!target.isFunctionDeclaration()) {
|
|---|
| 235 | if (_guessExecutionStatusRelativeToCached(base, target, cache) === "before") {
|
|---|
| 236 | return "before";
|
|---|
| 237 | }
|
|---|
| 238 | return "unknown";
|
|---|
| 239 | } else if (target.parentPath.isExportDeclaration()) {
|
|---|
| 240 | return "unknown";
|
|---|
| 241 | }
|
|---|
| 242 | const binding = target.scope.getBinding(target.node.id.name);
|
|---|
| 243 | if (!binding.references) return "before";
|
|---|
| 244 | const referencePaths = binding.referencePaths;
|
|---|
| 245 | let allStatus;
|
|---|
| 246 | for (const path of referencePaths) {
|
|---|
| 247 | const childOfFunction = !!path.find(path => path.node === target.node);
|
|---|
| 248 | if (childOfFunction) continue;
|
|---|
| 249 | if (path.key !== "callee" || !path.parentPath.isCallExpression()) {
|
|---|
| 250 | return "unknown";
|
|---|
| 251 | }
|
|---|
| 252 | const status = _guessExecutionStatusRelativeToCached(base, path, cache);
|
|---|
| 253 | if (allStatus && allStatus !== status) {
|
|---|
| 254 | return "unknown";
|
|---|
| 255 | } else {
|
|---|
| 256 | allStatus = status;
|
|---|
| 257 | }
|
|---|
| 258 | }
|
|---|
| 259 | return allStatus;
|
|---|
| 260 | }
|
|---|
| 261 | function _guessExecutionStatusRelativeToDifferentFunctionsCached(base, target, cache) {
|
|---|
| 262 | let nodeMap = cache.get(base.node);
|
|---|
| 263 | let cached;
|
|---|
| 264 | if (!nodeMap) {
|
|---|
| 265 | cache.set(base.node, nodeMap = new Map());
|
|---|
| 266 | } else if (cached = nodeMap.get(target.node)) {
|
|---|
| 267 | if (cached === SYMBOL_CHECKING) {
|
|---|
| 268 | return "unknown";
|
|---|
| 269 | }
|
|---|
| 270 | return cached;
|
|---|
| 271 | }
|
|---|
| 272 | nodeMap.set(target.node, SYMBOL_CHECKING);
|
|---|
| 273 | const result = _guessExecutionStatusRelativeToDifferentFunctionsInternal(base, target, cache);
|
|---|
| 274 | nodeMap.set(target.node, result);
|
|---|
| 275 | return result;
|
|---|
| 276 | }
|
|---|
| 277 | function resolve(dangerous, resolved) {
|
|---|
| 278 | return _resolve.call(this, dangerous, resolved) || this;
|
|---|
| 279 | }
|
|---|
| 280 | function _resolve(dangerous, resolved) {
|
|---|
| 281 | var _resolved;
|
|---|
| 282 | if ((_resolved = resolved) != null && _resolved.includes(this)) return;
|
|---|
| 283 | resolved = resolved || [];
|
|---|
| 284 | resolved.push(this);
|
|---|
| 285 | if (this.isVariableDeclarator()) {
|
|---|
| 286 | if (this.get("id").isIdentifier()) {
|
|---|
| 287 | return this.get("init").resolve(dangerous, resolved);
|
|---|
| 288 | } else {}
|
|---|
| 289 | } else if (this.isReferencedIdentifier()) {
|
|---|
| 290 | const binding = this.scope.getBinding(this.node.name);
|
|---|
| 291 | if (!binding) return;
|
|---|
| 292 | if (!binding.constant) return;
|
|---|
| 293 | if (binding.kind === "module") return;
|
|---|
| 294 | if (binding.path !== this) {
|
|---|
| 295 | const ret = binding.path.resolve(dangerous, resolved);
|
|---|
| 296 | if (this.find(parent => parent.node === ret.node)) return;
|
|---|
| 297 | return ret;
|
|---|
| 298 | }
|
|---|
| 299 | } else if (this.isTypeCastExpression()) {
|
|---|
| 300 | return this.get("expression").resolve(dangerous, resolved);
|
|---|
| 301 | } else if (dangerous && this.isMemberExpression()) {
|
|---|
| 302 | const targetKey = this.toComputedKey();
|
|---|
| 303 | if (!isLiteral(targetKey)) return;
|
|---|
| 304 | const targetName = targetKey.value;
|
|---|
| 305 | const target = this.get("object").resolve(dangerous, resolved);
|
|---|
| 306 | if (target.isObjectExpression()) {
|
|---|
| 307 | const props = target.get("properties");
|
|---|
| 308 | for (const prop of props) {
|
|---|
| 309 | if (!prop.isProperty()) continue;
|
|---|
| 310 | const key = prop.get("key");
|
|---|
| 311 | let match = prop.isnt("computed") && key.isIdentifier({
|
|---|
| 312 | name: targetName
|
|---|
| 313 | });
|
|---|
| 314 | match = match || key.isLiteral({
|
|---|
| 315 | value: targetName
|
|---|
| 316 | });
|
|---|
| 317 | if (match) return prop.get("value").resolve(dangerous, resolved);
|
|---|
| 318 | }
|
|---|
| 319 | } else if (target.isArrayExpression() && !isNaN(+targetName)) {
|
|---|
| 320 | const elems = target.get("elements");
|
|---|
| 321 | const elem = elems[targetName];
|
|---|
| 322 | if (elem) return elem.resolve(dangerous, resolved);
|
|---|
| 323 | }
|
|---|
| 324 | }
|
|---|
| 325 | }
|
|---|
| 326 | function isConstantExpression() {
|
|---|
| 327 | if (this.isIdentifier()) {
|
|---|
| 328 | const binding = this.scope.getBinding(this.node.name);
|
|---|
| 329 | if (!binding) return false;
|
|---|
| 330 | return binding.constant;
|
|---|
| 331 | }
|
|---|
| 332 | if (this.isLiteral()) {
|
|---|
| 333 | if (this.isRegExpLiteral()) {
|
|---|
| 334 | return false;
|
|---|
| 335 | }
|
|---|
| 336 | if (this.isTemplateLiteral()) {
|
|---|
| 337 | return this.get("expressions").every(expression => expression.isConstantExpression());
|
|---|
| 338 | }
|
|---|
| 339 | return true;
|
|---|
| 340 | }
|
|---|
| 341 | if (this.isUnaryExpression()) {
|
|---|
| 342 | if (this.node.operator !== "void") {
|
|---|
| 343 | return false;
|
|---|
| 344 | }
|
|---|
| 345 | return this.get("argument").isConstantExpression();
|
|---|
| 346 | }
|
|---|
| 347 | if (this.isBinaryExpression()) {
|
|---|
| 348 | const {
|
|---|
| 349 | operator
|
|---|
| 350 | } = this.node;
|
|---|
| 351 | return operator !== "in" && operator !== "instanceof" && this.get("left").isConstantExpression() && this.get("right").isConstantExpression();
|
|---|
| 352 | }
|
|---|
| 353 | if (this.isMemberExpression()) {
|
|---|
| 354 | return !this.node.computed && this.get("object").isIdentifier({
|
|---|
| 355 | name: "Symbol"
|
|---|
| 356 | }) && !this.scope.hasBinding("Symbol", {
|
|---|
| 357 | noGlobals: true
|
|---|
| 358 | });
|
|---|
| 359 | }
|
|---|
| 360 | if (this.isCallExpression()) {
|
|---|
| 361 | return this.node.arguments.length === 1 && this.get("callee").matchesPattern("Symbol.for") && !this.scope.hasBinding("Symbol", {
|
|---|
| 362 | noGlobals: true
|
|---|
| 363 | }) && this.get("arguments")[0].isStringLiteral();
|
|---|
| 364 | }
|
|---|
| 365 | return false;
|
|---|
| 366 | }
|
|---|
| 367 | function isInStrictMode() {
|
|---|
| 368 | const start = this.isProgram() ? this : this.parentPath;
|
|---|
| 369 | const strictParent = start.find(path => {
|
|---|
| 370 | if (path.isProgram({
|
|---|
| 371 | sourceType: "module"
|
|---|
| 372 | })) return true;
|
|---|
| 373 | if (path.isClass()) return true;
|
|---|
| 374 | if (path.isArrowFunctionExpression() && !path.get("body").isBlockStatement()) {
|
|---|
| 375 | return false;
|
|---|
| 376 | }
|
|---|
| 377 | let body;
|
|---|
| 378 | if (path.isFunction()) {
|
|---|
| 379 | body = path.node.body;
|
|---|
| 380 | } else if (path.isProgram()) {
|
|---|
| 381 | body = path.node;
|
|---|
| 382 | } else {
|
|---|
| 383 | return false;
|
|---|
| 384 | }
|
|---|
| 385 | for (const directive of body.directives) {
|
|---|
| 386 | if (directive.value.value === "use strict") {
|
|---|
| 387 | return true;
|
|---|
| 388 | }
|
|---|
| 389 | }
|
|---|
| 390 | return false;
|
|---|
| 391 | });
|
|---|
| 392 | return !!strictParent;
|
|---|
| 393 | }
|
|---|
| 394 |
|
|---|
| 395 | //# sourceMappingURL=introspection.js.map
|
|---|