| 1 | // this file contains hooks that handle ancestry cleanup of parent nodes when removing children |
| 2 | |
| 3 | import type NodePathscip-typescript npm @babel/traverse 7.20.13 src/path/`index.ts`/NodePath# from ".."scip-typescript npm @babel/traverse 7.20.13 src/path/`index.ts`/; |
| 4 | 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`/; |
| 5 | /** |
| 6 | * Pre hooks should be used for either rejecting removal or delegating removal |
| 7 | */ |
| 8 | |
| 9 | export const hooksscip-typescript npm @babel/traverse 7.20.13 src/path/lib/`removal-hooks.ts`/hooks. = [ |
| 10 | function (self: NodePathscip-typescript npm @babel/traverse 7.20.13 src/path/`index.ts`/NodePath#, parent: NodePathscip-typescript npm @babel/traverse 7.20.13 src/path/`index.ts`/NodePath#) { |
| 11 | const removeParent = |
| 12 | // while (NODE); |
| 13 | // removing the test of a while/switch, we can either just remove it entirely *or* turn the |
| 14 | // `test` into `true` unlikely that the latter will ever be what's wanted so we just remove |
| 15 | // the loop to avoid infinite recursion |
| 16 | (self.keyscip-typescript npm @babel/traverse 7.20.13 src/path/`index.ts`/NodePath#key. === "test" && (parent.isWhilescip-typescript npm @babel/traverse 7.20.13 src/path/generated/`validators.d.ts`/BaseNodePathValidators#isWhile().() || parent.isSwitchCasescip-typescript npm @babel/traverse 7.20.13 src/path/generated/`validators.d.ts`/BaseNodePathValidators#isSwitchCase().())) || |
| 17 | // export NODE; |
| 18 | // just remove a declaration for an export as this is no longer valid |
| 19 | (self.keyscip-typescript npm @babel/traverse 7.20.13 src/path/`index.ts`/NodePath#key. === "declaration" && parent.isExportDeclarationscip-typescript npm @babel/traverse 7.20.13 src/path/generated/`validators.d.ts`/BaseNodePathValidators#isExportDeclaration().()) || |
| 20 | // label: NODE |
| 21 | // stray labeled statement with no body |
| 22 | (self.keyscip-typescript npm @babel/traverse 7.20.13 src/path/`index.ts`/NodePath#key. === "body" && parent.isLabeledStatementscip-typescript npm @babel/traverse 7.20.13 src/path/generated/`validators.d.ts`/BaseNodePathValidators#isLabeledStatement().()) || |
| 23 | // let NODE; |
| 24 | // remove an entire declaration if there are no declarators left |
| 25 | (self.listKeyscip-typescript npm @babel/traverse 7.20.13 src/path/`index.ts`/NodePath#listKey. === "declarations" && |
| 26 | parent.isVariableDeclarationscip-typescript npm @babel/traverse 7.20.13 src/path/generated/`validators.d.ts`/BaseNodePathValidators#isVariableDeclaration().() && |
| 27 | parent.nodescip-typescript npm @babel/traverse 7.20.13 src/path/`index.ts`/NodePath#node..declarationsscip-typescript npm @babel/types 7.20.7 src/ast-types/generated/`index.ts`/VariableDeclaration#declarations..lengthscip-typescript npm typescript 5.9.3 lib/`lib.es5.d.ts`/Array#length. === 1) || |
| 28 | // NODE; |
| 29 | // remove the entire expression statement if there's no expression |
| 30 | (self.keyscip-typescript npm @babel/traverse 7.20.13 src/path/`index.ts`/NodePath#key. === "expression" && parent.isExpressionStatementscip-typescript npm @babel/traverse 7.20.13 src/path/generated/`validators.d.ts`/BaseNodePathValidators#isExpressionStatement().()); |
| 31 | |
| 32 | if (removeParent) { |
| 33 | parent.removescip-typescript npm @babel/traverse 7.20.13 src/path/`removal.ts`/remove().(); |
| 34 | return true; |
| 35 | } |
| 36 | }, |
| 37 | |
| 38 | function (self: NodePathscip-typescript npm @babel/traverse 7.20.13 src/path/`index.ts`/NodePath#, parent: NodePathscip-typescript npm @babel/traverse 7.20.13 src/path/`index.ts`/NodePath#) { |
| 39 | if (parent.isSequenceExpressionscip-typescript npm @babel/traverse 7.20.13 src/path/generated/`validators.d.ts`/BaseNodePathValidators#isSequenceExpression().() && parent.nodescip-typescript npm @babel/traverse 7.20.13 src/path/`index.ts`/NodePath#node..expressionsscip-typescript npm @babel/types 7.20.7 src/ast-types/generated/`index.ts`/SequenceExpression#expressions..lengthscip-typescript npm typescript 5.9.3 lib/`lib.es5.d.ts`/Array#length. === 1) { |
| 40 | // (node, NODE); |
| 41 | // we've just removed the second element of a sequence expression so let's turn that sequence |
| 42 | // expression into a regular expression |
| 43 | parent.replaceWithscip-typescript npm @babel/traverse 7.20.13 src/path/`replacement.ts`/replaceWith().(parent.nodescip-typescript npm @babel/traverse 7.20.13 src/path/`index.ts`/NodePath#node..expressionsscip-typescript npm @babel/types 7.20.7 src/ast-types/generated/`index.ts`/SequenceExpression#expressions.[0]); |
| 44 | return true; |
| 45 | } |
| 46 | }, |
| 47 | |
| 48 | function (self: NodePathscip-typescript npm @babel/traverse 7.20.13 src/path/`index.ts`/NodePath#, parent: NodePathscip-typescript npm @babel/traverse 7.20.13 src/path/`index.ts`/NodePath#) { |
| 49 | if (parent.isBinaryscip-typescript npm @babel/traverse 7.20.13 src/path/generated/`validators.d.ts`/BaseNodePathValidators#isBinary().()) { |
| 50 | // left + NODE; |
| 51 | // NODE + right; |
| 52 | // we're in a binary expression, better remove it and replace it with the last expression |
| 53 | if (self.keyscip-typescript npm @babel/traverse 7.20.13 src/path/`index.ts`/NodePath#key. === "left") { |
| 54 | parent.replaceWithscip-typescript npm @babel/traverse 7.20.13 src/path/`replacement.ts`/replaceWith().(parent.nodescip-typescript npm @babel/traverse 7.20.13 src/path/`index.ts`/NodePath#node..rightscip-typescript npm @babel/types 7.20.7 src/ast-types/generated/`index.ts`/BinaryExpression#right.); |
| 55 | } else { |
| 56 | // key === "right" |
| 57 | parent.replaceWithscip-typescript npm @babel/traverse 7.20.13 src/path/`replacement.ts`/replaceWith().(parent.nodescip-typescript npm @babel/traverse 7.20.13 src/path/`index.ts`/NodePath#node..leftscip-typescript npm @babel/types 7.20.7 src/ast-types/generated/`index.ts`/BinaryExpression#left.); |
| 58 | } |
| 59 | return true; |
| 60 | } |
| 61 | }, |
| 62 | |
| 63 | function (self: NodePathscip-typescript npm @babel/traverse 7.20.13 src/path/`index.ts`/NodePath#, parent: NodePathscip-typescript npm @babel/traverse 7.20.13 src/path/`index.ts`/NodePath#) { |
| 64 | if ( |
| 65 | (parent.isIfStatementscip-typescript npm @babel/traverse 7.20.13 src/path/generated/`validators.d.ts`/BaseNodePathValidators#isIfStatement().() && self.keyscip-typescript npm @babel/traverse 7.20.13 src/path/`index.ts`/NodePath#key. === "consequent") || |
| 66 | (self.keyscip-typescript npm @babel/traverse 7.20.13 src/path/`index.ts`/NodePath#key. === "body" && |
| 67 | (parent.isLoopscip-typescript npm @babel/traverse 7.20.13 src/path/generated/`validators.d.ts`/BaseNodePathValidators#isLoop().() || parent.isArrowFunctionExpressionscip-typescript npm @babel/traverse 7.20.13 src/path/generated/`validators.d.ts`/BaseNodePathValidators#isArrowFunctionExpression().())) |
| 68 | ) { |
| 69 | self.replaceWithscip-typescript npm @babel/traverse 7.20.13 src/path/`replacement.ts`/replaceWith().({ |
| 70 | typescip-typescript npm @babel/types 7.20.7 src/ast-types/generated/`index.ts`/BlockStatement#type.: "BlockStatement", |
| 71 | bodyscip-typescript npm @babel/types 7.20.7 src/ast-types/generated/`index.ts`/BlockStatement#body.: [], |
| 72 | } as tscip-typescript npm @babel/types 7.20.7 src/`index.ts`/.BlockStatementscip-typescript npm @babel/types 7.20.7 src/ast-types/generated/`index.ts`/BlockStatement#); |
| 73 | return true; |
| 74 | } |
| 75 | }, |
| 76 | ]; |