@babel/traverse 7.21.4-esm.1 → 7.21.4-esm.3

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.

Files changed (58) hide show
  1. package/lib/cache.js +18 -7
  2. package/lib/cache.js.map +1 -1
  3. package/lib/context.js +11 -4
  4. package/lib/context.js.map +1 -1
  5. package/lib/hub.js +8 -1
  6. package/lib/hub.js.map +1 -1
  7. package/lib/index.js +36 -11
  8. package/lib/index.js.map +1 -1
  9. package/lib/path/ancestry.js +26 -11
  10. package/lib/path/ancestry.js.map +1 -1
  11. package/lib/path/comments.js +12 -4
  12. package/lib/path/comments.js.map +1 -1
  13. package/lib/path/context.js +49 -25
  14. package/lib/path/context.js.map +1 -1
  15. package/lib/path/conversion.js +26 -16
  16. package/lib/path/conversion.js.map +1 -1
  17. package/lib/path/evaluation.js +9 -2
  18. package/lib/path/evaluation.js.map +1 -1
  19. package/lib/path/family.js +33 -19
  20. package/lib/path/family.js.map +1 -1
  21. package/lib/path/index.js +40 -30
  22. package/lib/path/index.js.map +1 -1
  23. package/lib/path/inference/index.js +19 -8
  24. package/lib/path/inference/index.js.map +1 -1
  25. package/lib/path/inference/inferer-reference.js +11 -5
  26. package/lib/path/inference/inferer-reference.js.map +1 -1
  27. package/lib/path/inference/inferers.js +65 -30
  28. package/lib/path/inference/inferers.js.map +1 -1
  29. package/lib/path/inference/util.js +8 -2
  30. package/lib/path/inference/util.js.map +1 -1
  31. package/lib/path/introspection.js +45 -20
  32. package/lib/path/introspection.js.map +1 -1
  33. package/lib/path/lib/hoister.js +10 -3
  34. package/lib/path/lib/hoister.js.map +1 -1
  35. package/lib/path/lib/removal-hooks.js +8 -1
  36. package/lib/path/lib/removal-hooks.js.map +1 -1
  37. package/lib/path/lib/virtual-types-validator.js +42 -19
  38. package/lib/path/lib/virtual-types-validator.js.map +1 -1
  39. package/lib/path/lib/virtual-types.js +42 -18
  40. package/lib/path/lib/virtual-types.js.map +1 -1
  41. package/lib/path/modification.js +34 -19
  42. package/lib/path/modification.js.map +1 -1
  43. package/lib/path/removal.js +23 -12
  44. package/lib/path/removal.js.map +1 -1
  45. package/lib/path/replacement.js +33 -22
  46. package/lib/path/replacement.js.map +1 -1
  47. package/lib/scope/binding.js +8 -1
  48. package/lib/scope/binding.js.map +1 -1
  49. package/lib/scope/index.js +20 -13
  50. package/lib/scope/index.js.map +1 -1
  51. package/lib/scope/lib/renamer.js +16 -9
  52. package/lib/scope/lib/renamer.js.map +1 -1
  53. package/lib/traverse-node.js +10 -4
  54. package/lib/traverse-node.js.map +1 -1
  55. package/lib/visitors.js +13 -5
  56. package/lib/visitors.js.map +1 -1
  57. package/package.json +10 -10
  58. package/lib/package.json +0 -1
@@ -1,12 +1,18 @@
1
- import * as _t from "@babel/types";
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.default = _default;
7
+ var _t = require("@babel/types");
8
+ var _util = require("./util");
2
9
  const {
3
10
  BOOLEAN_NUMBER_BINARY_OPERATORS,
4
11
  createTypeAnnotationBasedOnTypeof,
5
12
  numberTypeAnnotation,
6
13
  voidTypeAnnotation
7
14
  } = _t;
8
- import { createUnionType } from "./util.js";
9
- export default function (node) {
15
+ function _default(node) {
10
16
  if (!this.isReferenced()) return;
11
17
  const binding = this.scope.getBinding(node.name);
12
18
  if (binding) {
@@ -41,7 +47,7 @@ function getTypeAnnotationBindingConstantViolations(binding, path, name) {
41
47
  if (!types.length) {
42
48
  return;
43
49
  }
44
- return createUnionType(types);
50
+ return (0, _util.createUnionType)(types);
45
51
  }
46
52
  function getConstantViolationsBefore(binding, path, functions) {
47
53
  const violations = binding.constantViolations.slice();
@@ -135,7 +141,7 @@ function getConditionalAnnotation(binding, path, name) {
135
141
  }
136
142
  if (types.length) {
137
143
  return {
138
- typeAnnotation: createUnionType(types),
144
+ typeAnnotation: (0, _util.createUnionType)(types),
139
145
  ifStatement
140
146
  };
141
147
  }
@@ -1 +1 @@
1
- {"version":3,"names":["_t","BOOLEAN_NUMBER_BINARY_OPERATORS","createTypeAnnotationBasedOnTypeof","numberTypeAnnotation","voidTypeAnnotation","createUnionType","node","isReferenced","binding","scope","getBinding","name","identifier","typeAnnotation","getTypeAnnotationBindingConstantViolations","path","types","functionConstantViolations","constantViolations","getConstantViolationsBefore","testType","getConditionalAnnotation","testConstantViolations","ifStatement","filter","indexOf","push","length","violation","getTypeAnnotation","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,YAAAA,EAAA,MAKO,cAAc;AAAC;EAJpBC,+BAA+B;EAC/BC,iCAAiC;EACjCC,oBAAoB;EACpBC;AAAkB,IAAAJ,EAAA;AAKpB,SAASK,eAAe,QAAQ,WAAQ;AAExC,eAAe,UAAwCC,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,OAAOP,kBAAkB,EAAE;EAC7B,CAAC,MAAM,IAAIE,IAAI,CAACK,IAAI,KAAK,KAAK,IAAIL,IAAI,CAACK,IAAI,KAAK,UAAU,EAAE;IAC1D,OAAOR,oBAAoB,EAAE;EAC/B,CAAC,MAAM,IAAIG,IAAI,CAACK,IAAI,KAAK,WAAW,EAAE,CAEtC;AACF;AAEA,SAASG,0C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EACpC,MAAMiB,KAAK,GAAG,CAACD,IAAI,CAAC;EACpB,MAAMxC,KAAK,GAAG,EAAE;EAEhB,KAAK,IAAI0C,CAAC,GAAG,CAAC,EAAEA,CAAC,GAAGD,KAAK,CAAC9B,MAAM,EAAE+B,CAAC,EAAE,EAAE;IACrC,MAAM3C,IAAI,GAAG0C,KAAK,CAACC,CAAC,CAAC;IAErB,IAAI3C,IAAI,CAAC4C,mBAAmB,EAAE,EAAE;MAC9B,IAAI5C,IAAI,CAACT,IAAI,CAACgC,QAAQ,KAAK,IAAI,EAAE;QAC/BmB,KAAK,CAAC/B,IAAI,CAACX,IAAI,CAACyB,GAAG,CAAC,MAAM,CAAC,CAAC;QAC5BiB,KAAK,CAAC/B,IAAI,CAACX,IAAI,CAACyB,GAAG,CAAC,OAAO,CAAC,CAAC;MAC/B;IACF,CAAC,MAAM,IAAIzB,IAAI,CAAC6C,kBAAkB,EAAE,EAAE;MACpC,MAAMC,IAAI,GAAGxB,mCAAmC,CAAC1B,IAAI,EAAEI,IAAI,CAAC;MAC5D,IAAI8C,IAAI,EAAE7C,KAAK,CAACU,IAAI,CAACmC,IAAI,CAAC;IAC5B;EACF;EAEA,IAAI7C,KAAK,CAACW,MAAM,EAAE;IAChB,OAAO;MACLd,cAAc,EAAER,eAAe,CAACW,KAAK,CAAC;MACtCO;IACF,CAAC;EACH;EAEA,OAAOF,wBAAwB,CAACb,OAAO,EAAEe,WAAW,EAAEZ,IAAI,CAAC;AAC7D"}
1
+ {"version":3,"names":["_t","require","_util","BOOLEAN_NUMBER_BINARY_OPERATORS","createTypeAnnotationBasedOnTypeof","numberTypeAnnotation","voidTypeAnnotation","_default","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 };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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"}
@@ -1,4 +1,42 @@
1
- import * as _t from "@babel/types";
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.ArrayExpression = ArrayExpression;
7
+ exports.AssignmentExpression = AssignmentExpression;
8
+ exports.BinaryExpression = BinaryExpression;
9
+ exports.BooleanLiteral = BooleanLiteral;
10
+ exports.CallExpression = CallExpression;
11
+ exports.ConditionalExpression = ConditionalExpression;
12
+ exports.ClassDeclaration = exports.ClassExpression = exports.FunctionDeclaration = exports.ArrowFunctionExpression = exports.FunctionExpression = Func;
13
+ Object.defineProperty(exports, "Identifier", {
14
+ enumerable: true,
15
+ get: function () {
16
+ return _infererReference.default;
17
+ }
18
+ });
19
+ exports.LogicalExpression = LogicalExpression;
20
+ exports.NewExpression = NewExpression;
21
+ exports.NullLiteral = NullLiteral;
22
+ exports.NumericLiteral = NumericLiteral;
23
+ exports.ObjectExpression = ObjectExpression;
24
+ exports.ParenthesizedExpression = ParenthesizedExpression;
25
+ exports.RegExpLiteral = RegExpLiteral;
26
+ exports.RestElement = RestElement;
27
+ exports.SequenceExpression = SequenceExpression;
28
+ exports.StringLiteral = StringLiteral;
29
+ exports.TSAsExpression = TSAsExpression;
30
+ exports.TSNonNullExpression = TSNonNullExpression;
31
+ exports.TaggedTemplateExpression = TaggedTemplateExpression;
32
+ exports.TemplateLiteral = TemplateLiteral;
33
+ exports.TypeCastExpression = TypeCastExpression;
34
+ exports.UnaryExpression = UnaryExpression;
35
+ exports.UpdateExpression = UpdateExpression;
36
+ exports.VariableDeclarator = VariableDeclarator;
37
+ var _t = require("@babel/types");
38
+ var _infererReference = require("./inferer-reference");
39
+ var _util = require("./util");
2
40
  const {
3
41
  BOOLEAN_BINARY_OPERATORS,
4
42
  BOOLEAN_UNARY_OPERATORS,
@@ -19,32 +57,30 @@ const {
19
57
  voidTypeAnnotation,
20
58
  isIdentifier
21
59
  } = _t;
22
- export { default as Identifier } from "./inferer-reference.js";
23
- import { createUnionType } from "./util.js";
24
- export function VariableDeclarator() {
60
+ function VariableDeclarator() {
25
61
  if (!this.get("id").isIdentifier()) return;
26
62
  return this.get("init").getTypeAnnotation();
27
63
  }
28
- export function TypeCastExpression(node) {
64
+ function TypeCastExpression(node) {
29
65
  return node.typeAnnotation;
30
66
  }
31
67
  TypeCastExpression.validParent = true;
32
- export function TSAsExpression(node) {
68
+ function TSAsExpression(node) {
33
69
  return node.typeAnnotation;
34
70
  }
35
71
  TSAsExpression.validParent = true;
36
- export function TSNonNullExpression() {
72
+ function TSNonNullExpression() {
37
73
  return this.get("expression").getTypeAnnotation();
38
74
  }
39
- export function NewExpression(node) {
75
+ function NewExpression(node) {
40
76
  if (node.callee.type === "Identifier") {
41
77
  return genericTypeAnnotation(node.callee);
42
78
  }
43
79
  }
44
- export function TemplateLiteral() {
80
+ function TemplateLiteral() {
45
81
  return stringTypeAnnotation();
46
82
  }
47
- export function UnaryExpression(node) {
83
+ function UnaryExpression(node) {
48
84
  const operator = node.operator;
49
85
  if (operator === "void") {
50
86
  return voidTypeAnnotation();
@@ -56,7 +92,7 @@ export function UnaryExpression(node) {
56
92
  return booleanTypeAnnotation();
57
93
  }
58
94
  }
59
- export function BinaryExpression(node) {
95
+ function BinaryExpression(node) {
60
96
  const operator = node.operator;
61
97
  if (NUMBER_BINARY_OPERATORS.indexOf(operator) >= 0) {
62
98
  return numberTypeAnnotation();
@@ -73,63 +109,62 @@ export function BinaryExpression(node) {
73
109
  return unionTypeAnnotation([stringTypeAnnotation(), numberTypeAnnotation()]);
74
110
  }
75
111
  }
76
- export function LogicalExpression() {
112
+ function LogicalExpression() {
77
113
  const argumentTypes = [this.get("left").getTypeAnnotation(), this.get("right").getTypeAnnotation()];
78
- return createUnionType(argumentTypes);
114
+ return (0, _util.createUnionType)(argumentTypes);
79
115
  }
80
- export function ConditionalExpression() {
116
+ function ConditionalExpression() {
81
117
  const argumentTypes = [this.get("consequent").getTypeAnnotation(), this.get("alternate").getTypeAnnotation()];
82
- return createUnionType(argumentTypes);
118
+ return (0, _util.createUnionType)(argumentTypes);
83
119
  }
84
- export function SequenceExpression() {
120
+ function SequenceExpression() {
85
121
  return this.get("expressions").pop().getTypeAnnotation();
86
122
  }
87
- export function ParenthesizedExpression() {
123
+ function ParenthesizedExpression() {
88
124
  return this.get("expression").getTypeAnnotation();
89
125
  }
90
- export function AssignmentExpression() {
126
+ function AssignmentExpression() {
91
127
  return this.get("right").getTypeAnnotation();
92
128
  }
93
- export function UpdateExpression(node) {
129
+ function UpdateExpression(node) {
94
130
  const operator = node.operator;
95
131
  if (operator === "++" || operator === "--") {
96
132
  return numberTypeAnnotation();
97
133
  }
98
134
  }
99
- export function StringLiteral() {
135
+ function StringLiteral() {
100
136
  return stringTypeAnnotation();
101
137
  }
102
- export function NumericLiteral() {
138
+ function NumericLiteral() {
103
139
  return numberTypeAnnotation();
104
140
  }
105
- export function BooleanLiteral() {
141
+ function BooleanLiteral() {
106
142
  return booleanTypeAnnotation();
107
143
  }
108
- export function NullLiteral() {
144
+ function NullLiteral() {
109
145
  return nullLiteralTypeAnnotation();
110
146
  }
111
- export function RegExpLiteral() {
147
+ function RegExpLiteral() {
112
148
  return genericTypeAnnotation(identifier("RegExp"));
113
149
  }
114
- export function ObjectExpression() {
150
+ function ObjectExpression() {
115
151
  return genericTypeAnnotation(identifier("Object"));
116
152
  }
117
- export function ArrayExpression() {
153
+ function ArrayExpression() {
118
154
  return genericTypeAnnotation(identifier("Array"));
119
155
  }
120
- export function RestElement() {
156
+ function RestElement() {
121
157
  return ArrayExpression();
122
158
  }
123
159
  RestElement.validParent = true;
124
160
  function Func() {
125
161
  return genericTypeAnnotation(identifier("Function"));
126
162
  }
127
- export { Func as FunctionExpression, Func as ArrowFunctionExpression, Func as FunctionDeclaration, Func as ClassExpression, Func as ClassDeclaration };
128
163
  const isArrayFrom = buildMatchMemberExpression("Array.from");
129
164
  const isObjectKeys = buildMatchMemberExpression("Object.keys");
130
165
  const isObjectValues = buildMatchMemberExpression("Object.values");
131
166
  const isObjectEntries = buildMatchMemberExpression("Object.entries");
132
- export function CallExpression() {
167
+ function CallExpression() {
133
168
  const {
134
169
  callee
135
170
  } = this.node;
@@ -144,7 +179,7 @@ export function CallExpression() {
144
179
  }
145
180
  return resolveCall(this.get("callee"));
146
181
  }
147
- export function TaggedTemplateExpression() {
182
+ function TaggedTemplateExpression() {
148
183
  return resolveCall(this.get("tag"));
149
184
  }
150
185
  function resolveCall(callee) {
@@ -1 +1 @@
1
- {"version":3,"names":["_t","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","default","Identifier","createUnionType","VariableDeclarator","get","getTypeAnnotation","TypeCastExpression","node","typeAnnotation","validParent","TSAsExpression","TSNonNullExpression","NewExpression","callee","type","TemplateLiteral","UnaryExpression","operator","indexOf","BinaryExpression","right","left","isBaseType","LogicalExpression","argumentTypes","ConditionalExpression","SequenceExpression","pop","ParenthesizedExpression","AssignmentExpression","UpdateExpression","StringLiteral","NumericLiteral","BooleanLiteral","NullLiteral","RegExpLiteral","ObjectExpression","ArrayExpression","RestElement","Func","FunctionExpression","ArrowFunctionExpression","FunctionDeclaration","ClassExpression","ClassDeclaration","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,YAAAA,EAAA,MAmBO,cAAc;AAAC;EAlBpBC,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,IAAAlB,EAAA;AAId,SAASmB,OAAO,IAAIC,UAAU,QAAQ,wBAAqB;AAE3D,SAASC,eAAe,QAAQ,WAAQ;AAGxC,OAAO,SAASC,kBAAkBA,CAAA,EAAuC;EACvE,IAAI,CAAC,IAAI,CAACC,GAAG,CAAC,IAAI,CAAC,CAACL,YAAY,EAAE,EAAE;EACpC,OAAO,IAAI,CAACK,GAAG,CAAC,MAAM,CAAC,CAACC,iBAAiB,EAAE;AAC7C;AAEA,OAAO,SAASC,kBAAkBA,CAACC,IAA0B,EAAE;EAC7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ACA,OAAOD,WAAW,CAAC,IAAI,CAAC/C,GAAG,CAAC,KAAK,CAAC,CAAC;AACrC;AAEA,SAAS+C,WAAWA,CAACtC,MAAgB,EAAE;EACrCA,MAAM,GAAGA,MAAM,CAACwC,OAAO,EAAE;EAEzB,IAAIxC,MAAM,CAACyC,UAAU,EAAE,EAAE;IACvB,MAAM;MAAE/C;IAAK,CAAC,GAAGM,MAAM;IACvB,IAAIN,IAAI,CAACgD,KAAK,EAAE;MACd,IAAIhD,IAAI,CAACiD,SAAS,EAAE;QAClB,OAAOjE,qBAAqB,CAACC,UAAU,CAAC,eAAe,CAAC,CAAC;MAC3D,CAAC,MAAM;QACL,OAAOD,qBAAqB,CAACC,UAAU,CAAC,SAAS,CAAC,CAAC;MACrD;IACF,CAAC,MAAM;MACL,IAAIe,IAAI,CAACiD,SAAS,EAAE;QAClB,OAAOjE,qBAAqB,CAACC,UAAU,CAAC,UAAU,CAAC,CAAC;MACtD,CAAC,MAAM,IAAIqB,MAAM,CAACN,IAAI,CAACkD,UAAU,EAAE;QACjC,OAAO5C,MAAM,CAACN,IAAI,CAACkD,UAAU;MAC/B,CAAC,MAAM,CAEP;IACF;EACF;AACF"}
1
+ {"version":3,"names":["_t","require","_infererReference","_util","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,IAAAA,EAAA,GAAAC,OAAA;AAsBA,IAAAC,iBAAA,GAAAD,OAAA;AAEA,IAAAE,KAAA,GAAAF,OAAA;AAAyC;EAvBvCG,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,IAAArB,EAAA;AASP,SAASsB,kBAAkBA,CAAA,EAAuC;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,kBAAkBA,CAACC,IAA0B,EAAE;EAC7D,OAAOA,IAAI,CAACC,cAAc;AAC5B;AAEAF,kBAAkB,CAACG,WAAW,GAAG,IAAI;AAE9B,SAASC,cAAcA,CAACH,IAAsB,EAAE;EACrD,OAAOA,IAAI,CAACC,cAAc;AAC5B;AAEAE,cAAc,CAACD,WAAW,GAAG,IAAI;AAE1B,S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AC,CAAC;MACrD;IACF,CAAC,MAAM;MACL,IAAIY,IAAI,CAAC6C,SAAS,EAAE;QAClB,OAAO1D,qBAAqB,CAACC,UAAU,CAAC,UAAU,CAAC,CAAC;MACtD,CAAC,MAAM,IAAIkB,MAAM,CAACN,IAAI,CAAC8C,UAAU,EAAE;QACjC,OAAOxC,MAAM,CAACN,IAAI,CAAC8C,UAAU;MAC/B,CAAC,MAAM,CAEP;IACF;EACF;AACF"}
@@ -1,4 +1,10 @@
1
- import * as _t from "@babel/types";
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.createUnionType = createUnionType;
7
+ var _t = require("@babel/types");
2
8
  const {
3
9
  createFlowUnionType,
4
10
  createTSUnionType,
@@ -6,7 +12,7 @@ const {
6
12
  isFlowType,
7
13
  isTSType
8
14
  } = _t;
9
- export function createUnionType(types) {
15
+ function createUnionType(types) {
10
16
  {
11
17
  if (isFlowType(types[0])) {
12
18
  if (createFlowUnionType) {
@@ -1 +1 @@
1
- {"version":3,"names":["_t","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,YAAAA,EAAA,MAMO,cAAc;AAAC;EALpBC,mBAAmB;EACnBC,iBAAiB;EACjBC,yBAAyB;EACzBC,UAAU;EACVC;AAAQ,IAAAL,EAAA;AAIV,OAAO,SAASM,eAAeA,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"}
1
+ {"version":3,"names":["_t","require","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,IAAAA,EAAA,GAAAC,OAAA;AAMsB;EALpBC,mBAAmB;EACnBC,iBAAiB;EACjBC,yBAAyB;EACzBC,UAAU;EACVC;AAAQ,IAAAN,EAAA;AAIH,SAASO,eAAeA,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"}
@@ -1,4 +1,28 @@
1
- import * as _t from "@babel/types";
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");
2
26
  const {
3
27
  STATEMENT_OR_BLOCK_KEYS,
4
28
  VISITOR_KEYS,
@@ -10,10 +34,10 @@ const {
10
34
  isType,
11
35
  matchesPattern: _matchesPattern
12
36
  } = _t;
13
- export function matchesPattern(pattern, allowPartial) {
37
+ function matchesPattern(pattern, allowPartial) {
14
38
  return _matchesPattern(this.node, pattern, allowPartial);
15
39
  }
16
- export function has(key) {
40
+ function has(key) {
17
41
  const val = this.node && this.node[key];
18
42
  if (val && Array.isArray(val)) {
19
43
  return !!val.length;
@@ -21,23 +45,24 @@ export function has(key) {
21
45
  return !!val;
22
46
  }
23
47
  }
24
- export function isStatic() {
48
+ function isStatic() {
25
49
  return this.scope.isStatic(this.node);
26
50
  }
27
- export const is = has;
28
- export function isnt(key) {
51
+ const is = has;
52
+ exports.is = is;
53
+ function isnt(key) {
29
54
  return !this.has(key);
30
55
  }
31
- export function equals(key, value) {
56
+ function equals(key, value) {
32
57
  return this.node[key] === value;
33
58
  }
34
- export function isNodeType(type) {
59
+ function isNodeType(type) {
35
60
  return isType(this.type, type);
36
61
  }
37
- export function canHaveVariableDeclarationOrExpression() {
62
+ function canHaveVariableDeclarationOrExpression() {
38
63
  return (this.key === "init" || this.key === "left") && this.parentPath.isFor();
39
64
  }
40
- export function canSwapBetweenExpressionAndStatement(replacement) {
65
+ function canSwapBetweenExpressionAndStatement(replacement) {
41
66
  if (this.key !== "body" || !this.parentPath.isArrowFunctionExpression()) {
42
67
  return false;
43
68
  }
@@ -48,7 +73,7 @@ export function canSwapBetweenExpressionAndStatement(replacement) {
48
73
  }
49
74
  return false;
50
75
  }
51
- export function isCompletionRecord(allowInsideFunction) {
76
+ function isCompletionRecord(allowInsideFunction) {
52
77
  let path = this;
53
78
  let first = true;
54
79
  do {
@@ -66,14 +91,14 @@ export function isCompletionRecord(allowInsideFunction) {
66
91
  } while ((path = path.parentPath) && !path.isProgram() && !path.isDoExpression());
67
92
  return true;
68
93
  }
69
- export function isStatementOrBlock() {
94
+ function isStatementOrBlock() {
70
95
  if (this.parentPath.isLabeledStatement() || isBlockStatement(this.container)) {
71
96
  return false;
72
97
  } else {
73
98
  return STATEMENT_OR_BLOCK_KEYS.includes(this.key);
74
99
  }
75
100
  }
76
- export function referencesImport(moduleSource, importName) {
101
+ function referencesImport(moduleSource, importName) {
77
102
  if (!this.isReferencedIdentifier()) {
78
103
  if (this.isJSXMemberExpression() && this.node.property.name === importName || (this.isMemberExpression() || this.isOptionalMemberExpression()) && (this.node.computed ? isStringLiteral(this.node.property, {
79
104
  value: importName
@@ -106,7 +131,7 @@ export function referencesImport(moduleSource, importName) {
106
131
  }
107
132
  return false;
108
133
  }
109
- export function getSource() {
134
+ function getSource() {
110
135
  const node = this.node;
111
136
  if (node.end) {
112
137
  const code = this.hub.getCode();
@@ -114,7 +139,7 @@ export function getSource() {
114
139
  }
115
140
  return "";
116
141
  }
117
- export function willIMaybeExecuteBefore(target) {
142
+ function willIMaybeExecuteBefore(target) {
118
143
  return this._guessExecutionStatusRelativeTo(target) !== "after";
119
144
  }
120
145
  function getOuterFunction(path) {
@@ -158,7 +183,7 @@ function isExecutionUncertainInList(paths, maxIndex) {
158
183
  return false;
159
184
  }
160
185
  const SYMBOL_CHECKING = Symbol();
161
- export function _guessExecutionStatusRelativeTo(target) {
186
+ function _guessExecutionStatusRelativeTo(target) {
162
187
  return _guessExecutionStatusRelativeToCached(this, target, new Map());
163
188
  }
164
189
  function _guessExecutionStatusRelativeToCached(base, target, cache) {
@@ -253,10 +278,10 @@ function _guessExecutionStatusRelativeToDifferentFunctionsCached(base, target, c
253
278
  nodeMap.set(target.node, result);
254
279
  return result;
255
280
  }
256
- export function resolve(dangerous, resolved) {
281
+ function resolve(dangerous, resolved) {
257
282
  return this._resolve(dangerous, resolved) || this;
258
283
  }
259
- export function _resolve(dangerous, resolved) {
284
+ function _resolve(dangerous, resolved) {
260
285
  if (resolved && resolved.indexOf(this) >= 0) return;
261
286
  resolved = resolved || [];
262
287
  resolved.push(this);
@@ -301,7 +326,7 @@ export function _resolve(dangerous, resolved) {
301
326
  }
302
327
  }
303
328
  }
304
- export function isConstantExpression() {
329
+ function isConstantExpression() {
305
330
  if (this.isIdentifier()) {
306
331
  const binding = this.scope.getBinding(this.node.name);
307
332
  if (!binding) return false;
@@ -330,7 +355,7 @@ export function isConstantExpression() {
330
355
  }
331
356
  return false;
332
357
  }
333
- export function isInStrictMode() {
358
+ function isInStrictMode() {
334
359
  const start = this.isProgram() ? this : this.parentPath;
335
360
  const strictParent = start.find(path => {
336
361
  if (path.isProgram({