@babel/traverse 7.22.8 → 8.0.0-alpha.0
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.
- package/lib/cache.js +7 -18
- package/lib/cache.js.map +1 -1
- package/lib/context.js +7 -14
- package/lib/context.js.map +1 -1
- package/lib/hub.js +1 -8
- package/lib/hub.js.map +1 -1
- package/lib/index.js +4846 -33
- package/lib/index.js.map +1 -1
- package/lib/path/ancestry.js +11 -26
- package/lib/path/ancestry.js.map +1 -1
- package/lib/path/comments.js +4 -12
- package/lib/path/comments.js.map +1 -1
- package/lib/path/context.js +35 -63
- package/lib/path/context.js.map +1 -1
- package/lib/path/conversion.js +18 -38
- package/lib/path/conversion.js.map +1 -1
- package/lib/path/evaluation.js +4 -11
- package/lib/path/evaluation.js.map +1 -1
- package/lib/path/family.js +19 -33
- package/lib/path/family.js.map +1 -1
- package/lib/path/index.js +42 -54
- package/lib/path/index.js.map +1 -1
- package/lib/path/inference/index.js +9 -21
- package/lib/path/inference/index.js.map +1 -1
- package/lib/path/inference/inferer-reference.js +5 -11
- package/lib/path/inference/inferer-reference.js.map +1 -1
- package/lib/path/inference/inferers.js +30 -65
- package/lib/path/inference/inferers.js.map +1 -1
- package/lib/path/inference/util.js +6 -16
- package/lib/path/inference/util.js.map +1 -1
- package/lib/path/introspection.js +20 -45
- package/lib/path/introspection.js.map +1 -1
- package/lib/path/lib/hoister.js +10 -17
- package/lib/path/lib/hoister.js.map +1 -1
- package/lib/path/lib/removal-hooks.js +1 -8
- package/lib/path/lib/removal-hooks.js.map +1 -1
- package/lib/path/lib/virtual-types-validator.js +19 -42
- package/lib/path/lib/virtual-types-validator.js.map +1 -1
- package/lib/path/lib/virtual-types.js +18 -42
- package/lib/path/lib/virtual-types.js.map +1 -1
- package/lib/path/modification.js +20 -35
- package/lib/path/modification.js.map +1 -1
- package/lib/path/removal.js +13 -25
- package/lib/path/removal.js.map +1 -1
- package/lib/path/replacement.js +25 -38
- package/lib/path/replacement.js.map +1 -1
- package/lib/scope/binding.js +10 -17
- package/lib/scope/binding.js.map +1 -1
- package/lib/scope/index.js +49 -66
- package/lib/scope/index.js.map +1 -1
- package/lib/scope/lib/renamer.js +12 -20
- package/lib/scope/lib/renamer.js.map +1 -1
- package/lib/traverse-node.js +4 -10
- package/lib/traverse-node.js.map +1 -1
- package/lib/visitors.js +16 -24
- package/lib/visitors.js.map +1 -1
- package/package.json +17 -13
@@ -1 +1 @@
|
|
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 };\n }\n\n return getConditionalAnnotation(binding, ifStatement, name);\n}\n"],"mappings":";;;;;;AACA,IAAAA,EAAA,GAAAC,OAAA;AASA,IAAAC,KAAA,GAAAD,OAAA;AAAyC;EARvCE,+BAA+B;EAC/BC,iCAAiC;EACjCC,oBAAoB;EACpBC;AAAkB,IAAAN,EAAA;AAOL,SAAAO,SAAwCC,IAAkB,EAAE;EACzE,IAAI,CAAC,IAAI,CAACC,YAAY,CAAC,CAAC,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,IACP,CAAC;IACH;EACF;EAGA,IAAIL,IAAI,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ACH;EAEA,OAAOF,wBAAwB,CAACb,OAAO,EAAEe,WAAW,EAAEZ,IAAI,CAAC;AAC7D"}
|
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,CAAC,CAAC,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,IACP,CAAC;IACH;EACF;EAGA,IAAIL,IAAI,CAACK,IAAI,KAAK,WAAW,EAAE;IAC7B,OAAOP,kBAAkB,CAAC,CAAC;EAC7B,CAAC,MAAM,IAAIE,IAAI,CAACK,IAAI,KAAK,KAAK,IAAIL,IAAI,CAACK,IAAI,KAAK,UAAU,EAAE;IAC1D,OAAOR,oBAAoB,CAAC,CAAC;EAC/B,CAAC,MAAM,IAAIG,IAA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EO,IAAI,EAAEJ,IAAI,CAAC;EACjE,IAAI,CAACY,WAAW,EAAE;EAElB,MAAMiC,IAAI,GAAGjC,WAAW,CAACiB,GAAG,CAAC,MAAM,CAAC;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,CAAC,CAAC,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,CAAC,CAAC,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,42 +1,4 @@
|
|
1
|
-
|
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");
|
1
|
+
import * as _t from "@babel/types";
|
40
2
|
const {
|
41
3
|
BOOLEAN_BINARY_OPERATORS,
|
42
4
|
BOOLEAN_UNARY_OPERATORS,
|
@@ -57,30 +19,32 @@ const {
|
|
57
19
|
voidTypeAnnotation,
|
58
20
|
isIdentifier
|
59
21
|
} = _t;
|
60
|
-
|
22
|
+
export { default as Identifier } from "./inferer-reference.js";
|
23
|
+
import { createUnionType } from "./util.js";
|
24
|
+
export function VariableDeclarator() {
|
61
25
|
if (!this.get("id").isIdentifier()) return;
|
62
26
|
return this.get("init").getTypeAnnotation();
|
63
27
|
}
|
64
|
-
function TypeCastExpression(node) {
|
28
|
+
export function TypeCastExpression(node) {
|
65
29
|
return node.typeAnnotation;
|
66
30
|
}
|
67
31
|
TypeCastExpression.validParent = true;
|
68
|
-
function TSAsExpression(node) {
|
32
|
+
export function TSAsExpression(node) {
|
69
33
|
return node.typeAnnotation;
|
70
34
|
}
|
71
35
|
TSAsExpression.validParent = true;
|
72
|
-
function TSNonNullExpression() {
|
36
|
+
export function TSNonNullExpression() {
|
73
37
|
return this.get("expression").getTypeAnnotation();
|
74
38
|
}
|
75
|
-
function NewExpression(node) {
|
39
|
+
export function NewExpression(node) {
|
76
40
|
if (node.callee.type === "Identifier") {
|
77
41
|
return genericTypeAnnotation(node.callee);
|
78
42
|
}
|
79
43
|
}
|
80
|
-
function TemplateLiteral() {
|
44
|
+
export function TemplateLiteral() {
|
81
45
|
return stringTypeAnnotation();
|
82
46
|
}
|
83
|
-
function UnaryExpression(node) {
|
47
|
+
export function UnaryExpression(node) {
|
84
48
|
const operator = node.operator;
|
85
49
|
if (operator === "void") {
|
86
50
|
return voidTypeAnnotation();
|
@@ -92,7 +56,7 @@ function UnaryExpression(node) {
|
|
92
56
|
return booleanTypeAnnotation();
|
93
57
|
}
|
94
58
|
}
|
95
|
-
function BinaryExpression(node) {
|
59
|
+
export function BinaryExpression(node) {
|
96
60
|
const operator = node.operator;
|
97
61
|
if (NUMBER_BINARY_OPERATORS.indexOf(operator) >= 0) {
|
98
62
|
return numberTypeAnnotation();
|
@@ -109,62 +73,63 @@ function BinaryExpression(node) {
|
|
109
73
|
return unionTypeAnnotation([stringTypeAnnotation(), numberTypeAnnotation()]);
|
110
74
|
}
|
111
75
|
}
|
112
|
-
function LogicalExpression() {
|
76
|
+
export function LogicalExpression() {
|
113
77
|
const argumentTypes = [this.get("left").getTypeAnnotation(), this.get("right").getTypeAnnotation()];
|
114
|
-
return
|
78
|
+
return createUnionType(argumentTypes);
|
115
79
|
}
|
116
|
-
function ConditionalExpression() {
|
80
|
+
export function ConditionalExpression() {
|
117
81
|
const argumentTypes = [this.get("consequent").getTypeAnnotation(), this.get("alternate").getTypeAnnotation()];
|
118
|
-
return
|
82
|
+
return createUnionType(argumentTypes);
|
119
83
|
}
|
120
|
-
function SequenceExpression() {
|
84
|
+
export function SequenceExpression() {
|
121
85
|
return this.get("expressions").pop().getTypeAnnotation();
|
122
86
|
}
|
123
|
-
function ParenthesizedExpression() {
|
87
|
+
export function ParenthesizedExpression() {
|
124
88
|
return this.get("expression").getTypeAnnotation();
|
125
89
|
}
|
126
|
-
function AssignmentExpression() {
|
90
|
+
export function AssignmentExpression() {
|
127
91
|
return this.get("right").getTypeAnnotation();
|
128
92
|
}
|
129
|
-
function UpdateExpression(node) {
|
93
|
+
export function UpdateExpression(node) {
|
130
94
|
const operator = node.operator;
|
131
95
|
if (operator === "++" || operator === "--") {
|
132
96
|
return numberTypeAnnotation();
|
133
97
|
}
|
134
98
|
}
|
135
|
-
function StringLiteral() {
|
99
|
+
export function StringLiteral() {
|
136
100
|
return stringTypeAnnotation();
|
137
101
|
}
|
138
|
-
function NumericLiteral() {
|
102
|
+
export function NumericLiteral() {
|
139
103
|
return numberTypeAnnotation();
|
140
104
|
}
|
141
|
-
function BooleanLiteral() {
|
105
|
+
export function BooleanLiteral() {
|
142
106
|
return booleanTypeAnnotation();
|
143
107
|
}
|
144
|
-
function NullLiteral() {
|
108
|
+
export function NullLiteral() {
|
145
109
|
return nullLiteralTypeAnnotation();
|
146
110
|
}
|
147
|
-
function RegExpLiteral() {
|
111
|
+
export function RegExpLiteral() {
|
148
112
|
return genericTypeAnnotation(identifier("RegExp"));
|
149
113
|
}
|
150
|
-
function ObjectExpression() {
|
114
|
+
export function ObjectExpression() {
|
151
115
|
return genericTypeAnnotation(identifier("Object"));
|
152
116
|
}
|
153
|
-
function ArrayExpression() {
|
117
|
+
export function ArrayExpression() {
|
154
118
|
return genericTypeAnnotation(identifier("Array"));
|
155
119
|
}
|
156
|
-
function RestElement() {
|
120
|
+
export function RestElement() {
|
157
121
|
return ArrayExpression();
|
158
122
|
}
|
159
123
|
RestElement.validParent = true;
|
160
124
|
function Func() {
|
161
125
|
return genericTypeAnnotation(identifier("Function"));
|
162
126
|
}
|
127
|
+
export { Func as FunctionExpression, Func as ArrowFunctionExpression, Func as FunctionDeclaration, Func as ClassExpression, Func as ClassDeclaration };
|
163
128
|
const isArrayFrom = buildMatchMemberExpression("Array.from");
|
164
129
|
const isObjectKeys = buildMatchMemberExpression("Object.keys");
|
165
130
|
const isObjectValues = buildMatchMemberExpression("Object.values");
|
166
131
|
const isObjectEntries = buildMatchMemberExpression("Object.entries");
|
167
|
-
function CallExpression() {
|
132
|
+
export function CallExpression() {
|
168
133
|
const {
|
169
134
|
callee
|
170
135
|
} = this.node;
|
@@ -179,7 +144,7 @@ function CallExpression() {
|
|
179
144
|
}
|
180
145
|
return resolveCall(this.get("callee"));
|
181
146
|
}
|
182
|
-
function TaggedTemplateExpression() {
|
147
|
+
export function TaggedTemplateExpression() {
|
183
148
|
return resolveCall(this.get("tag"));
|
184
149
|
}
|
185
150
|
function resolveCall(callee) {
|
@@ -1 +1 @@
|
|
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,CAAC,CAAC,EAAE;EACpC,OAAO,IAAI,CAACE,GAAG,CAAC,MAAM,CAAC,CAACC,iBAAiB,CAAC,CAAC;AAC7C;AAEO,SAASC,kBAAkBA,CAACC,IAA0B,EAAE;EAC7D,OAAOA,IAAI,CAACC,cAAc;AAC5B;AAEAF,kB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AAM,GAAGA,MAAM,CAACoC,OAAO,CAAC,CAAC;EAEzB,IAAIpC,MAAM,CAACqC,UAAU,CAAC,CAAC,EAAE;IACvB,MAAM;MAAE3C;IAAK,CAAC,GAAGM,MAAM;IACvB,IAAIN,IAAI,CAAC4C,KAAK,EAAE;MACd,IAAI5C,IAAI,CAAC6C,SAAS,EAAE;QAClB,OAAO1D,qBAAqB,CAACC,UAAU,CAAC,eAAe,CAAC,CAAC;MAC3D,CAAC,MAAM;QACL,OAAOD,qBAAqB,CAACC,UAAU,CAAC,SAAS,CAAC,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
|
+
{"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,CAAC,CAAC,EAAE;EACpC,OAAO,IAAI,CAACK,GAAG,CAAC,MAAM,CAAC,CAACC,iBAAiB,CAAC,CAAC;AAC7C;AAEA,OAAO,SAASC,kBAAkBA,CAACC,IAA0B,EAAE;EAC7D,OAAOA,IAAI,CAACC,cAAc;AAC5B;AAEAF,kBAAkB,CAACG,WAAW,GAAG,IAAI;AAErC,OAAO,SAASC,cAAcA,CAACH,IAAsB,EAAE;EACrD,OAAOA,IAAI,CAACC,cAAc;AAC5B;AAEAE,cAAc,CAACD,WAAW,GAAG,IAAI;AAEjC,OAAO,SAASE,mBAAmBA,CAAA,EAAwC;EACzE,OAAO,IAA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;AAEA,SAAS+C,WAAWA,CAACtC,MAAgB,EAAE;EACrCA,MAAM,GAAGA,MAAM,CAACwC,OAAO,CAAC,CAAC;EAEzB,IAAIxC,MAAM,CAACyC,UAAU,CAAC,CAAC,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,10 +1,4 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
Object.defineProperty(exports, "__esModule", {
|
4
|
-
value: true
|
5
|
-
});
|
6
|
-
exports.createUnionType = createUnionType;
|
7
|
-
var _t = require("@babel/types");
|
1
|
+
import * as _t from "@babel/types";
|
8
2
|
const {
|
9
3
|
createFlowUnionType,
|
10
4
|
createTSUnionType,
|
@@ -12,17 +6,13 @@ const {
|
|
12
6
|
isFlowType,
|
13
7
|
isTSType
|
14
8
|
} = _t;
|
15
|
-
function createUnionType(types) {
|
9
|
+
export function createUnionType(types) {
|
16
10
|
{
|
17
11
|
if (isFlowType(types[0])) {
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
return
|
22
|
-
} else {
|
23
|
-
if (createTSUnionType) {
|
24
|
-
return createTSUnionType(types);
|
25
|
-
}
|
12
|
+
return createFlowUnionType(types);
|
13
|
+
}
|
14
|
+
if (isTSType(types[0])) {
|
15
|
+
return createTSUnionType(types);
|
26
16
|
}
|
27
17
|
}
|
28
18
|
}
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"names":["_t","
|
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;EACW;IAChC,IAAIH,UAAU,CAACG,KAAK,CAAC,CAAC,CAAC,CAAC,EAAE;MACxB,OAAON,mBAAmB,CAACM,KAAqB,CAAC;IACnD;IACA,IAAIF,QAAQ,CAACE,KAAK,CAAC,CAAC,CAAC,CAAC,EAAE;MACtB,OAAOL,iBAAiB,CAACK,KAAmB,CAAC;IAC/C;EACF;AAaF"}
|
@@ -1,28 +1,4 @@
|
|
1
|
-
|
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");
|
1
|
+
import * as _t from "@babel/types";
|
26
2
|
const {
|
27
3
|
STATEMENT_OR_BLOCK_KEYS,
|
28
4
|
VISITOR_KEYS,
|
@@ -34,10 +10,10 @@ const {
|
|
34
10
|
isType,
|
35
11
|
matchesPattern: _matchesPattern
|
36
12
|
} = _t;
|
37
|
-
function matchesPattern(pattern, allowPartial) {
|
13
|
+
export function matchesPattern(pattern, allowPartial) {
|
38
14
|
return _matchesPattern(this.node, pattern, allowPartial);
|
39
15
|
}
|
40
|
-
function has(key) {
|
16
|
+
export function has(key) {
|
41
17
|
const val = this.node && this.node[key];
|
42
18
|
if (val && Array.isArray(val)) {
|
43
19
|
return !!val.length;
|
@@ -45,24 +21,23 @@ function has(key) {
|
|
45
21
|
return !!val;
|
46
22
|
}
|
47
23
|
}
|
48
|
-
function isStatic() {
|
24
|
+
export function isStatic() {
|
49
25
|
return this.scope.isStatic(this.node);
|
50
26
|
}
|
51
|
-
const is = has;
|
52
|
-
|
53
|
-
function isnt(key) {
|
27
|
+
export const is = has;
|
28
|
+
export function isnt(key) {
|
54
29
|
return !this.has(key);
|
55
30
|
}
|
56
|
-
function equals(key, value) {
|
31
|
+
export function equals(key, value) {
|
57
32
|
return this.node[key] === value;
|
58
33
|
}
|
59
|
-
function isNodeType(type) {
|
34
|
+
export function isNodeType(type) {
|
60
35
|
return isType(this.type, type);
|
61
36
|
}
|
62
|
-
function canHaveVariableDeclarationOrExpression() {
|
37
|
+
export function canHaveVariableDeclarationOrExpression() {
|
63
38
|
return (this.key === "init" || this.key === "left") && this.parentPath.isFor();
|
64
39
|
}
|
65
|
-
function canSwapBetweenExpressionAndStatement(replacement) {
|
40
|
+
export function canSwapBetweenExpressionAndStatement(replacement) {
|
66
41
|
if (this.key !== "body" || !this.parentPath.isArrowFunctionExpression()) {
|
67
42
|
return false;
|
68
43
|
}
|
@@ -73,7 +48,7 @@ function canSwapBetweenExpressionAndStatement(replacement) {
|
|
73
48
|
}
|
74
49
|
return false;
|
75
50
|
}
|
76
|
-
function isCompletionRecord(allowInsideFunction) {
|
51
|
+
export function isCompletionRecord(allowInsideFunction) {
|
77
52
|
let path = this;
|
78
53
|
let first = true;
|
79
54
|
do {
|
@@ -91,14 +66,14 @@ function isCompletionRecord(allowInsideFunction) {
|
|
91
66
|
} while ((path = path.parentPath) && !path.isProgram() && !path.isDoExpression());
|
92
67
|
return true;
|
93
68
|
}
|
94
|
-
function isStatementOrBlock() {
|
69
|
+
export function isStatementOrBlock() {
|
95
70
|
if (this.parentPath.isLabeledStatement() || isBlockStatement(this.container)) {
|
96
71
|
return false;
|
97
72
|
} else {
|
98
73
|
return STATEMENT_OR_BLOCK_KEYS.includes(this.key);
|
99
74
|
}
|
100
75
|
}
|
101
|
-
function referencesImport(moduleSource, importName) {
|
76
|
+
export function referencesImport(moduleSource, importName) {
|
102
77
|
if (!this.isReferencedIdentifier()) {
|
103
78
|
if (this.isJSXMemberExpression() && this.node.property.name === importName || (this.isMemberExpression() || this.isOptionalMemberExpression()) && (this.node.computed ? isStringLiteral(this.node.property, {
|
104
79
|
value: importName
|
@@ -131,7 +106,7 @@ function referencesImport(moduleSource, importName) {
|
|
131
106
|
}
|
132
107
|
return false;
|
133
108
|
}
|
134
|
-
function getSource() {
|
109
|
+
export function getSource() {
|
135
110
|
const node = this.node;
|
136
111
|
if (node.end) {
|
137
112
|
const code = this.hub.getCode();
|
@@ -139,7 +114,7 @@ function getSource() {
|
|
139
114
|
}
|
140
115
|
return "";
|
141
116
|
}
|
142
|
-
function willIMaybeExecuteBefore(target) {
|
117
|
+
export function willIMaybeExecuteBefore(target) {
|
143
118
|
return this._guessExecutionStatusRelativeTo(target) !== "after";
|
144
119
|
}
|
145
120
|
function getOuterFunction(path) {
|
@@ -183,7 +158,7 @@ function isExecutionUncertainInList(paths, maxIndex) {
|
|
183
158
|
return false;
|
184
159
|
}
|
185
160
|
const SYMBOL_CHECKING = Symbol();
|
186
|
-
function _guessExecutionStatusRelativeTo(target) {
|
161
|
+
export function _guessExecutionStatusRelativeTo(target) {
|
187
162
|
return _guessExecutionStatusRelativeToCached(this, target, new Map());
|
188
163
|
}
|
189
164
|
function _guessExecutionStatusRelativeToCached(base, target, cache) {
|
@@ -278,10 +253,10 @@ function _guessExecutionStatusRelativeToDifferentFunctionsCached(base, target, c
|
|
278
253
|
nodeMap.set(target.node, result);
|
279
254
|
return result;
|
280
255
|
}
|
281
|
-
function resolve(dangerous, resolved) {
|
256
|
+
export function resolve(dangerous, resolved) {
|
282
257
|
return this._resolve(dangerous, resolved) || this;
|
283
258
|
}
|
284
|
-
function _resolve(dangerous, resolved) {
|
259
|
+
export function _resolve(dangerous, resolved) {
|
285
260
|
if (resolved && resolved.indexOf(this) >= 0) return;
|
286
261
|
resolved = resolved || [];
|
287
262
|
resolved.push(this);
|
@@ -326,7 +301,7 @@ function _resolve(dangerous, resolved) {
|
|
326
301
|
}
|
327
302
|
}
|
328
303
|
}
|
329
|
-
function isConstantExpression() {
|
304
|
+
export function isConstantExpression() {
|
330
305
|
if (this.isIdentifier()) {
|
331
306
|
const binding = this.scope.getBinding(this.node.name);
|
332
307
|
if (!binding) return false;
|
@@ -355,7 +330,7 @@ function isConstantExpression() {
|
|
355
330
|
}
|
356
331
|
return false;
|
357
332
|
}
|
358
|
-
function isInStrictMode() {
|
333
|
+
export function isInStrictMode() {
|
359
334
|
const start = this.isProgram() ? this : this.parentPath;
|
360
335
|
const strictParent = start.find(path => {
|
361
336
|
if (path.isProgram({
|