1 line
No EOL
10 KiB
Text
Executable file
1 line
No EOL
10 KiB
Text
Executable file
{"version":3,"names":["inferers","require","_t","anyTypeAnnotation","isAnyTypeAnnotation","isArrayTypeAnnotation","isBooleanTypeAnnotation","isEmptyTypeAnnotation","isFlowBaseAnnotation","isGenericTypeAnnotation","isIdentifier","isMixedTypeAnnotation","isNumberTypeAnnotation","isStringTypeAnnotation","isTSArrayType","isTSTypeAnnotation","isTSTypeReference","isTupleTypeAnnotation","isTypeAnnotation","isUnionTypeAnnotation","isVoidTypeAnnotation","stringTypeAnnotation","voidTypeAnnotation","getTypeAnnotation","type","getData","_getTypeAnnotation","typeAnnotation","setData","typeAnnotationInferringNodes","WeakSet","node","key","parentPath","isVariableDeclarator","declar","declarParent","isForInStatement","isForOfStatement","has","add","_inferer","inferer","call","validParent","delete","isBaseType","baseName","soft","_isBaseType","Error","couldBeBaseType","name","type2","types","baseTypeStrictlyMatches","rightArg","left","right","isGenericType","genericName","id","typeName"],"sources":["../../../src/path/inference/index.ts"],"sourcesContent":["import type NodePath from \"../index.ts\";\nimport * as inferers from \"./inferers.ts\";\nimport {\n anyTypeAnnotation,\n isAnyTypeAnnotation,\n isArrayTypeAnnotation,\n isBooleanTypeAnnotation,\n isEmptyTypeAnnotation,\n isFlowBaseAnnotation,\n isGenericTypeAnnotation,\n isIdentifier,\n isMixedTypeAnnotation,\n isNumberTypeAnnotation,\n isStringTypeAnnotation,\n isTSArrayType,\n isTSTypeAnnotation,\n isTSTypeReference,\n isTupleTypeAnnotation,\n isTypeAnnotation,\n isUnionTypeAnnotation,\n isVoidTypeAnnotation,\n stringTypeAnnotation,\n voidTypeAnnotation,\n} from \"@babel/types\";\nimport type * as t from \"@babel/types\";\n\n/**\n * Infer the type of the current `NodePath`.\n */\n\nexport function getTypeAnnotation(this: NodePath): t.FlowType | t.TSType {\n let type = this.getData(\"typeAnnotation\");\n if (type != null) {\n return type;\n }\n type = this._getTypeAnnotation() || anyTypeAnnotation();\n if (isTypeAnnotation(type) || isTSTypeAnnotation(type)) {\n type = type.typeAnnotation;\n }\n this.setData(\"typeAnnotation\", type);\n return type;\n}\n\n// Used to avoid infinite recursion in cases like\n// var b, c; if (0) { c = 1; b = c; } c = b;\n// It also works with indirect recursion.\nconst typeAnnotationInferringNodes = new WeakSet();\n\n/**\n * todo: split up this method\n */\n\nexport function _getTypeAnnotation(this: NodePath): any {\n const node = this.node;\n\n if (!node) {\n // handle initializerless variables, add in checks for loop initializers too\n if (this.key === \"init\" && this.parentPath.isVariableDeclarator()) {\n const declar = this.parentPath.parentPath;\n const declarParent = declar.parentPath;\n\n // for (let NODE in bar) {}\n if (declar.key === \"left\" && declarParent.isForInStatement()) {\n return stringTypeAnnotation();\n }\n\n // for (let NODE of bar) {}\n if (declar.key === \"left\" && declarParent.isForOfStatement()) {\n return anyTypeAnnotation();\n }\n\n return voidTypeAnnotation();\n } else {\n return;\n }\n }\n\n // @ts-expect-error typeAnnotation may not index node\n if (node.typeAnnotation) {\n // @ts-expect-error typeAnnotation may not index node\n return node.typeAnnotation;\n }\n\n if (typeAnnotationInferringNodes.has(node)) {\n // Bail out from type inference to avoid infinite loops\n return;\n }\n typeAnnotationInferringNodes.add(node);\n\n try {\n let inferer =\n // @ts-expect-error inferers do not cover all AST types\n inferers[node.type];\n if (inferer) {\n return inferer.call(this, node);\n }\n\n // @ts-expect-error inferers do not cover all AST types\n inferer = inferers[this.parentPath.type];\n if (inferer?.validParent) {\n return this.parentPath.getTypeAnnotation();\n }\n } finally {\n typeAnnotationInferringNodes.delete(node);\n }\n}\n\nexport function isBaseType(\n this: NodePath,\n baseName: string,\n soft?: boolean,\n): boolean {\n return _isBaseType(baseName, this.getTypeAnnotation(), soft);\n}\n\nfunction _isBaseType(\n baseName: string,\n type?: t.FlowType | t.TSType,\n soft?: boolean,\n): boolean {\n if (baseName === \"string\") {\n return isStringTypeAnnotation(type);\n } else if (baseName === \"number\") {\n return isNumberTypeAnnotation(type);\n } else if (baseName === \"boolean\") {\n return isBooleanTypeAnnotation(type);\n } else if (baseName === \"any\") {\n return isAnyTypeAnnotation(type);\n } else if (baseName === \"mixed\") {\n return isMixedTypeAnnotation(type);\n } else if (baseName === \"empty\") {\n return isEmptyTypeAnnotation(type);\n } else if (baseName === \"void\") {\n return isVoidTypeAnnotation(type);\n } else {\n if (soft) {\n return false;\n } else {\n throw new Error(`Unknown base type ${baseName}`);\n }\n }\n}\n\nexport function couldBeBaseType(this: NodePath, name: string): boolean {\n const type = this.getTypeAnnotation();\n if (isAnyTypeAnnotation(type)) return true;\n\n if (isUnionTypeAnnotation(type)) {\n for (const type2 of type.types) {\n if (isAnyTypeAnnotation(type2) || _isBaseType(name, type2, true)) {\n return true;\n }\n }\n return false;\n } else {\n return _isBaseType(name, type, true);\n }\n}\n\nexport function baseTypeStrictlyMatches(\n this: NodePath,\n rightArg: NodePath,\n): boolean {\n const left = this.getTypeAnnotation();\n const right = rightArg.getTypeAnnotation();\n\n if (!isAnyTypeAnnotation(left) && isFlowBaseAnnotation(left)) {\n return right.type === left.type;\n }\n return false;\n}\n\nexport function isGenericType(this: NodePath, genericName: string): boolean {\n const type = this.getTypeAnnotation();\n if (genericName === \"Array\") {\n // T[]\n if (\n isTSArrayType(type) ||\n isArrayTypeAnnotation(type) ||\n isTupleTypeAnnotation(type)\n ) {\n return true;\n }\n }\n return (\n (isGenericTypeAnnotation(type) &&\n isIdentifier(type.id, {\n name: genericName,\n })) ||\n (isTSTypeReference(type) &&\n isIdentifier(type.typeName, {\n name: genericName,\n }))\n );\n}\n"],"mappings":";;;;;;;;;;;AACA,IAAAA,QAAA,GAAAC,OAAA;AACA,IAAAC,EAAA,GAAAD,OAAA;AAqBsB;EApBpBE,iBAAiB;EACjBC,mBAAmB;EACnBC,qBAAqB;EACrBC,uBAAuB;EACvBC,qBAAqB;EACrBC,oBAAoB;EACpBC,uBAAuB;EACvBC,YAAY;EACZC,qBAAqB;EACrBC,sBAAsB;EACtBC,sBAAsB;EACtBC,aAAa;EACbC,kBAAk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