@babel/traverse 7.20.0 → 7.20.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of @babel/traverse might be problematic. Click here for more details.
- package/lib/cache.js +0 -3
- package/lib/cache.js.map +1 -1
- package/lib/context.js +7 -21
- package/lib/context.js.map +1 -1
- package/lib/hub.js +0 -6
- package/lib/hub.js.map +1 -1
- package/lib/index.js +3 -21
- package/lib/index.js.map +1 -1
- package/lib/path/ancestry.js +3 -23
- package/lib/path/ancestry.js.map +1 -1
- package/lib/path/comments.js +0 -4
- package/lib/path/comments.js.map +1 -1
- package/lib/path/context.js +14 -54
- package/lib/path/context.js.map +1 -1
- package/lib/path/conversion.js +23 -78
- package/lib/path/conversion.js.map +1 -1
- package/lib/path/evaluation.js +9 -85
- package/lib/path/evaluation.js.map +1 -1
- package/lib/path/family.js +6 -73
- package/lib/path/family.js.map +1 -1
- package/lib/path/index.js +2 -61
- package/lib/path/index.js.map +1 -1
- package/lib/path/inference/index.js +2 -27
- package/lib/path/inference/index.js.map +1 -1
- package/lib/path/inference/inferer-reference.js +10 -31
- package/lib/path/inference/inferer-reference.js.map +1 -1
- package/lib/path/inference/inferers.js +4 -44
- package/lib/path/inference/inferers.js.map +1 -1
- package/lib/path/inference/util.js +0 -4
- package/lib/path/inference/util.js.map +1 -1
- package/lib/path/introspection.js +17 -59
- package/lib/path/introspection.js.map +1 -1
- package/lib/path/lib/hoister.js +7 -23
- package/lib/path/lib/hoister.js.map +1 -1
- package/lib/path/lib/removal-hooks.js +7 -3
- package/lib/path/lib/removal-hooks.js.map +1 -1
- package/lib/path/lib/virtual-types-validator.js +0 -23
- package/lib/path/lib/virtual-types-validator.js.map +1 -1
- package/lib/path/lib/virtual-types.js +1 -0
- package/lib/path/lib/virtual-types.js.map +1 -1
- package/lib/path/modification.js +12 -47
- package/lib/path/modification.js.map +1 -1
- package/lib/path/removal.js +0 -16
- package/lib/path/removal.js.map +1 -1
- package/lib/path/replacement.js +8 -50
- package/lib/path/replacement.js.map +1 -1
- package/lib/scope/binding.js +2 -14
- package/lib/scope/binding.js.map +1 -1
- package/lib/scope/index.js +19 -184
- package/lib/scope/index.js.map +1 -1
- package/lib/scope/lib/renamer.js +2 -25
- package/lib/scope/lib/renamer.js.map +1 -1
- package/lib/traverse-node.js +0 -7
- package/lib/traverse-node.js.map +1 -1
- package/lib/visitors.js +13 -44
- package/lib/visitors.js.map +1 -1
- package/package.json +4 -4
@@ -1 +1 @@
|
|
1
|
-
{"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,C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idTypeAnnotation","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;AAAyC;EARvCA,+BAA+B;EAC/BC,iCAAiC;EACjCC,oBAAoB;EACpBC;AAAkB;AAOL,kBAAwCC,IAAkB,EAAE;EACzE,IAAI,CAAC,IAAI,CAACC,YAAY,EAAE,EAAE;;EAI1B,MAAMC,OAAO,GAAG,IAAI,CAACC,KAAK,CAACC,UAAU,CAACJ,IAAI,CAACK,IAAI,CAAC;EAChD,IAAIH,OAAO,EAAE;IACX,IAAIA,OAAO,CAACI,UAAU,CAACC,cAAc,EAAE;MACrC,OAAOL,OAAO,CAACI,UAAU,CAACC,cAAc;IAC1C,CAAC,MAAM;MACL,OAAOC,0CAA0C,CAC/CN,OAAO,EACP,IAAI,EACJF,IAAI,CAACK,IAAI,CACV;IACH;EACF;;EAGA,IAAIL,IAAI,CAACK,IAAI,KAAK,WAAW,EAAE;IAC7B,OAAON,kBAAkB,EAAE;EAC7B,CAAC,MAAM,IAAIC,IAAI,CAACK,IAAI,KAAK,KAAK,IAAIL,IAAI,CAACK,IAAI,KAAK,UAAU,EAAE;IAC1D,OAAOP,oBAAoB,EAAE;EAC/B,CAAC,MAAM,IAAIE,IAAI,CAACK,IAAI,KAAK,WAAW,EAAE;EAEtC;AACF;;AAEA,SAASG,0CAA0C,CACjDN,OAAgB,EAChBO,IAA4B,EAC5BJ,IAAY,EACZ;EACA,MAAMK,KAAK,GAAG,EAAE;EAEhB,MAAMC,0BAAsC,GAAG,EAAE;EACjD,IAAIC,kBAAkB,GAAGC,2BAA2B,CAClDX,OAAO,EACPO,IAAI,EACJE,0BAA0B,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E;IACrC,MAAM5C,IAAI,GAAG2C,KAAK,CAACC,CAAC,CAAC;IAErB,IAAI5C,IAAI,CAAC6C,mBAAmB,EAAE,EAAE;MAC9B,IAAI7C,IAAI,CAACT,IAAI,CAACiC,QAAQ,KAAK,IAAI,EAAE;QAC/BmB,KAAK,CAAChC,IAAI,CAACX,IAAI,CAAC0B,GAAG,CAAC,MAAM,CAAC,CAAC;QAC5BiB,KAAK,CAAChC,IAAI,CAACX,IAAI,CAAC0B,GAAG,CAAC,OAAO,CAAC,CAAC;MAC/B;IACF,CAAC,MAAM,IAAI1B,IAAI,CAAC8C,kBAAkB,EAAE,EAAE;MACpC,MAAMC,IAAI,GAAGxB,mCAAmC,CAAC3B,IAAI,EAAEI,IAAI,CAAC;MAC5D,IAAI+C,IAAI,EAAE9C,KAAK,CAACU,IAAI,CAACoC,IAAI,CAAC;IAC5B;EACF;EAEA,IAAI9C,KAAK,CAACW,MAAM,EAAE;IAChB,OAAO;MACLd,cAAc,EAAE,IAAAiB,qBAAe,EAACd,KAAK,CAAC;MACtCO;IACF,CAAC;EACH;EAEA,OAAOF,wBAAwB,CAACb,OAAO,EAAEe,WAAW,EAAEZ,IAAI,CAAC;AAC7D"}
|
@@ -34,13 +34,9 @@ exports.TypeCastExpression = TypeCastExpression;
|
|
34
34
|
exports.UnaryExpression = UnaryExpression;
|
35
35
|
exports.UpdateExpression = UpdateExpression;
|
36
36
|
exports.VariableDeclarator = VariableDeclarator;
|
37
|
-
|
38
37
|
var _t = require("@babel/types");
|
39
|
-
|
40
38
|
var _infererReference = require("./inferer-reference");
|
41
|
-
|
42
39
|
var _util = require("./util");
|
43
|
-
|
44
40
|
const {
|
45
41
|
BOOLEAN_BINARY_OPERATORS,
|
46
42
|
BOOLEAN_UNARY_OPERATORS,
|
@@ -61,41 +57,31 @@ const {
|
|
61
57
|
voidTypeAnnotation,
|
62
58
|
isIdentifier
|
63
59
|
} = _t;
|
64
|
-
|
65
60
|
function VariableDeclarator() {
|
66
61
|
if (!this.get("id").isIdentifier()) return;
|
67
62
|
return this.get("init").getTypeAnnotation();
|
68
63
|
}
|
69
|
-
|
70
64
|
function TypeCastExpression(node) {
|
71
65
|
return node.typeAnnotation;
|
72
66
|
}
|
73
|
-
|
74
67
|
TypeCastExpression.validParent = true;
|
75
|
-
|
76
68
|
function TSAsExpression(node) {
|
77
69
|
return node.typeAnnotation;
|
78
70
|
}
|
79
|
-
|
80
71
|
TSAsExpression.validParent = true;
|
81
|
-
|
82
72
|
function TSNonNullExpression() {
|
83
73
|
return this.get("expression").getTypeAnnotation();
|
84
74
|
}
|
85
|
-
|
86
75
|
function NewExpression(node) {
|
87
76
|
if (node.callee.type === "Identifier") {
|
88
77
|
return genericTypeAnnotation(node.callee);
|
89
78
|
}
|
90
79
|
}
|
91
|
-
|
92
80
|
function TemplateLiteral() {
|
93
81
|
return stringTypeAnnotation();
|
94
82
|
}
|
95
|
-
|
96
83
|
function UnaryExpression(node) {
|
97
84
|
const operator = node.operator;
|
98
|
-
|
99
85
|
if (operator === "void") {
|
100
86
|
return voidTypeAnnotation();
|
101
87
|
} else if (NUMBER_UNARY_OPERATORS.indexOf(operator) >= 0) {
|
@@ -106,10 +92,8 @@ function UnaryExpression(node) {
|
|
106
92
|
return booleanTypeAnnotation();
|
107
93
|
}
|
108
94
|
}
|
109
|
-
|
110
95
|
function BinaryExpression(node) {
|
111
96
|
const operator = node.operator;
|
112
|
-
|
113
97
|
if (NUMBER_BINARY_OPERATORS.indexOf(operator) >= 0) {
|
114
98
|
return numberTypeAnnotation();
|
115
99
|
} else if (BOOLEAN_BINARY_OPERATORS.indexOf(operator) >= 0) {
|
@@ -117,7 +101,6 @@ function BinaryExpression(node) {
|
|
117
101
|
} else if (operator === "+") {
|
118
102
|
const right = this.get("right");
|
119
103
|
const left = this.get("left");
|
120
|
-
|
121
104
|
if (left.isBaseType("number") && right.isBaseType("number")) {
|
122
105
|
return numberTypeAnnotation();
|
123
106
|
} else if (left.isBaseType("string") || right.isBaseType("string")) {
|
@@ -127,110 +110,86 @@ function BinaryExpression(node) {
|
|
127
110
|
return unionTypeAnnotation([stringTypeAnnotation(), numberTypeAnnotation()]);
|
128
111
|
}
|
129
112
|
}
|
130
|
-
|
131
113
|
function LogicalExpression() {
|
132
114
|
const argumentTypes = [this.get("left").getTypeAnnotation(), this.get("right").getTypeAnnotation()];
|
133
115
|
return (0, _util.createUnionType)(argumentTypes);
|
134
116
|
}
|
135
|
-
|
136
117
|
function ConditionalExpression() {
|
137
118
|
const argumentTypes = [this.get("consequent").getTypeAnnotation(), this.get("alternate").getTypeAnnotation()];
|
138
119
|
return (0, _util.createUnionType)(argumentTypes);
|
139
120
|
}
|
140
|
-
|
141
121
|
function SequenceExpression() {
|
142
122
|
return this.get("expressions").pop().getTypeAnnotation();
|
143
123
|
}
|
144
|
-
|
145
124
|
function ParenthesizedExpression() {
|
146
125
|
return this.get("expression").getTypeAnnotation();
|
147
126
|
}
|
148
|
-
|
149
127
|
function AssignmentExpression() {
|
150
128
|
return this.get("right").getTypeAnnotation();
|
151
129
|
}
|
152
|
-
|
153
130
|
function UpdateExpression(node) {
|
154
131
|
const operator = node.operator;
|
155
|
-
|
156
132
|
if (operator === "++" || operator === "--") {
|
157
133
|
return numberTypeAnnotation();
|
158
134
|
}
|
159
135
|
}
|
160
|
-
|
161
136
|
function StringLiteral() {
|
162
137
|
return stringTypeAnnotation();
|
163
138
|
}
|
164
|
-
|
165
139
|
function NumericLiteral() {
|
166
140
|
return numberTypeAnnotation();
|
167
141
|
}
|
168
|
-
|
169
142
|
function BooleanLiteral() {
|
170
143
|
return booleanTypeAnnotation();
|
171
144
|
}
|
172
|
-
|
173
145
|
function NullLiteral() {
|
174
146
|
return nullLiteralTypeAnnotation();
|
175
147
|
}
|
176
|
-
|
177
148
|
function RegExpLiteral() {
|
178
149
|
return genericTypeAnnotation(identifier("RegExp"));
|
179
150
|
}
|
180
|
-
|
181
151
|
function ObjectExpression() {
|
182
152
|
return genericTypeAnnotation(identifier("Object"));
|
183
153
|
}
|
184
|
-
|
185
154
|
function ArrayExpression() {
|
186
155
|
return genericTypeAnnotation(identifier("Array"));
|
187
156
|
}
|
188
|
-
|
189
157
|
function RestElement() {
|
190
158
|
return ArrayExpression();
|
191
159
|
}
|
192
|
-
|
193
160
|
RestElement.validParent = true;
|
194
|
-
|
195
161
|
function Func() {
|
196
162
|
return genericTypeAnnotation(identifier("Function"));
|
197
163
|
}
|
198
|
-
|
199
164
|
const isArrayFrom = buildMatchMemberExpression("Array.from");
|
200
165
|
const isObjectKeys = buildMatchMemberExpression("Object.keys");
|
201
166
|
const isObjectValues = buildMatchMemberExpression("Object.values");
|
202
167
|
const isObjectEntries = buildMatchMemberExpression("Object.entries");
|
203
|
-
|
204
168
|
function CallExpression() {
|
205
169
|
const {
|
206
170
|
callee
|
207
171
|
} = this.node;
|
208
|
-
|
209
172
|
if (isObjectKeys(callee)) {
|
210
173
|
return arrayTypeAnnotation(stringTypeAnnotation());
|
211
|
-
} else if (isArrayFrom(callee) || isObjectValues(callee) ||
|
174
|
+
} else if (isArrayFrom(callee) || isObjectValues(callee) ||
|
175
|
+
isIdentifier(callee, {
|
212
176
|
name: "Array"
|
213
177
|
})) {
|
214
178
|
return arrayTypeAnnotation(anyTypeAnnotation());
|
215
179
|
} else if (isObjectEntries(callee)) {
|
216
180
|
return arrayTypeAnnotation(tupleTypeAnnotation([stringTypeAnnotation(), anyTypeAnnotation()]));
|
217
181
|
}
|
218
|
-
|
219
182
|
return resolveCall(this.get("callee"));
|
220
183
|
}
|
221
|
-
|
222
184
|
function TaggedTemplateExpression() {
|
223
185
|
return resolveCall(this.get("tag"));
|
224
186
|
}
|
225
|
-
|
226
187
|
function resolveCall(callee) {
|
227
188
|
callee = callee.resolve();
|
228
|
-
|
229
189
|
if (callee.isFunction()) {
|
230
190
|
const {
|
231
191
|
node
|
232
192
|
} = callee;
|
233
|
-
|
234
193
|
if (node.async) {
|
235
194
|
if (node.generator) {
|
236
195
|
return genericTypeAnnotation(identifier("AsyncIterator"));
|
@@ -242,7 +201,8 @@ function resolveCall(callee) {
|
|
242
201
|
return genericTypeAnnotation(identifier("Iterator"));
|
243
202
|
} else if (callee.node.returnType) {
|
244
203
|
return callee.node.returnType;
|
245
|
-
} else {
|
204
|
+
} else {
|
205
|
+
}
|
246
206
|
}
|
247
207
|
}
|
248
208
|
}
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"names":["BOOLEAN_BINARY_OPERATORS","BOOLEAN_UNARY_OPERATORS","NUMBER_BINARY_OPERATORS","NUMBER_UNARY_OPERATORS","STRING_UNARY_OPERATORS","anyTypeAnnotation","arrayTypeAnnotation","booleanTypeAnnotation","buildMatchMemberExpression","genericTypeAnnotation","identifier","nullLiteralTypeAnnotation","numberTypeAnnotation","stringTypeAnnotation","tupleTypeAnnotation","unionTypeAnnotation","voidTypeAnnotation","isIdentifier","VariableDeclarator","get","getTypeAnnotation","TypeCastExpression","node","typeAnnotation","validParent","TSAsExpression","TSNonNullExpression","NewExpression","callee","type","TemplateLiteral","UnaryExpression","operator","indexOf","BinaryExpression","right","left","isBaseType","LogicalExpression","argumentTypes","createUnionType","ConditionalExpression","SequenceExpression","pop","ParenthesizedExpression","AssignmentExpression","UpdateExpression","StringLiteral","NumericLiteral","BooleanLiteral","NullLiteral","RegExpLiteral","ObjectExpression","ArrayExpression","RestElement","Func","isArrayFrom","isObjectKeys","isObjectValues","isObjectEntries","CallExpression","name","resolveCall","TaggedTemplateExpression","resolve","isFunction","async","generator","returnType"],"sources":["../../../src/path/inference/inferers.ts"],"sourcesContent":["import {\n BOOLEAN_BINARY_OPERATORS,\n BOOLEAN_UNARY_OPERATORS,\n NUMBER_BINARY_OPERATORS,\n NUMBER_UNARY_OPERATORS,\n STRING_UNARY_OPERATORS,\n anyTypeAnnotation,\n arrayTypeAnnotation,\n booleanTypeAnnotation,\n buildMatchMemberExpression,\n genericTypeAnnotation,\n identifier,\n nullLiteralTypeAnnotation,\n numberTypeAnnotation,\n stringTypeAnnotation,\n tupleTypeAnnotation,\n unionTypeAnnotation,\n voidTypeAnnotation,\n isIdentifier,\n} from \"@babel/types\";\nimport type * as t from \"@babel/types\";\n\nexport { default as Identifier } from \"./inferer-reference\";\n\nimport { createUnionType } from \"./util\";\nimport type NodePath from \"..\";\n\nexport function VariableDeclarator(this: NodePath<t.VariableDeclarator>) {\n if (!this.get(\"id\").isIdentifier()) return;\n return this.get(\"init\").getTypeAnnotation();\n}\n\nexport function TypeCastExpression(node: t.TypeCastExpression) {\n return node.typeAnnotation;\n}\n\nTypeCastExpression.validParent = true;\n\nexport function TSAsExpression(node: t.TSAsExpression) {\n return node.typeAnnotation;\n}\n\nTSAsExpression.validParent = true;\n\nexport function TSNonNullExpression(this: NodePath<t.TSNonNullExpression>) {\n return this.get(\"expression\").getTypeAnnotation();\n}\n\nexport function NewExpression(\n this: NodePath<t.NewExpression>,\n node: t.NewExpression,\n) {\n if (node.callee.type === \"Identifier\") {\n // only resolve identifier callee\n return genericTypeAnnotation(node.callee);\n }\n}\n\nexport function TemplateLiteral() {\n return stringTypeAnnotation();\n}\n\nexport function UnaryExpression(node: t.UnaryExpression) {\n const operator = node.operator;\n\n if (operator === \"void\") {\n return voidTypeAnnotation();\n } else if (NUMBER_UNARY_OPERATORS.indexOf(operator) >= 0) {\n return numberTypeAnnotation();\n } else if (STRING_UNARY_OPERATORS.indexOf(operator) >= 0) {\n return stringTypeAnnotation();\n } else if (BOOLEAN_UNARY_OPERATORS.indexOf(operator) >= 0) {\n return booleanTypeAnnotation();\n }\n}\n\nexport function BinaryExpression(\n this: NodePath<t.BinaryExpression>,\n node: t.BinaryExpression,\n) {\n const operator = node.operator;\n\n if (NUMBER_BINARY_OPERATORS.indexOf(operator) >= 0) {\n return numberTypeAnnotation();\n } else if (BOOLEAN_BINARY_OPERATORS.indexOf(operator) >= 0) {\n return booleanTypeAnnotation();\n } else if (operator === \"+\") {\n const right = this.get(\"right\");\n const left = this.get(\"left\");\n\n if (left.isBaseType(\"number\") && right.isBaseType(\"number\")) {\n // both numbers so this will be a number\n return numberTypeAnnotation();\n } else if (left.isBaseType(\"string\") || right.isBaseType(\"string\")) {\n // one is a string so the result will be a string\n return stringTypeAnnotation();\n }\n\n // unsure if left and right are strings or numbers so stay on the safe side\n return unionTypeAnnotation([\n stringTypeAnnotation(),\n numberTypeAnnotation(),\n ]);\n }\n}\n\nexport function LogicalExpression(this: NodePath<t.LogicalExpression>) {\n const argumentTypes = [\n this.get(\"left\").getTypeAnnotation(),\n this.get(\"right\").getTypeAnnotation(),\n ];\n\n return createUnionType(argumentTypes);\n}\n\nexport function ConditionalExpression(this: NodePath<t.ConditionalExpression>) {\n const argumentTypes = [\n this.get(\"consequent\").getTypeAnnotation(),\n this.get(\"alternate\").getTypeAnnotation(),\n ];\n\n return createUnionType(argumentTypes);\n}\n\nexport function SequenceExpression(this: NodePath<t.SequenceExpression>) {\n return this.get(\"expressions\").pop().getTypeAnnotation();\n}\n\nexport function ParenthesizedExpression(\n this: NodePath<t.ParenthesizedExpression>,\n) {\n return this.get(\"expression\").getTypeAnnotation();\n}\n\nexport function AssignmentExpression(this: NodePath<t.AssignmentExpression>) {\n return this.get(\"right\").getTypeAnnotation();\n}\n\nexport function UpdateExpression(\n this: NodePath<t.UpdateExpression>,\n node: t.UpdateExpression,\n) {\n const operator = node.operator;\n if (operator === \"++\" || operator === \"--\") {\n return numberTypeAnnotation();\n }\n}\n\nexport function StringLiteral() {\n return stringTypeAnnotation();\n}\n\nexport function NumericLiteral() {\n return numberTypeAnnotation();\n}\n\nexport function BooleanLiteral() {\n return booleanTypeAnnotation();\n}\n\nexport function NullLiteral() {\n return nullLiteralTypeAnnotation();\n}\n\nexport function RegExpLiteral() {\n return genericTypeAnnotation(identifier(\"RegExp\"));\n}\n\nexport function ObjectExpression() {\n return genericTypeAnnotation(identifier(\"Object\"));\n}\n\nexport function ArrayExpression() {\n return genericTypeAnnotation(identifier(\"Array\"));\n}\n\nexport function RestElement() {\n return ArrayExpression();\n}\n\nRestElement.validParent = true;\n\nfunction Func() {\n return genericTypeAnnotation(identifier(\"Function\"));\n}\n\nexport {\n Func as FunctionExpression,\n Func as ArrowFunctionExpression,\n Func as FunctionDeclaration,\n Func as ClassExpression,\n Func as ClassDeclaration,\n};\n\nconst isArrayFrom = buildMatchMemberExpression(\"Array.from\");\nconst isObjectKeys = buildMatchMemberExpression(\"Object.keys\");\nconst isObjectValues = buildMatchMemberExpression(\"Object.values\");\nconst isObjectEntries = buildMatchMemberExpression(\"Object.entries\");\nexport function CallExpression(this: NodePath<t.CallExpression>) {\n const { callee } = this.node;\n if (isObjectKeys(callee)) {\n return arrayTypeAnnotation(stringTypeAnnotation());\n } else if (\n isArrayFrom(callee) ||\n isObjectValues(callee) ||\n // Detect \"var foo = Array()\" calls so we can optimize for arrays vs iterables.\n isIdentifier(callee, { name: \"Array\" })\n ) {\n return arrayTypeAnnotation(anyTypeAnnotation());\n } else if (isObjectEntries(callee)) {\n return arrayTypeAnnotation(\n tupleTypeAnnotation([stringTypeAnnotation(), anyTypeAnnotation()]),\n );\n }\n\n return resolveCall(this.get(\"callee\"));\n}\n\nexport function TaggedTemplateExpression(\n this: NodePath<t.TaggedTemplateExpression>,\n) {\n return resolveCall(this.get(\"tag\"));\n}\n\nfunction resolveCall(callee: NodePath) {\n callee = callee.resolve();\n\n if (callee.isFunction()) {\n const { node } = callee;\n if (node.async) {\n if (node.generator) {\n return genericTypeAnnotation(identifier(\"AsyncIterator\"));\n } else {\n return genericTypeAnnotation(identifier(\"Promise\"));\n }\n } else {\n if (node.generator) {\n return genericTypeAnnotation(identifier(\"Iterator\"));\n } else if (callee.node.returnType) {\n return callee.node.returnType;\n } else {\n // todo: get union type of all return arguments\n }\n }\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;;AAsBA;;AAEA;;;EAvBEA,wB;EACAC,uB;EACAC,uB;EACAC,sB;EACAC,sB;EACAC,iB;EACAC,mB;EACAC,qB;EACA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EAEA,IAAIpC,MAAM,CAACqC,UAAP,EAAJ,EAAyB;IACvB,MAAM;MAAE3C;IAAF,IAAWM,MAAjB;;IACA,IAAIN,IAAI,CAAC4C,KAAT,EAAgB;MACd,IAAI5C,IAAI,CAAC6C,SAAT,EAAoB;QAClB,OAAO1D,qBAAqB,CAACC,UAAU,CAAC,eAAD,CAAX,CAA5B;MACD,CAFD,MAEO;QACL,OAAOD,qBAAqB,CAACC,UAAU,CAAC,SAAD,CAAX,CAA5B;MACD;IACF,CAND,MAMO;MACL,IAAIY,IAAI,CAAC6C,SAAT,EAAoB;QAClB,OAAO1D,qBAAqB,CAACC,UAAU,CAAC,UAAD,CAAX,CAA5B;MACD,CAFD,MAEO,IAAIkB,MAAM,CAACN,IAAP,CAAY8C,UAAhB,EAA4B;QACjC,OAAOxC,MAAM,CAACN,IAAP,CAAY8C,UAAnB;MACD,CAFM,MAEA,CAEN;IACF;EACF;AACF"}
|
1
|
+
{"version":3,"names":["BOOLEAN_BINARY_OPERATORS","BOOLEAN_UNARY_OPERATORS","NUMBER_BINARY_OPERATORS","NUMBER_UNARY_OPERATORS","STRING_UNARY_OPERATORS","anyTypeAnnotation","arrayTypeAnnotation","booleanTypeAnnotation","buildMatchMemberExpression","genericTypeAnnotation","identifier","nullLiteralTypeAnnotation","numberTypeAnnotation","stringTypeAnnotation","tupleTypeAnnotation","unionTypeAnnotation","voidTypeAnnotation","isIdentifier","VariableDeclarator","get","getTypeAnnotation","TypeCastExpression","node","typeAnnotation","validParent","TSAsExpression","TSNonNullExpression","NewExpression","callee","type","TemplateLiteral","UnaryExpression","operator","indexOf","BinaryExpression","right","left","isBaseType","LogicalExpression","argumentTypes","createUnionType","ConditionalExpression","SequenceExpression","pop","ParenthesizedExpression","AssignmentExpression","UpdateExpression","StringLiteral","NumericLiteral","BooleanLiteral","NullLiteral","RegExpLiteral","ObjectExpression","ArrayExpression","RestElement","Func","isArrayFrom","isObjectKeys","isObjectValues","isObjectEntries","CallExpression","name","resolveCall","TaggedTemplateExpression","resolve","isFunction","async","generator","returnType"],"sources":["../../../src/path/inference/inferers.ts"],"sourcesContent":["import {\n BOOLEAN_BINARY_OPERATORS,\n BOOLEAN_UNARY_OPERATORS,\n NUMBER_BINARY_OPERATORS,\n NUMBER_UNARY_OPERATORS,\n STRING_UNARY_OPERATORS,\n anyTypeAnnotation,\n arrayTypeAnnotation,\n booleanTypeAnnotation,\n buildMatchMemberExpression,\n genericTypeAnnotation,\n identifier,\n nullLiteralTypeAnnotation,\n numberTypeAnnotation,\n stringTypeAnnotation,\n tupleTypeAnnotation,\n unionTypeAnnotation,\n voidTypeAnnotation,\n isIdentifier,\n} from \"@babel/types\";\nimport type * as t from \"@babel/types\";\n\nexport { default as Identifier } from \"./inferer-reference\";\n\nimport { createUnionType } from \"./util\";\nimport type NodePath from \"..\";\n\nexport function VariableDeclarator(this: NodePath<t.VariableDeclarator>) {\n if (!this.get(\"id\").isIdentifier()) return;\n return this.get(\"init\").getTypeAnnotation();\n}\n\nexport function TypeCastExpression(node: t.TypeCastExpression) {\n return node.typeAnnotation;\n}\n\nTypeCastExpression.validParent = true;\n\nexport function TSAsExpression(node: t.TSAsExpression) {\n return node.typeAnnotation;\n}\n\nTSAsExpression.validParent = true;\n\nexport function TSNonNullExpression(this: NodePath<t.TSNonNullExpression>) {\n return this.get(\"expression\").getTypeAnnotation();\n}\n\nexport function NewExpression(\n this: NodePath<t.NewExpression>,\n node: t.NewExpression,\n) {\n if (node.callee.type === \"Identifier\") {\n // only resolve identifier callee\n return genericTypeAnnotation(node.callee);\n }\n}\n\nexport function TemplateLiteral() {\n return stringTypeAnnotation();\n}\n\nexport function UnaryExpression(node: t.UnaryExpression) {\n const operator = node.operator;\n\n if (operator === \"void\") {\n return voidTypeAnnotation();\n } else if (NUMBER_UNARY_OPERATORS.indexOf(operator) >= 0) {\n return numberTypeAnnotation();\n } else if (STRING_UNARY_OPERATORS.indexOf(operator) >= 0) {\n return stringTypeAnnotation();\n } else if (BOOLEAN_UNARY_OPERATORS.indexOf(operator) >= 0) {\n return booleanTypeAnnotation();\n }\n}\n\nexport function BinaryExpression(\n this: NodePath<t.BinaryExpression>,\n node: t.BinaryExpression,\n) {\n const operator = node.operator;\n\n if (NUMBER_BINARY_OPERATORS.indexOf(operator) >= 0) {\n return numberTypeAnnotation();\n } else if (BOOLEAN_BINARY_OPERATORS.indexOf(operator) >= 0) {\n return booleanTypeAnnotation();\n } else if (operator === \"+\") {\n const right = this.get(\"right\");\n const left = this.get(\"left\");\n\n if (left.isBaseType(\"number\") && right.isBaseType(\"number\")) {\n // both numbers so this will be a number\n return numberTypeAnnotation();\n } else if (left.isBaseType(\"string\") || right.isBaseType(\"string\")) {\n // one is a string so the result will be a string\n return stringTypeAnnotation();\n }\n\n // unsure if left and right are strings or numbers so stay on the safe side\n return unionTypeAnnotation([\n stringTypeAnnotation(),\n numberTypeAnnotation(),\n ]);\n }\n}\n\nexport function LogicalExpression(this: NodePath<t.LogicalExpression>) {\n const argumentTypes = [\n this.get(\"left\").getTypeAnnotation(),\n this.get(\"right\").getTypeAnnotation(),\n ];\n\n return createUnionType(argumentTypes);\n}\n\nexport function ConditionalExpression(this: NodePath<t.ConditionalExpression>) {\n const argumentTypes = [\n this.get(\"consequent\").getTypeAnnotation(),\n this.get(\"alternate\").getTypeAnnotation(),\n ];\n\n return createUnionType(argumentTypes);\n}\n\nexport function SequenceExpression(this: NodePath<t.SequenceExpression>) {\n return this.get(\"expressions\").pop().getTypeAnnotation();\n}\n\nexport function ParenthesizedExpression(\n this: NodePath<t.ParenthesizedExpression>,\n) {\n return this.get(\"expression\").getTypeAnnotation();\n}\n\nexport function AssignmentExpression(this: NodePath<t.AssignmentExpression>) {\n return this.get(\"right\").getTypeAnnotation();\n}\n\nexport function UpdateExpression(\n this: NodePath<t.UpdateExpression>,\n node: t.UpdateExpression,\n) {\n const operator = node.operator;\n if (operator === \"++\" || operator === \"--\") {\n return numberTypeAnnotation();\n }\n}\n\nexport function StringLiteral() {\n return stringTypeAnnotation();\n}\n\nexport function NumericLiteral() {\n return numberTypeAnnotation();\n}\n\nexport function BooleanLiteral() {\n return booleanTypeAnnotation();\n}\n\nexport function NullLiteral() {\n return nullLiteralTypeAnnotation();\n}\n\nexport function RegExpLiteral() {\n return genericTypeAnnotation(identifier(\"RegExp\"));\n}\n\nexport function ObjectExpression() {\n return genericTypeAnnotation(identifier(\"Object\"));\n}\n\nexport function ArrayExpression() {\n return genericTypeAnnotation(identifier(\"Array\"));\n}\n\nexport function RestElement() {\n return ArrayExpression();\n}\n\nRestElement.validParent = true;\n\nfunction Func() {\n return genericTypeAnnotation(identifier(\"Function\"));\n}\n\nexport {\n Func as FunctionExpression,\n Func as ArrowFunctionExpression,\n Func as FunctionDeclaration,\n Func as ClassExpression,\n Func as ClassDeclaration,\n};\n\nconst isArrayFrom = buildMatchMemberExpression(\"Array.from\");\nconst isObjectKeys = buildMatchMemberExpression(\"Object.keys\");\nconst isObjectValues = buildMatchMemberExpression(\"Object.values\");\nconst isObjectEntries = buildMatchMemberExpression(\"Object.entries\");\nexport function CallExpression(this: NodePath<t.CallExpression>) {\n const { callee } = this.node;\n if (isObjectKeys(callee)) {\n return arrayTypeAnnotation(stringTypeAnnotation());\n } else if (\n isArrayFrom(callee) ||\n isObjectValues(callee) ||\n // Detect \"var foo = Array()\" calls so we can optimize for arrays vs iterables.\n isIdentifier(callee, { name: \"Array\" })\n ) {\n return arrayTypeAnnotation(anyTypeAnnotation());\n } else if (isObjectEntries(callee)) {\n return arrayTypeAnnotation(\n tupleTypeAnnotation([stringTypeAnnotation(), anyTypeAnnotation()]),\n );\n }\n\n return resolveCall(this.get(\"callee\"));\n}\n\nexport function TaggedTemplateExpression(\n this: NodePath<t.TaggedTemplateExpression>,\n) {\n return resolveCall(this.get(\"tag\"));\n}\n\nfunction resolveCall(callee: NodePath) {\n callee = callee.resolve();\n\n if (callee.isFunction()) {\n const { node } = callee;\n if (node.async) {\n if (node.generator) {\n return genericTypeAnnotation(identifier(\"AsyncIterator\"));\n } else {\n return genericTypeAnnotation(identifier(\"Promise\"));\n }\n } else {\n if (node.generator) {\n return genericTypeAnnotation(identifier(\"Iterator\"));\n } else if (callee.node.returnType) {\n return callee.node.returnType;\n } else {\n // todo: get union type of all return arguments\n }\n }\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAsBA;AAEA;AAAyC;EAvBvCA,wBAAwB;EACxBC,uBAAuB;EACvBC,uBAAuB;EACvBC,sBAAsB;EACtBC,sBAAsB;EACtBC,iBAAiB;EACjBC,mBAAmB;EACnBC,qBAAqB;EACrBC,0BAA0B;EAC1BC,qBAAqB;EACrBC,UAAU;EACVC,yBAAyB;EACzBC,oBAAoB;EACpBC,oBAAoB;EACpBC,mBAAmB;EACnBC,mBAAmB;EACnBC,kBAAkB;EAClBC;AAAY;AASP,SAASC,kBAAkB,GAAuC;EACvE,IAAI,CAAC,IAAI,CAACC,GAAG,CAAC,IAAI,CAAC,CAACF,YAAY,EAAE,EAAE;EACpC,OAAO,IAAI,CAACE,GAAG,CAAC,MAAM,CAAC,CAACC,iBAAiB,EAAE;AAC7C;AAEO,SAASC,kBAAkB,CAACC,IAA0B,EAAE;EAC7D,OAAOA,IAAI,CAACC,cAAc;AAC5B;AAEAF,kBAAkB,CAACG,WAAW,GAAG,IAAI;AAE9B,SAASC,cAAc,CAACH,IAAsB,EAAE;EACrD,OAAOA,IAAI,CAACC,cAAc;AAC5B;AAEAE,cAAc,CAACD,WAAW,GAAG,IAAI;AAE1B,SAASE,mBAAmB,GAAwC;EACzE,OAAO,IAAI,CAACP,GAAG,CAAC,YAAY,CAAC,CAACC,iBAAiB,EAAE;AACnD;AAEO,SAASO,aAAa,CAE3BL,IAAqB,EACrB;EACA,IAAIA,IAAI,CAACM,MAAM,CAACC,IAAI,KAAK,YAAY,EAAE;IAErC,OAAOpB,qBAAqB,CAACa,IAAI,CAACM,MAAM,CAAC;EAC3C;AACF;AAEO,SAASE,e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_t = require("@babel/types");
|
9
|
-
|
10
8
|
const {
|
11
9
|
createFlowUnionType,
|
12
10
|
createTSUnionType,
|
@@ -14,14 +12,12 @@ const {
|
|
14
12
|
isFlowType,
|
15
13
|
isTSType
|
16
14
|
} = _t;
|
17
|
-
|
18
15
|
function createUnionType(types) {
|
19
16
|
{
|
20
17
|
if (isFlowType(types[0])) {
|
21
18
|
if (createFlowUnionType) {
|
22
19
|
return createFlowUnionType(types);
|
23
20
|
}
|
24
|
-
|
25
21
|
return createUnionTypeAnnotation(types);
|
26
22
|
} else {
|
27
23
|
if (createTSUnionType) {
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"names":["createFlowUnionType","createTSUnionType","createUnionTypeAnnotation","isFlowType","isTSType","createUnionType","types"],"sources":["../../../src/path/inference/util.ts"],"sourcesContent":["import {\n createFlowUnionType,\n createTSUnionType,\n createUnionTypeAnnotation,\n isFlowType,\n isTSType,\n} from \"@babel/types\";\nimport type * as t from \"@babel/types\";\n\nexport function createUnionType(\n types: Array<t.FlowType | t.TSType>,\n): t.FlowType | t.TSType {\n if (process.env.BABEL_8_BREAKING) {\n if (isFlowType(types[0])) {\n return createFlowUnionType(types as t.FlowType[]);\n }\n if (isTSType(types[0])) {\n return createTSUnionType(types as t.TSType[]);\n }\n } else {\n if (isFlowType(types[0])) {\n if (createFlowUnionType) {\n return createFlowUnionType(types as t.FlowType[]);\n }\n\n return createUnionTypeAnnotation(types as t.FlowType[]);\n } else {\n if (createTSUnionType) {\n return createTSUnionType(types as t.TSType[]);\n }\n }\n }\n}\n"],"mappings":"
|
1
|
+
{"version":3,"names":["createFlowUnionType","createTSUnionType","createUnionTypeAnnotation","isFlowType","isTSType","createUnionType","types"],"sources":["../../../src/path/inference/util.ts"],"sourcesContent":["import {\n createFlowUnionType,\n createTSUnionType,\n createUnionTypeAnnotation,\n isFlowType,\n isTSType,\n} from \"@babel/types\";\nimport type * as t from \"@babel/types\";\n\nexport function createUnionType(\n types: Array<t.FlowType | t.TSType>,\n): t.FlowType | t.TSType {\n if (process.env.BABEL_8_BREAKING) {\n if (isFlowType(types[0])) {\n return createFlowUnionType(types as t.FlowType[]);\n }\n if (isTSType(types[0])) {\n return createTSUnionType(types as t.TSType[]);\n }\n } else {\n if (isFlowType(types[0])) {\n if (createFlowUnionType) {\n return createFlowUnionType(types as t.FlowType[]);\n }\n\n return createUnionTypeAnnotation(types as t.FlowType[]);\n } else {\n if (createTSUnionType) {\n return createTSUnionType(types as t.TSType[]);\n }\n }\n }\n}\n"],"mappings":";;;;;;AAAA;AAMsB;EALpBA,mBAAmB;EACnBC,iBAAiB;EACjBC,yBAAyB;EACzBC,UAAU;EACVC;AAAQ;AAIH,SAASC,eAAe,CAC7BC,KAAmC,EACZ;EAQhB;IACL,IAAIH,UAAU,CAACG,KAAK,CAAC,CAAC,CAAC,CAAC,EAAE;MACxB,IAAIN,mBAAmB,EAAE;QACvB,OAAOA,mBAAmB,CAACM,KAAK,CAAiB;MACnD;MAEA,OAAOJ,yBAAyB,CAACI,KAAK,CAAiB;IACzD,CAAC,MAAM;MACL,IAAIL,iBAAiB,EAAE;QACrB,OAAOA,iBAAiB,CAACK,KAAK,CAAe;MAC/C;IACF;EACF;AACF"}
|