| 1 | 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`/; |
| 2 | |
| 3 | /** |
| 4 | * Test if a NodePath will be cast to boolean when evaluated. |
| 5 | * |
| 6 | * @example |
| 7 | * // returns true |
| 8 | * const nodePathAQDotB = NodePath("if (a?.#b) {}").get("test"); // a?.#b |
| 9 | * willPathCastToBoolean(nodePathAQDotB) |
| 10 | * @example |
| 11 | * // returns false |
| 12 | * willPathCastToBoolean(NodePath("a?.#b")) |
| 13 | * @todo Respect transparent expression wrappers |
| 14 | * @see {@link packages/babel-plugin-proposal-optional-chaining/src/util.js} |
| 15 | * @param {NodePath} path |
| 16 | * @returns {boolean} |
| 17 | */ |
| 18 | export function willPathCastToBooleanscip-typescript npm @babel/helper-member-expression-to-functions 7.20.7 src/`util.ts`/willPathCastToBoolean().(pathscip-typescript npm @babel/helper-member-expression-to-functions 7.20.7 src/`util.ts`/willPathCastToBoolean().(path): NodePathscip-typescript npm @babel/traverse 7.20.13 src/path/`index.ts`/NodePath#): boolean { |
| 19 | const maybeWrapped = pathscip-typescript npm @babel/helper-member-expression-to-functions 7.20.7 src/`util.ts`/willPathCastToBoolean().(path); |
| 20 | const { nodescip-typescript npm @babel/traverse 7.20.13 src/path/`index.ts`/NodePath#node., parentPathscip-typescript npm @babel/traverse 7.20.13 src/path/`index.ts`/NodePath#parentPath. } = maybeWrapped; |
| 21 | if (parentPath.isLogicalExpressionscip-typescript npm @babel/traverse 7.20.13 src/path/generated/`validators.d.ts`/BaseNodePathValidators#isLogicalExpression().()) { |
| 22 | const { operatorscip-typescript npm @babel/types 7.20.7 src/ast-types/generated/`index.ts`/LogicalExpression#operator., rightscip-typescript npm @babel/types 7.20.7 src/ast-types/generated/`index.ts`/LogicalExpression#right. } = parentPath.nodescip-typescript npm @babel/traverse 7.20.13 src/path/`index.ts`/NodePath#node.; |
| 23 | if ( |
| 24 | operator === "&&" || |
| 25 | operator === "||" || |
| 26 | (operator === "??" && node === right) |
| 27 | ) { |
| 28 | return willPathCastToBooleanscip-typescript npm @babel/helper-member-expression-to-functions 7.20.7 src/`util.ts`/willPathCastToBoolean().(parentPath); |
| 29 | } |
| 30 | } |
| 31 | if (parentPath.isSequenceExpressionscip-typescript npm @babel/traverse 7.20.13 src/path/generated/`validators.d.ts`/BaseNodePathValidators#isSequenceExpression().()) { |
| 32 | const { expressionsscip-typescript npm @babel/types 7.20.7 src/ast-types/generated/`index.ts`/SequenceExpression#expressions. } = parentPath.nodescip-typescript npm @babel/traverse 7.20.13 src/path/`index.ts`/NodePath#node.; |
| 33 | if (expressions[expressions.lengthscip-typescript npm typescript 5.9.3 lib/`lib.es5.d.ts`/Array#length. - 1] === node) { |
| 34 | return willPathCastToBooleanscip-typescript npm @babel/helper-member-expression-to-functions 7.20.7 src/`util.ts`/willPathCastToBoolean().(parentPath); |
| 35 | } else { |
| 36 | // if it is in the middle of a sequence expression, we don't |
| 37 | // care the return value so just cast to boolean for smaller |
| 38 | // output |
| 39 | return true; |
| 40 | } |
| 41 | } |
| 42 | return ( |
| 43 | parentPath.isConditionalscip-typescript npm @babel/traverse 7.20.13 src/path/generated/`validators.d.ts`/BaseNodePathValidators#isConditional().({ testscip-typescript npm @babel/helper-member-expression-to-functions 7.20.7 src/`util.ts`/test0:: node }) || |
| 44 | parentPath.isUnaryExpressionscip-typescript npm @babel/traverse 7.20.13 src/path/generated/`validators.d.ts`/BaseNodePathValidators#isUnaryExpression().({ operatorscip-typescript npm @babel/helper-member-expression-to-functions 7.20.7 src/`util.ts`/operator0:: "!" }) || |
| 45 | parentPath.isLoopscip-typescript npm @babel/traverse 7.20.13 src/path/generated/`validators.d.ts`/BaseNodePathValidators#isLoop().({ testscip-typescript npm @babel/helper-member-expression-to-functions 7.20.7 src/`util.ts`/test1:: node }) |
| 46 | ); |
| 47 | } |