| 1 | import { |
| 2 | LOGICAL_OPERATORS, |
| 3 | assignmentExpression, |
| 4 | binaryExpression, |
| 5 | cloneNode, |
| 6 | identifier, |
| 7 | logicalExpression, |
| 8 | numericLiteral, |
| 9 | sequenceExpression, |
| 10 | unaryExpression, |
| 11 | } from "@babel/types"; |
| 12 | import type * as t from "@babel/types"; |
| 13 | import type { NodePath, Scope, Visitor } from "@babel/traverse"; |
| 14 | |
| 15 | type State = { |
| 16 | scope: Scope; |
| 17 | bindingNames: Set<string>; |
| 18 | seen: WeakSet<t.Node>; |
| 19 | includeUpdateExpression: boolean; |
| 20 | }; |
| 21 | |
| 22 | const simpleAssignmentVisitor: Visitor<State> = { |
| 23 | // TODO(Babel 8): Remove UpdateExpression |
| 24 | UpdateExpression: { |
| 25 | exit(path) { |
| 26 | const { scope, bindingNames, includeUpdateExpression } = this; |
| 27 | if (!includeUpdateExpression) { |
| 28 | return; |
| 29 | } |
| 30 | |
| 31 | const arg = path.get("argument"); |
| 32 | if (!arg.isIdentifier()) return; |
| 33 | const localName = arg.node.name; |
| 34 | |
| 35 | if (!bindingNames.has(localName)) return; |
| 36 | |
| 37 | // redeclared in this scope |
| 38 | if (scope.getBinding(localName) !== path.scope.getBinding(localName)) { |
| 39 | return; |
| 40 | } |
| 41 | |
| 42 | if ( |
| 43 | path.parentPath.isExpressionStatement() && |
| 44 | !path.isCompletionRecord() |
| 45 | ) { |
| 46 | // ++i => (i += 1); |
| 47 | const operator = path.node.operator == "++" ? "+=" : "-="; |
| 48 | path.replaceWith( |
| 49 | assignmentExpression(operator, arg.node, numericLiteral(1)), |
| 50 | ); |
| 51 | } else if (path.node.prefix) { |
| 52 | // ++i => (i = (+i) + 1); |
| 53 | path.replaceWith( |
| 54 | assignmentExpression( |
| 55 | "=", |
| 56 | identifier(localName), |
| 57 | binaryExpression( |
| 58 | path.node.operator[0] as "+" | "-", |
| 59 | unaryExpression("+", arg.node), |
| 60 | numericLiteral(1), |
| 61 | ), |
| 62 | ), |
| 63 | ); |
| 64 | } else { |
| 65 | const old = path.scope.generateUidIdentifierBasedOnNode( |
| 66 | arg.node, |
| 67 | "old", |
| 68 | ); |
| 69 | const varName = old.name; |
| 70 | path.scope.push({ id: old }); |
| 71 | |
| 72 | const binary = binaryExpression( |
| 73 | path.node.operator[0] as "+" | "-", |
| 74 | identifier(varName), |
| 75 | // todo: support bigint |
| 76 | numericLiteral(1), |
| 77 | ); |
| 78 | |
| 79 | // i++ => (_old = (+i), i = _old + 1, _old) |
| 80 | path.replaceWith( |
| 81 | sequenceExpression([ |
| 82 | assignmentExpression( |
| 83 | "=", |
| 84 | identifier(varName), |
| 85 | unaryExpression("+", arg.node), |
| 86 | ), |
| 87 | assignmentExpression("=", cloneNode(arg.node), binary), |
| 88 | identifier(varName), |
| 89 | ]), |
| 90 | ); |
| 91 | } |
| 92 | }, |
| 93 | }, |
| 94 | |
| 95 | AssignmentExpression: { |
| 96 | exit(path) { |
| 97 | const { scope, seen, bindingNames } = this; |
| 98 | |
| 99 | if (path.node.operator === "=") return; |
| 100 | |
| 101 | if (seen.has(path.node)) return; |
| 102 | seen.add(path.node); |
| 103 | |
| 104 | const left = path.get("left"); |
| 105 | if (!left.isIdentifier()) return; |
| 106 | |
| 107 | // Simple update-assign foo += 1; |
| 108 | // => exports.foo = (foo += 1); |
| 109 | const localName = left.node.name; |
| 110 | |
| 111 | if (!bindingNames.has(localName)) return; |
| 112 | |
| 113 | // redeclared in this scope |
| 114 | if (scope.getBinding(localName) !== path.scope.getBinding(localName)) { |
| 115 | return; |
| 116 | } |
| 117 | |
| 118 | const operator = path.node.operator.slice(0, -1); |
| 119 | if (LOGICAL_OPERATORS.includes(operator)) { |
| 120 | // &&, ||, ?? |
| 121 | // (foo &&= bar) => (foo && foo = bar) |
| 122 | path.replaceWith( |
| 123 | logicalExpression( |
| 124 | // @ts-expect-error Guarded by LOGICAL_OPERATORS.includes |
| 125 | operator, |
| 126 | path.node.left, |
| 127 | assignmentExpression( |
| 128 | "=", |
| 129 | cloneNode(path.node.left), |
| 130 | path.node.right, |
| 131 | ), |
| 132 | ), |
| 133 | ); |
| 134 | } else { |
| 135 | // (foo += bar) => (foo = foo + bar) |
| 136 | path.node.right = binaryExpression( |
| 137 | // @ts-expect-error An assignment expression operator removing "=" must |
| 138 | // be a valid binary operator |
| 139 | operator, |
| 140 | cloneNode(path.node.left), |
| 141 | path.node.right, |
| 142 | ); |
| 143 | path.node.operator = "="; |
| 144 | } |
| 145 | }, |
| 146 | }, |
| 147 | }; |
| 148 | |
| 149 | export default function simplifyAccess( |
| 150 | path: NodePath, |
| 151 | bindingNames: Set<string>, |
| 152 | // TODO(Babel 8): Remove this |
| 153 | includeUpdateExpression: boolean = true, |
| 154 | ) { |
| 155 | path.traverse(simpleAssignmentVisitor, { |
| 156 | scope: path.scope, |
| 157 | bindingNames, |
| 158 | seen: new WeakSet(), |
| 159 | includeUpdateExpression, |
| 160 | }); |
| 161 | } |