1 line
8.2 KiB
Plaintext
1 line
8.2 KiB
Plaintext
{"version":3,"names":["LOGICAL_OPERATORS","assignmentExpression","binaryExpression","cloneNode","identifier","logicalExpression","numericLiteral","sequenceExpression","unaryExpression","simpleAssignmentVisitor","UpdateExpression","exit","path","scope","bindingNames","includeUpdateExpression","arg","get","isIdentifier","localName","node","name","has","getBinding","parentPath","isExpressionStatement","isCompletionRecord","operator","replaceWith","prefix","old","generateUidIdentifierBasedOnNode","varName","push","id","binary","AssignmentExpression","seen","add","left","slice","includes","right","simplifyAccess","traverse","WeakSet"],"sources":["../src/index.ts"],"sourcesContent":["import {\n LOGICAL_OPERATORS,\n assignmentExpression,\n binaryExpression,\n cloneNode,\n identifier,\n logicalExpression,\n numericLiteral,\n sequenceExpression,\n unaryExpression,\n} from \"@babel/types\";\nimport type * as t from \"@babel/types\";\nimport type { NodePath, Scope, Visitor } from \"@babel/traverse\";\n\ntype State = {\n scope: Scope;\n bindingNames: Set<string>;\n seen: WeakSet<t.Node>;\n includeUpdateExpression: boolean;\n};\n\nconst simpleAssignmentVisitor: Visitor<State> = {\n // TODO(Babel 8): Remove UpdateExpression\n UpdateExpression: {\n exit(path) {\n const { scope, bindingNames, includeUpdateExpression } = this;\n if (!includeUpdateExpression) {\n return;\n }\n\n const arg = path.get(\"argument\");\n if (!arg.isIdentifier()) return;\n const localName = arg.node.name;\n\n if (!bindingNames.has(localName)) return;\n\n // redeclared in this scope\n if (scope.getBinding(localName) !== path.scope.getBinding(localName)) {\n return;\n }\n\n if (\n path.parentPath.isExpressionStatement() &&\n !path.isCompletionRecord()\n ) {\n // ++i => (i += 1);\n const operator = path.node.operator == \"++\" ? \"+=\" : \"-=\";\n path.replaceWith(\n assignmentExpression(operator, arg.node, numericLiteral(1)),\n );\n } else if (path.node.prefix) {\n // ++i => (i = (+i) + 1);\n path.replaceWith(\n assignmentExpression(\n \"=\",\n identifier(localName),\n binaryExpression(\n path.node.operator[0] as \"+\" | \"-\",\n unaryExpression(\"+\", arg.node),\n numericLiteral(1),\n ),\n ),\n );\n } else {\n const old = path.scope.generateUidIdentifierBasedOnNode(\n arg.node,\n \"old\",\n );\n const varName = old.name;\n path.scope.push({ id: old });\n\n const binary = binaryExpression(\n path.node.operator[0] as \"+\" | \"-\",\n identifier(varName),\n // todo: support bigint\n numericLiteral(1),\n );\n\n // i++ => (_old = (+i), i = _old + 1, _old)\n path.replaceWith(\n sequenceExpression([\n assignmentExpression(\n \"=\",\n identifier(varName),\n unaryExpression(\"+\", arg.node),\n ),\n assignmentExpression(\"=\", cloneNode(arg.node), binary),\n identifier(varName),\n ]),\n );\n }\n },\n },\n\n AssignmentExpression: {\n exit(path) {\n const { scope, seen, bindingNames } = this;\n\n if (path.node.operator === \"=\") return;\n\n if (seen.has(path.node)) return;\n seen.add(path.node);\n\n const left = path.get(\"left\");\n if (!left.isIdentifier()) return;\n\n // Simple update-assign foo += 1;\n // => exports.foo = (foo += 1);\n const localName = left.node.name;\n\n if (!bindingNames.has(localName)) return;\n\n // redeclared in this scope\n if (scope.getBinding(localName) !== path.scope.getBinding(localName)) {\n return;\n }\n\n const operator = path.node.operator.slice(0, -1);\n if (LOGICAL_OPERATORS.includes(operator)) {\n // &&, ||, ??\n // (foo &&= bar) => (foo && foo = bar)\n path.replaceWith(\n logicalExpression(\n // @ts-expect-error Guarded by LOGICAL_OPERATORS.includes\n operator,\n path.node.left,\n assignmentExpression(\n \"=\",\n cloneNode(path.node.left),\n path.node.right,\n ),\n ),\n );\n } else {\n // (foo += bar) => (foo = foo + bar)\n path.node.right = binaryExpression(\n // @ts-expect-error An assignment expression operator removing \"=\" must\n // be a valid binary operator\n operator,\n cloneNode(path.node.left),\n path.node.right,\n );\n path.node.operator = \"=\";\n }\n },\n },\n};\n\nexport default function simplifyAccess(\n path: NodePath,\n bindingNames: Set<string>,\n // TODO(Babel 8): Remove this\n includeUpdateExpression: boolean = true,\n) {\n path.traverse(simpleAssignmentVisitor, {\n scope: path.scope,\n bindingNames,\n seen: new Weak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