mirror of https://github.com/jkjoy/sunpeiwen.git
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 &&
|