| 1 | import { |
| 2 | LOGICAL_OPERATORSscip-typescript npm @babel/types 7.20.7 src/constants/`index.ts`/LOGICAL_OPERATORS., |
| 3 | assignmentExpressionscip-typescript npm @babel/types 7.20.7 src/builders/generated/`index.ts`/assignmentExpression()., |
| 4 | binaryExpressionscip-typescript npm @babel/types 7.20.7 src/builders/generated/`index.ts`/binaryExpression()., |
| 5 | cloneNodescip-typescript npm @babel/types 7.20.7 src/clone/`cloneNode.ts`/cloneNode()., |
| 6 | identifierscip-typescript npm @babel/types 7.20.7 src/builders/generated/`index.ts`/identifier()., |
| 7 | logicalExpressionscip-typescript npm @babel/types 7.20.7 src/builders/generated/`index.ts`/logicalExpression()., |
| 8 | numericLiteralscip-typescript npm @babel/types 7.20.7 src/builders/generated/`index.ts`/numericLiteral()., |
| 9 | sequenceExpressionscip-typescript npm @babel/types 7.20.7 src/builders/generated/`index.ts`/sequenceExpression()., |
| 10 | unaryExpressionscip-typescript npm @babel/types 7.20.7 src/builders/generated/`index.ts`/unaryExpression()., |
| 11 | } from "@babel/types"scip-typescript npm @babel/types 7.20.7 src/`index.ts`/; |
| 12 | 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`/; |
| 13 | import type { NodePathscip-typescript npm @babel/traverse 7.20.13 src/path/`index.ts`/NodePath#, Scopescip-typescript npm @babel/traverse 7.20.13 src/scope/`index.ts`/Scope#, Visitorscip-typescript npm @babel/traverse 7.20.13 src/`types.ts`/Visitor# } from "@babel/traverse"scip-typescript npm @babel/traverse 7.20.13 src/`index.ts`/; |
| 14 | |
| 15 | type Statescip-typescript npm @babel/helper-simple-access 7.20.2 src/`index.ts`/State# = { |
| 16 | scopescip-typescript npm @babel/helper-simple-access 7.20.2 src/`index.ts`/State#typeLiteral0:scope.: Scopescip-typescript npm @babel/traverse 7.20.13 src/scope/`index.ts`/Scope#; |
| 17 | bindingNamesscip-typescript npm @babel/helper-simple-access 7.20.2 src/`index.ts`/State#typeLiteral0:bindingNames.: Setscip-typescript npm typescript 5.9.3 lib/`lib.es2015.collection.d.ts`/Set#<string>; |
| 18 | seenscip-typescript npm @babel/helper-simple-access 7.20.2 src/`index.ts`/State#typeLiteral0:seen.: WeakSetscip-typescript npm typescript 5.9.3 lib/`lib.es2015.collection.d.ts`/WeakSet#<tscip-typescript npm @babel/types 7.20.7 src/`index.ts`/.Nodescip-typescript npm @babel/types 7.20.7 src/ast-types/generated/`index.ts`/Node#>; |
| 19 | includeUpdateExpressionscip-typescript npm @babel/helper-simple-access 7.20.2 src/`index.ts`/State#typeLiteral0:includeUpdateExpression.: boolean; |
| 20 | }; |
| 21 | |
| 22 | const simpleAssignmentVisitorscip-typescript npm @babel/helper-simple-access 7.20.2 src/`index.ts`/simpleAssignmentVisitor.: Visitorscip-typescript npm @babel/traverse 7.20.13 src/`types.ts`/Visitor#<Statescip-typescript npm @babel/helper-simple-access 7.20.2 src/`index.ts`/State#> = { |
| 23 | // TODO(Babel 8): Remove UpdateExpression |
| 24 | UpdateExpressionscip-typescript npm @babel/helper-simple-access 7.20.2 src/`index.ts`/UpdateExpression0:: { |
| 25 | exit(path) { |
| 26 | const { scopescip-typescript npm @babel/helper-simple-access 7.20.2 src/`index.ts`/State#typeLiteral0:scope., bindingNamesscip-typescript npm @babel/helper-simple-access 7.20.2 src/`index.ts`/State#typeLiteral0:bindingNames., includeUpdateExpressionscip-typescript npm @babel/helper-simple-access 7.20.2 src/`index.ts`/State#typeLiteral0:includeUpdateExpression. } = this; |
| 27 | if (!includeUpdateExpression) { |
| 28 | return; |
| 29 | } |
| 30 | |
| 31 | const arg = path.get("argument"); |
| 32 | if (!arg.isIdentifierscip-typescript npm @babel/traverse 7.20.13 src/path/generated/`validators.d.ts`/BaseNodePathValidators#isIdentifier().()) return; |
| 33 | const localName = arg.nodescip-typescript npm @babel/traverse 7.20.13 src/path/`index.ts`/NodePath#node..namescip-typescript npm @babel/types 7.20.7 src/ast-types/generated/`index.ts`/Identifier#name.; |
| 34 | |
| 35 | if (!bindingNames.hasscip-typescript npm typescript 5.9.3 lib/`lib.es2015.collection.d.ts`/Set#has().(localName)) return; |
| 36 | |
| 37 | // redeclared in this scope |
| 38 | if (scope.getBindingscip-typescript npm @babel/traverse 7.20.13 src/scope/`index.ts`/Scope#getBinding().(localName) !== path.scopescip-typescript npm @babel/traverse 7.20.13 src/path/`index.ts`/NodePath#scope..getBindingscip-typescript npm @babel/traverse 7.20.13 src/scope/`index.ts`/Scope#getBinding().(localName)) { |
| 39 | return; |
| 40 | } |
| 41 | |
| 42 | if ( |
| 43 | path.parentPathscip-typescript npm @babel/traverse 7.20.13 src/path/`index.ts`/NodePath#parentPath..isExpressionStatementscip-typescript npm @babel/traverse 7.20.13 src/path/generated/`validators.d.ts`/BaseNodePathValidators#isExpressionStatement().() && |
| 44 | !path.isCompletionRecordscip-typescript npm @babel/traverse 7.20.13 src/path/`introspection.ts`/isCompletionRecord().() |
| 45 | ) { |
| 46 | // ++i => (i += 1); |
| 47 | const operator = path.nodescip-typescript npm @babel/traverse 7.20.13 src/path/`index.ts`/NodePath#node..operatorscip-typescript npm @babel/types 7.20.7 src/ast-types/generated/`index.ts`/UpdateExpression#operator. == "++" ? "+=" : "-="; |
| 48 | path.replaceWithscip-typescript npm @babel/traverse 7.20.13 src/path/`replacement.ts`/replaceWith().( |
| 49 | assignmentExpressionscip-typescript npm @babel/types 7.20.7 src/builders/generated/`index.ts`/assignmentExpression().(operator, arg.nodescip-typescript npm @babel/traverse 7.20.13 src/path/`index.ts`/NodePath#node., numericLiteralscip-typescript npm @babel/types 7.20.7 src/builders/generated/`index.ts`/numericLiteral().(1)), |
| 50 | ); |
| 51 | } else if (path.nodescip-typescript npm @babel/traverse 7.20.13 src/path/`index.ts`/NodePath#node..prefixscip-typescript npm @babel/types 7.20.7 src/ast-types/generated/`index.ts`/UpdateExpression#prefix.) { |
| 52 | // ++i => (i = (+i) + 1); |
| 53 | path.replaceWithscip-typescript npm @babel/traverse 7.20.13 src/path/`replacement.ts`/replaceWith().( |
| 54 | assignmentExpressionscip-typescript npm @babel/types 7.20.7 src/builders/generated/`index.ts`/assignmentExpression().( |
| 55 | "=", |
| 56 | identifierscip-typescript npm @babel/types 7.20.7 src/builders/generated/`index.ts`/identifier().(localName), |
| 57 | binaryExpressionscip-typescript npm @babel/types 7.20.7 src/builders/generated/`index.ts`/binaryExpression().( |
| 58 | path.nodescip-typescript npm @babel/traverse 7.20.13 src/path/`index.ts`/NodePath#node..operatorscip-typescript npm @babel/types 7.20.7 src/ast-types/generated/`index.ts`/UpdateExpression#operator.[0] as "+" | "-", |
| 59 | unaryExpressionscip-typescript npm @babel/types 7.20.7 src/builders/generated/`index.ts`/unaryExpression().("+", arg.nodescip-typescript npm @babel/traverse 7.20.13 src/path/`index.ts`/NodePath#node.), |
| 60 | numericLiteralscip-typescript npm @babel/types 7.20.7 src/builders/generated/`index.ts`/numericLiteral().(1), |
| 61 | ), |
| 62 | ), |
| 63 | ); |
| 64 | } else { |
| 65 | const old = path.scopescip-typescript npm @babel/traverse 7.20.13 src/path/`index.ts`/NodePath#scope..generateUidIdentifierBasedOnNodescip-typescript npm @babel/traverse 7.20.13 src/scope/`index.ts`/Scope#generateUidIdentifierBasedOnNode().( |
| 66 | arg.nodescip-typescript npm @babel/traverse 7.20.13 src/path/`index.ts`/NodePath#node., |
| 67 | "old", |
| 68 | ); |
| 69 | const varName = old.namescip-typescript npm @babel/types 7.20.7 src/ast-types/generated/`index.ts`/Identifier#name.; |
| 70 | path.scopescip-typescript npm @babel/traverse 7.20.13 src/path/`index.ts`/NodePath#scope..pushscip-typescript npm @babel/traverse 7.20.13 src/scope/`index.ts`/Scope#push().({ idscip-typescript npm @babel/traverse 7.20.13 src/scope/`index.ts`/Scope#push().(opts)typeLiteral191:id.: old }); |
| 71 | |
| 72 | const binary = binaryExpressionscip-typescript npm @babel/types 7.20.7 src/builders/generated/`index.ts`/binaryExpression().( |
| 73 | path.nodescip-typescript npm @babel/traverse 7.20.13 src/path/`index.ts`/NodePath#node..operatorscip-typescript npm @babel/types 7.20.7 src/ast-types/generated/`index.ts`/UpdateExpression#operator.[0] as "+" | "-", |
| 74 | identifierscip-typescript npm @babel/types 7.20.7 src/builders/generated/`index.ts`/identifier().(varName), |
| 75 | // todo: support bigint |
| 76 | numericLiteralscip-typescript npm @babel/types 7.20.7 src/builders/generated/`index.ts`/numericLiteral().(1), |
| 77 | ); |
| 78 | |
| 79 | // i++ => (_old = (+i), i = _old + 1, _old) |
| 80 | path.replaceWithscip-typescript npm @babel/traverse 7.20.13 src/path/`replacement.ts`/replaceWith().( |
| 81 | sequenceExpressionscip-typescript npm @babel/types 7.20.7 src/builders/generated/`index.ts`/sequenceExpression().([ |
| 82 | assignmentExpressionscip-typescript npm @babel/types 7.20.7 src/builders/generated/`index.ts`/assignmentExpression().( |
| 83 | "=", |
| 84 | identifierscip-typescript npm @babel/types 7.20.7 src/builders/generated/`index.ts`/identifier().(varName), |
| 85 | unaryExpressionscip-typescript npm @babel/types 7.20.7 src/builders/generated/`index.ts`/unaryExpression().("+", arg.nodescip-typescript npm @babel/traverse 7.20.13 src/path/`index.ts`/NodePath#node.), |
| 86 | ), |
| 87 | assignmentExpressionscip-typescript npm @babel/types 7.20.7 src/builders/generated/`index.ts`/assignmentExpression().("=", cloneNodescip-typescript npm @babel/types 7.20.7 src/clone/`cloneNode.ts`/cloneNode().(arg.nodescip-typescript npm @babel/traverse 7.20.13 src/path/`index.ts`/NodePath#node.), binary), |
| 88 | identifierscip-typescript npm @babel/types 7.20.7 src/builders/generated/`index.ts`/identifier().(varName), |
| 89 | ]), |
| 90 | ); |
| 91 | } |
| 92 | }, |
| 93 | }, |
| 94 | |
| 95 | AssignmentExpressionscip-typescript npm @babel/helper-simple-access 7.20.2 src/`index.ts`/AssignmentExpression0:: { |
| 96 | exit(path) { |
| 97 | const { scopescip-typescript npm @babel/helper-simple-access 7.20.2 src/`index.ts`/State#typeLiteral0:scope., seenscip-typescript npm @babel/helper-simple-access 7.20.2 src/`index.ts`/State#typeLiteral0:seen., bindingNamesscip-typescript npm @babel/helper-simple-access 7.20.2 src/`index.ts`/State#typeLiteral0:bindingNames. } = this; |
| 98 | |
| 99 | if (path.nodescip-typescript npm @babel/traverse 7.20.13 src/path/`index.ts`/NodePath#node..operatorscip-typescript npm @babel/types 7.20.7 src/ast-types/generated/`index.ts`/AssignmentExpression#operator. === "=") return; |
| 100 | |
| 101 | if (seen.hasscip-typescript npm typescript 5.9.3 lib/`lib.es2015.collection.d.ts`/WeakSet#has().(path.nodescip-typescript npm @babel/traverse 7.20.13 src/path/`index.ts`/NodePath#node.)) return; |
| 102 | seen.addscip-typescript npm typescript 5.9.3 lib/`lib.es2015.collection.d.ts`/WeakSet#add().(path.nodescip-typescript npm @babel/traverse 7.20.13 src/path/`index.ts`/NodePath#node.); |
| 103 | |
| 104 | const left = path.get("left"); |
| 105 | if (!left.isIdentifierscip-typescript npm @babel/traverse 7.20.13 src/path/generated/`validators.d.ts`/BaseNodePathValidators#isIdentifier().()) return; |
| 106 | |
| 107 | // Simple update-assign foo += 1; |
| 108 | // => exports.foo = (foo += 1); |
| 109 | const localName = left.nodescip-typescript npm @babel/traverse 7.20.13 src/path/`index.ts`/NodePath#node..namescip-typescript npm @babel/types 7.20.7 src/ast-types/generated/`index.ts`/Identifier#name.; |
| 110 | |
| 111 | if (!bindingNames.hasscip-typescript npm typescript 5.9.3 lib/`lib.es2015.collection.d.ts`/Set#has().(localName)) return; |
| 112 | |
| 113 | // redeclared in this scope |
| 114 | if (scope.getBindingscip-typescript npm @babel/traverse 7.20.13 src/scope/`index.ts`/Scope#getBinding().(localName) !== path.scopescip-typescript npm @babel/traverse 7.20.13 src/path/`index.ts`/NodePath#scope..getBindingscip-typescript npm @babel/traverse 7.20.13 src/scope/`index.ts`/Scope#getBinding().(localName)) { |
| 115 | return; |
| 116 | } |
| 117 | |
| 118 | const operator = path.nodescip-typescript npm @babel/traverse 7.20.13 src/path/`index.ts`/NodePath#node..operatorscip-typescript npm @babel/types 7.20.7 src/ast-types/generated/`index.ts`/AssignmentExpression#operator..slicescip-typescript npm typescript 5.9.3 lib/`lib.es5.d.ts`/String#slice().(0, -1); |
| 119 | if (LOGICAL_OPERATORSscip-typescript npm @babel/types 7.20.7 src/constants/`index.ts`/LOGICAL_OPERATORS..includesscip-typescript npm typescript 5.9.3 lib/`lib.es2016.array.include.d.ts`/Array#includes().(operator)) { |
| 120 | // &&, ||, ?? |
| 121 | // (foo &&= bar) => (foo && foo = bar) |
| 122 | path.replaceWithscip-typescript npm @babel/traverse 7.20.13 src/path/`replacement.ts`/replaceWith().( |
| 123 | logicalExpressionscip-typescript npm @babel/types 7.20.7 src/builders/generated/`index.ts`/logicalExpression().( |
| 124 | // @ts-expect-error Guarded by LOGICAL_OPERATORS.includes |
| 125 | operator, |
| 126 | path.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`/AssignmentExpression#left., |
| 127 | assignmentExpressionscip-typescript npm @babel/types 7.20.7 src/builders/generated/`index.ts`/assignmentExpression().( |
| 128 | "=", |
| 129 | cloneNodescip-typescript npm @babel/types 7.20.7 src/clone/`cloneNode.ts`/cloneNode().(path.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`/AssignmentExpression#left.), |
| 130 | path.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`/AssignmentExpression#right., |
| 131 | ), |
| 132 | ), |
| 133 | ); |
| 134 | } else { |
| 135 | // (foo += bar) => (foo = foo + bar) |
| 136 | path.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`/AssignmentExpression#right. = binaryExpressionscip-typescript npm @babel/types 7.20.7 src/builders/generated/`index.ts`/binaryExpression().( |
| 137 | // @ts-expect-error An assignment expression operator removing "=" must |
| 138 | // be a valid binary operator |
| 139 | operator, |
| 140 | cloneNodescip-typescript npm @babel/types 7.20.7 src/clone/`cloneNode.ts`/cloneNode().(path.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`/AssignmentExpression#left.), |
| 141 | path.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`/AssignmentExpression#right., |
| 142 | ); |
| 143 | path.nodescip-typescript npm @babel/traverse 7.20.13 src/path/`index.ts`/NodePath#node..operatorscip-typescript npm @babel/types 7.20.7 src/ast-types/generated/`index.ts`/AssignmentExpression#operator. = "="; |
| 144 | } |
| 145 | }, |
| 146 | }, |
| 147 | }; |
| 148 | |
| 149 | export default function simplifyAccessscip-typescript npm @babel/helper-simple-access 7.20.2 src/`index.ts`/simplifyAccess().( |
| 150 | pathscip-typescript npm @babel/helper-simple-access 7.20.2 src/`index.ts`/simplifyAccess().(path): NodePathscip-typescript npm @babel/traverse 7.20.13 src/path/`index.ts`/NodePath#, |
| 151 | bindingNamesscip-typescript npm @babel/helper-simple-access 7.20.2 src/`index.ts`/simplifyAccess().(bindingNames): Setscip-typescript npm typescript 5.9.3 lib/`lib.es2015.collection.d.ts`/Set#<string>, |
| 152 | // TODO(Babel 8): Remove this |
| 153 | includeUpdateExpressionscip-typescript npm @babel/helper-simple-access 7.20.2 src/`index.ts`/simplifyAccess().(includeUpdateExpression): boolean = true, |
| 154 | ) { |
| 155 | pathscip-typescript npm @babel/helper-simple-access 7.20.2 src/`index.ts`/simplifyAccess().(path).traversescip-typescript npm @babel/traverse 7.20.13 src/path/`index.ts`/NodePath#traverse().(simpleAssignmentVisitorscip-typescript npm @babel/helper-simple-access 7.20.2 src/`index.ts`/simpleAssignmentVisitor., { |
| 156 | scopescip-typescript npm @babel/helper-simple-access 7.20.2 src/`index.ts`/scope0:: pathscip-typescript npm @babel/helper-simple-access 7.20.2 src/`index.ts`/simplifyAccess().(path).scopescip-typescript npm @babel/traverse 7.20.13 src/path/`index.ts`/NodePath#scope., |
| 157 | bindingNamesscip-typescript npm @babel/helper-simple-access 7.20.2 src/`index.ts`/bindingNames0:, |
| 158 | seenscip-typescript npm @babel/helper-simple-access 7.20.2 src/`index.ts`/seen0:: new WeakSetscip-typescript npm typescript 5.9.3 lib/`lib.es2015.collection.d.ts`/WeakSet#(), |
| 159 | includeUpdateExpressionscip-typescript npm @babel/helper-simple-access 7.20.2 src/`index.ts`/includeUpdateExpression0:, |
| 160 | }); |
| 161 | } |