| 1 | /* @noflow */ |
| 2 | |
| 3 | import type { NodePathscip-typescript npm @babel/traverse 7.20.13 src/path/`index.ts`/NodePath# } from "@babel/traverse"scip-typescript npm @babel/traverse 7.20.13 src/`index.ts`/; |
| 4 | import wrapFunctionscip-typescript npm @babel/helper-wrap-function 7.20.5 src/`index.ts`/wrapFunction(). from "@babel/helper-wrap-function"scip-typescript npm @babel/helper-wrap-function 7.20.5 src/`index.ts`/; |
| 5 | import annotateAsPurescip-typescript npm @babel/helper-annotate-as-pure 7.18.6 src/`index.ts`/annotateAsPure(). from "@babel/helper-annotate-as-pure"scip-typescript npm @babel/helper-annotate-as-pure 7.18.6 src/`index.ts`/; |
| 6 | import environmentVisitorscip-typescript npm @babel/helper-environment-visitor 7.18.9 src/`index.ts`/visitor. from "@babel/helper-environment-visitor"scip-typescript npm @babel/helper-environment-visitor 7.18.9 src/`index.ts`/; |
| 7 | import { traversescip-typescript npm @babel/traverse 7.20.13 src/`index.ts`/traverse(). } from "@babel/core"scip-typescript npm @babel/core 7.20.12 src/`index.ts`/; |
| 8 | import { |
| 9 | callExpressionscip-typescript npm @babel/types 7.20.7 src/builders/generated/`index.ts`/callExpression()., |
| 10 | cloneNodescip-typescript npm @babel/types 7.20.7 src/clone/`cloneNode.ts`/cloneNode()., |
| 11 | isIdentifierscip-typescript npm @babel/types 7.20.7 src/validators/generated/`index.ts`/isIdentifier()., |
| 12 | isThisExpressionscip-typescript npm @babel/types 7.20.7 src/validators/generated/`index.ts`/isThisExpression()., |
| 13 | yieldExpressionscip-typescript npm @babel/types 7.20.7 src/builders/generated/`index.ts`/yieldExpression()., |
| 14 | } from "@babel/types"scip-typescript npm @babel/types 7.20.7 src/`index.ts`/; |
| 15 | import type * as tscip-typescript npm @babel/types 7.20.7 src/`index.ts`/ from "@babel/types"scip-typescript npm @babel/types 7.20.7 src/`index.ts`/; |
| 16 | |
| 17 | const awaitVisitorscip-typescript npm @babel/helper-remap-async-to-generator 7.18.9 src/`index.ts`/awaitVisitor. = traversescip-typescript npm @babel/traverse 7.20.13 src/`index.ts`/traverse()..visitors.mergescip-typescript npm @babel/traverse 7.20.13 src/`visitors.ts`/merge().<{ wrapAwait: tscip-typescript npm @babel/types 7.20.7 src/`index.ts`/.Expressionscip-typescript npm @babel/types 7.20.7 src/ast-types/generated/`index.ts`/Expression# }>([ |
| 18 | { |
| 19 | ArrowFunctionExpression(path) { |
| 20 | path.skipscip-typescript npm @babel/traverse 7.20.13 src/path/`context.ts`/skip().(); |
| 21 | }, |
| 22 | |
| 23 | AwaitExpression(path, { wrapAwait }) { |
| 24 | const argument = path.get("argument"); |
| 25 | |
| 26 | path.replaceWithscip-typescript npm @babel/traverse 7.20.13 src/path/`replacement.ts`/replaceWith().( |
| 27 | yieldExpressionscip-typescript npm @babel/types 7.20.7 src/builders/generated/`index.ts`/yieldExpression().( |
| 28 | wrapAwait |
| 29 | ? callExpressionscip-typescript npm @babel/types 7.20.7 src/builders/generated/`index.ts`/callExpression().(cloneNodescip-typescript npm @babel/types 7.20.7 src/clone/`cloneNode.ts`/cloneNode().(wrapAwait), [argument.nodescip-typescript npm @babel/traverse 7.20.13 src/path/`index.ts`/NodePath#node.]) |
| 30 | : argument.nodescip-typescript npm @babel/traverse 7.20.13 src/path/`index.ts`/NodePath#node., |
| 31 | ), |
| 32 | ); |
| 33 | }, |
| 34 | }, |
| 35 | environmentVisitorscip-typescript npm @babel/helper-environment-visitor 7.18.9 src/`index.ts`/visitor., |
| 36 | ]); |
| 37 | |
| 38 | export default function ( |
| 39 | path: NodePathscip-typescript npm @babel/traverse 7.20.13 src/path/`index.ts`/NodePath#<tscip-typescript npm @babel/types 7.20.7 src/`index.ts`/.Functionscip-typescript npm @babel/types 7.20.7 src/ast-types/generated/`index.ts`/Function#>, |
| 40 | helpers: { |
| 41 | wrapAsync: tscip-typescript npm @babel/types 7.20.7 src/`index.ts`/.Expressionscip-typescript npm @babel/types 7.20.7 src/ast-types/generated/`index.ts`/Expression#; |
| 42 | wrapAwait?: tscip-typescript npm @babel/types 7.20.7 src/`index.ts`/.Expressionscip-typescript npm @babel/types 7.20.7 src/ast-types/generated/`index.ts`/Expression#; |
| 43 | }, |
| 44 | noNewArrows?: boolean, |
| 45 | ignoreFunctionLength?: boolean, |
| 46 | ) { |
| 47 | path.traversescip-typescript npm @babel/traverse 7.20.13 src/path/`index.ts`/NodePath#traverse().(awaitVisitorscip-typescript npm @babel/helper-remap-async-to-generator 7.18.9 src/`index.ts`/awaitVisitor., { |
| 48 | wrapAwaitscip-typescript npm @babel/helper-remap-async-to-generator 7.18.9 src/`index.ts`/wrapAwait0:: helpers.wrapAwait, |
| 49 | }); |
| 50 | |
| 51 | const isIIFE = checkIsIIFE(path); |
| 52 | |
| 53 | path.nodescip-typescript npm @babel/traverse 7.20.13 src/path/`index.ts`/NodePath#node..asyncscip-typescript npm @babel/types 7.20.7 src/ast-types/generated/`index.ts`/ArrowFunctionExpression#async. = false; |
| 54 | path.nodescip-typescript npm @babel/traverse 7.20.13 src/path/`index.ts`/NodePath#node..generatorscip-typescript npm @babel/types 7.20.7 src/ast-types/generated/`index.ts`/ArrowFunctionExpression#generator. = true; |
| 55 | |
| 56 | wrapFunctionscip-typescript npm @babel/helper-wrap-function 7.20.5 src/`index.ts`/wrapFunction().( |
| 57 | path, |
| 58 | cloneNodescip-typescript npm @babel/types 7.20.7 src/clone/`cloneNode.ts`/cloneNode().(helpers.wrapAsync), |
| 59 | noNewArrows, |
| 60 | ignoreFunctionLength, |
| 61 | ); |
| 62 | |
| 63 | const isProperty = |
| 64 | path.isObjectMethodscip-typescript npm @babel/traverse 7.20.13 src/path/generated/`validators.d.ts`/BaseNodePathValidators#isObjectMethod().() || |
| 65 | path.isClassMethodscip-typescript npm @babel/traverse 7.20.13 src/path/generated/`validators.d.ts`/BaseNodePathValidators#isClassMethod().() || |
| 66 | path.parentPathscip-typescript npm @babel/traverse 7.20.13 src/path/`index.ts`/NodePath#parentPath..isObjectPropertyscip-typescript npm @babel/traverse 7.20.13 src/path/generated/`validators.d.ts`/BaseNodePathValidators#isObjectProperty().() || |
| 67 | path.parentPathscip-typescript npm @babel/traverse 7.20.13 src/path/`index.ts`/NodePath#parentPath..isClassPropertyscip-typescript npm @babel/traverse 7.20.13 src/path/generated/`validators.d.ts`/BaseNodePathValidators#isClassProperty().(); |
| 68 | |
| 69 | if (!isProperty && !isIIFE && path.isExpressionscip-typescript npm @babel/traverse 7.20.13 src/path/generated/`validators.d.ts`/BaseNodePathValidators#isExpression().()) { |
| 70 | annotateAsPurescip-typescript npm @babel/helper-annotate-as-pure 7.18.6 src/`index.ts`/annotateAsPure().(path); |
| 71 | } |
| 72 | |
| 73 | function checkIsIIFE(path: NodePathscip-typescript npm @babel/traverse 7.20.13 src/path/`index.ts`/NodePath#) { |
| 74 | if (path.parentPathscip-typescript npm @babel/traverse 7.20.13 src/path/`index.ts`/NodePath#parentPath..isCallExpressionscip-typescript npm @babel/traverse 7.20.13 src/path/generated/`validators.d.ts`/BaseNodePathValidators#isCallExpression().({ calleescip-typescript npm @babel/helper-remap-async-to-generator 7.18.9 src/`index.ts`/callee0:: path.nodescip-typescript npm @babel/traverse 7.20.13 src/path/`index.ts`/NodePath#node. })) { |
| 75 | return true; |
| 76 | } |
| 77 | |
| 78 | // try to catch calls to Function#bind, as emitted by arrowFunctionToExpression in spec mode |
| 79 | // this may also catch .bind(this) written by users, but does it matter? 🤔 |
| 80 | const { parentPathscip-typescript npm @babel/traverse 7.20.13 src/path/`index.ts`/NodePath#parentPath. } = path; |
| 81 | if ( |
| 82 | parentPath.isMemberExpressionscip-typescript npm @babel/traverse 7.20.13 src/path/generated/`validators.d.ts`/BaseNodePathValidators#isMemberExpression().() && |
| 83 | isIdentifierscip-typescript npm @babel/types 7.20.7 src/validators/generated/`index.ts`/isIdentifier().(parentPath.nodescip-typescript npm @babel/traverse 7.20.13 src/path/`index.ts`/NodePath#node..propertyscip-typescript npm @babel/types 7.20.7 src/ast-types/generated/`index.ts`/MemberExpression#property., { namescip-typescript npm @babel/helper-remap-async-to-generator 7.18.9 src/`index.ts`/name0:: "bind" }) |
| 84 | ) { |
| 85 | const { parentPathscip-typescript npm @babel/traverse 7.20.13 src/path/`index.ts`/NodePath#parentPath.: bindCall } = parentPath; |
| 86 | |
| 87 | // (function () { ... }).bind(this)() |
| 88 | |
| 89 | return ( |
| 90 | // first, check if the .bind is actually being called |
| 91 | bindCall.isCallExpressionscip-typescript npm @babel/traverse 7.20.13 src/path/generated/`validators.d.ts`/BaseNodePathValidators#isCallExpression().() && |
| 92 | // and whether its sole argument is 'this' |
| 93 | bindCall.nodescip-typescript npm @babel/traverse 7.20.13 src/path/`index.ts`/NodePath#node..argumentsscip-typescript npm @babel/types 7.20.7 src/ast-types/generated/`index.ts`/CallExpression#arguments..lengthscip-typescript npm typescript 5.9.3 lib/`lib.es5.d.ts`/Array#length. === 1 && |
| 94 | isThisExpressionscip-typescript npm @babel/types 7.20.7 src/validators/generated/`index.ts`/isThisExpression().(bindCall.nodescip-typescript npm @babel/traverse 7.20.13 src/path/`index.ts`/NodePath#node..argumentsscip-typescript npm @babel/types 7.20.7 src/ast-types/generated/`index.ts`/CallExpression#arguments.[0]) && |
| 95 | // and whether the result of the .bind(this) is being called |
| 96 | bindCall.parentPathscip-typescript npm @babel/traverse 7.20.13 src/path/`index.ts`/NodePath#parentPath..isCallExpressionscip-typescript npm @babel/traverse 7.20.13 src/path/generated/`validators.d.ts`/BaseNodePathValidators#isCallExpression().({ calleescip-typescript npm @babel/helper-remap-async-to-generator 7.18.9 src/`index.ts`/callee1:: bindCall.nodescip-typescript npm @babel/traverse 7.20.13 src/path/`index.ts`/NodePath#node. }) |
| 97 | ); |
| 98 | } |
| 99 | |
| 100 | return false; |
| 101 | } |
| 102 | } |