challenge-algorithms-v2.0-s.../node_modules/@babel/traverse/lib/path/inference/inferer-reference.js.map

1 line
13 KiB
Plaintext

{"version":3,"names":["BOOLEAN_NUMBER_BINARY_OPERATORS","createTypeAnnotationBasedOnTypeof","numberTypeAnnotation","voidTypeAnnotation","node","isReferenced","binding","scope","getBinding","name","identifier","typeAnnotation","getTypeAnnotationBindingConstantViolations","path","types","functionConstantViolations","constantViolations","getConstantViolationsBefore","testType","getConditionalAnnotation","testConstantViolations","ifStatement","filter","indexOf","push","length","violation","getTypeAnnotation","createUnionType","functions","violations","slice","unshift","resolve","status","_guessExecutionStatusRelativeTo","inferAnnotationFromBinaryExpression","operator","right","get","left","target","isIdentifier","typeofPath","typePath","isUnaryExpression","isLiteral","typeValue","value","getParentConditionalPath","parentPath","isIfStatement","isConditionalExpression","key","isFunction","test","paths","i","isLogicalExpression","isBinaryExpression","type"],"sources":["../../../src/path/inference/inferer-reference.ts"],"sourcesContent":["import type NodePath from \"../index\";\nimport {\n BOOLEAN_NUMBER_BINARY_OPERATORS,\n createTypeAnnotationBasedOnTypeof,\n numberTypeAnnotation,\n voidTypeAnnotation,\n} from \"@babel/types\";\nimport type * as t from \"@babel/types\";\nimport type Binding from \"../../scope/binding\";\n\nimport { createUnionType } from \"./util\";\n\nexport default function (this: NodePath<t.Identifier>, node: t.Identifier) {\n if (!this.isReferenced()) return;\n\n // check if a binding exists of this value and if so then return a union type of all\n // possible types that the binding could be\n const binding = this.scope.getBinding(node.name);\n if (binding) {\n if (binding.identifier.typeAnnotation) {\n return binding.identifier.typeAnnotation;\n } else {\n return getTypeAnnotationBindingConstantViolations(\n binding,\n this,\n node.name,\n );\n }\n }\n\n // built-in values\n if (node.name === \"undefined\") {\n return voidTypeAnnotation();\n } else if (node.name === \"NaN\" || node.name === \"Infinity\") {\n return numberTypeAnnotation();\n } else if (node.name === \"arguments\") {\n // todo\n }\n}\n\nfunction getTypeAnnotationBindingConstantViolations(\n binding: Binding,\n path: NodePath<t.Identifier>,\n name: string,\n) {\n const types = [];\n\n const functionConstantViolations: NodePath[] = [];\n let constantViolations = getConstantViolationsBefore(\n binding,\n path,\n functionConstantViolations,\n );\n\n const testType = getConditionalAnnotation(binding, path, name);\n if (testType) {\n const testConstantViolations = getConstantViolationsBefore(\n binding,\n testType.ifStatement,\n );\n\n // remove constant violations observed before the IfStatement\n constantViolations = constantViolations.filter(\n path => testConstantViolations.indexOf(path) < 0,\n );\n\n // clear current types and add in observed test type\n types.push(testType.typeAnnotation);\n }\n\n if (constantViolations.length) {\n // pick one constant from each scope which will represent the last possible\n // control flow path that it could've taken/been\n /* This code is broken for the following problems:\n * It thinks that assignments can only happen in scopes.\n * What about conditionals, if statements without block,\n * or guarded assignments.\n * It also checks to see if one of the assignments is in the\n * same scope and uses that as the only \"violation\". However,\n * the binding is returned by `getConstantViolationsBefore` so we for\n * sure always going to return that as the only \"violation\".\n let rawConstantViolations = constantViolations.reverse();\n let visitedScopes = [];\n constantViolations = [];\n for (let violation of (rawConstantViolations: Array<NodePath>)) {\n let violationScope = violation.scope;\n if (visitedScopes.indexOf(violationScope) >= 0) continue;\n\n visitedScopes.push(violationScope);\n constantViolations.push(violation);\n\n if (violationScope === path.scope) {\n constantViolations = [violation];\n break;\n }\n }*/\n\n // add back on function constant violations since we can't track calls\n constantViolations.push(...functionConstantViolations);\n\n // push on inferred types of violated paths\n for (const violation of constantViolations) {\n types.push(violation.getTypeAnnotation());\n }\n }\n\n if (!types.length) {\n return;\n }\n\n return createUnionType(types);\n}\n\nfunction getConstantViolationsBefore(\n binding: Binding,\n path: NodePath,\n functions?: NodePath[],\n) {\n const violations = binding.constantViolations.slice();\n violations.unshift(binding.path);\n return violations.filter(violation => {\n violation = violation.resolve();\n const status = violation._guessExecutionStatusRelativeTo(path);\n if (functions && status === \"unknown\") functions.push(violation);\n return status === \"before\";\n });\n}\n\nfunction inferAnnotationFromBinaryExpression(\n name: string,\n path: NodePath<t.BinaryExpression>,\n) {\n const operator = path.node.operator;\n\n const right = path.get(\"right\").resolve();\n const left = path.get(\"left\").resolve();\n\n let target;\n if (left.isIdentifier({ name })) {\n target = right;\n } else if (right.isIdentifier({ name })) {\n target = left;\n }\n\n if (target) {\n if (operator === \"===\") {\n return target.getTypeAnnotation();\n }\n if (BOOLEAN_NUMBER_BINARY_OPERATORS.indexOf(operator) >= 0) {\n return numberTypeAnnotation();\n }\n\n return;\n }\n\n if (operator !== \"===\" && operator !== \"==\") return;\n\n //\n let typeofPath: NodePath<t.UnaryExpression>;\n let typePath: NodePath<t.Expression>;\n if (left.isUnaryExpression({ operator: \"typeof\" })) {\n typeofPath = left;\n typePath = right as NodePath<t.Expression>;\n } else if (right.isUnaryExpression({ operator: \"typeof\" })) {\n typeofPath = right;\n typePath = left as NodePath<t.Expression>;\n }\n\n if (!typeofPath) return;\n // and that the argument of the typeof path references us!\n if (!typeofPath.get(\"argument\").isIdentifier({ name })) return;\n\n // ensure that the type path is a Literal\n typePath = typePath.resolve() as NodePath<t.Expression>;\n if (!typePath.isLiteral()) return;\n\n // and that it's a string so we can infer it\n // @ts-expect-error todo(flow->ts): value is not defined for NullLiteral and some other\n const typeValue = typePath.node.value;\n if (typeof typeValue !== \"string\") return;\n\n // turn type value into a type annotation\n // @ts-expect-error todo(flow->ts): move validation from helper or relax type constraint to just a string\n return createTypeAnnotationBasedOnTypeof(typeValue);\n}\n\nfunction getParentConditionalPath(\n binding: Binding,\n path: NodePath,\n name: string,\n) {\n let parentPath;\n while ((parentPath = path.parentPath)) {\n if (parentPath.isIfStatement() || parentPath.isConditionalExpression()) {\n if (path.key === \"test\") {\n return;\n }\n\n return parentPath as NodePath<t.IfStatement | t.ConditionalExpression>;\n }\n if (parentPath.isFunction()) {\n if (parentPath.parentPath.scope.getBinding(name) !== binding) return;\n }\n\n path = parentPath;\n }\n}\n\nfunction getConditionalAnnotation<T extends t.Node>(\n binding: Binding,\n path: NodePath<T>,\n name?: string,\n): {\n typeAnnotation: t.FlowType | t.TSType;\n ifStatement: NodePath<t.IfStatement | t.ConditionalExpression>;\n} {\n const ifStatement = getParentConditionalPath(binding, path, name);\n if (!ifStatement) return;\n\n const test = ifStatement.get(\"test\");\n const paths = [test];\n const types = [];\n\n for (let i = 0; i < paths.length; i++) {\n const path = paths[i];\n\n if (path.isLogicalExpression()) {\n if (path.node.operator === \"&&\") {\n paths.push(path.get(\"left\"));\n paths.push(path.get(\"right\"));\n }\n } else if (path.isBinaryExpression()) {\n const type = inferAnnotationFromBinaryExpression(name, path);\n if (type) types.push(type);\n }\n }\n\n if (types.length) {\n return {\n typeAnnotation: createUnionType(types),\n ifStatement,\n };\n }\n\n return getConditionalAnnotation(binding, ifStatement, name);\n}\n"],"mappings":";;;;;;;AACA;;AASA;;;EAREA,+B;EACAC,iC;EACAC,oB;EACAC;;;AAOa,kBAAwCC,IAAxC,EAA4D;EACzE,IAAI,CAAC,KAAKC,YAAL,EAAL,EAA0B;EAI1B,MAAMC,OAAO,GAAG,KAAKC,KAAL,CAAWC,UAAX,CAAsBJ,IAAI,CAACK,IAA3B,CAAhB;;EACA,IAAIH,OAAJ,EAAa;IACX,IAAIA,OAAO,CAACI,UAAR,CAAmBC,cAAvB,EAAuC;MACrC,OAAOL,OAAO,CAACI,UAAR,CAAmBC,cAA1B;IACD,CAFD,MAEO;MACL,OAAOC,0CAA0C,CAC/CN,OAD+C,EAE/C,IAF+C,EAG/CF,IAAI,CAACK,IAH0C,CAAjD;IAKD;EACF;;EAGD,IAAIL,IAAI,CAACK,IAAL,KAAc,WAAlB,EAA+B;IAC7B,OAAON,kBAAkB,EAAzB;EACD,CAFD,MAEO,IAAIC,IAAI,CAACK,IAAL,KAAc,KAAd,IAAuBL,IAAI,CAACK,IAAL,KAAc,UAAzC,EAAqD;IAC1D,OAAOP,oBAAoB,EAA3B;EACD,CAFM,MAEA,IAAIE,IAAI,CAACK,IAAL,KAAc,WAAlB,EAA+B,CAErC;AACF;;AAED,SAASG,0CAAT,CACEN,OADF,EAEEO,IAFF,EAGEJ,IAHF,EAI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