1 | "use strict";
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", {
|
4 | value: true
|
5 | });
|
6 | exports.default = simplifyAccess;
|
7 |
|
8 | var t = _interopRequireWildcard(require("@babel/types"));
|
9 |
|
10 | function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function () { return cache; }; return cache; }
|
11 |
|
12 | function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
|
13 |
|
14 | function simplifyAccess(path, bindingNames) {
|
15 | path.traverse(simpleAssignmentVisitor, {
|
16 | scope: path.scope,
|
17 | bindingNames,
|
18 | seen: new WeakSet()
|
19 | });
|
20 | }
|
21 |
|
22 | const simpleAssignmentVisitor = {
|
23 | UpdateExpression: {
|
24 | exit(path) {
|
25 | const {
|
26 | scope,
|
27 | bindingNames
|
28 | } = this;
|
29 | const arg = path.get("argument");
|
30 | if (!arg.isIdentifier()) return;
|
31 | const localName = arg.node.name;
|
32 | if (!bindingNames.has(localName)) return;
|
33 |
|
34 | if (scope.getBinding(localName) !== path.scope.getBinding(localName)) {
|
35 | return;
|
36 | }
|
37 |
|
38 | if (path.parentPath.isExpressionStatement() && !path.isCompletionRecord()) {
|
39 | const operator = path.node.operator == "++" ? "+=" : "-=";
|
40 | path.replaceWith(t.assignmentExpression(operator, arg.node, t.numericLiteral(1)));
|
41 | } else if (path.node.prefix) {
|
42 | path.replaceWith(t.assignmentExpression("=", t.identifier(localName), t.binaryExpression(path.node.operator[0], t.unaryExpression("+", arg.node), t.numericLiteral(1))));
|
43 | } else {
|
44 | const old = path.scope.generateUidIdentifierBasedOnNode(arg.node, "old");
|
45 | const varName = old.name;
|
46 | path.scope.push({
|
47 | id: old
|
48 | });
|
49 | const binary = t.binaryExpression(path.node.operator[0], t.identifier(varName), t.numericLiteral(1));
|
50 | path.replaceWith(t.sequenceExpression([t.assignmentExpression("=", t.identifier(varName), t.unaryExpression("+", arg.node)), t.assignmentExpression("=", t.cloneNode(arg.node), binary), t.identifier(varName)]));
|
51 | }
|
52 | }
|
53 |
|
54 | },
|
55 | AssignmentExpression: {
|
56 | exit(path) {
|
57 | const {
|
58 | scope,
|
59 | seen,
|
60 | bindingNames
|
61 | } = this;
|
62 | if (path.node.operator === "=") return;
|
63 | if (seen.has(path.node)) return;
|
64 | seen.add(path.node);
|
65 | const left = path.get("left");
|
66 | if (!left.isIdentifier()) return;
|
67 | const localName = left.node.name;
|
68 | if (!bindingNames.has(localName)) return;
|
69 |
|
70 | if (scope.getBinding(localName) !== path.scope.getBinding(localName)) {
|
71 | return;
|
72 | }
|
73 |
|
74 | path.node.right = t.binaryExpression(path.node.operator.slice(0, -1), t.cloneNode(path.node.left), path.node.right);
|
75 | path.node.operator = "=";
|
76 | }
|
77 |
|
78 | }
|
79 | }; |
\ | No newline at end of file |