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