source: trip-planner-front/node_modules/@babel/helper-simple-access/lib/index.js@ 8d391a1

Last change on this file since 8d391a1 was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 2.7 KB
Line 
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.default = simplifyAccess;
7
8var _t = require("@babel/types");
9
10const {
11 LOGICAL_OPERATORS,
12 assignmentExpression,
13 binaryExpression,
14 cloneNode,
15 identifier,
16 logicalExpression,
17 numericLiteral,
18 sequenceExpression,
19 unaryExpression
20} = _t;
21
22function simplifyAccess(path, bindingNames) {
23 path.traverse(simpleAssignmentVisitor, {
24 scope: path.scope,
25 bindingNames,
26 seen: new WeakSet()
27 });
28}
29
30const simpleAssignmentVisitor = {
31 UpdateExpression: {
32 exit(path) {
33 const {
34 scope,
35 bindingNames
36 } = this;
37 const arg = path.get("argument");
38 if (!arg.isIdentifier()) return;
39 const localName = arg.node.name;
40 if (!bindingNames.has(localName)) return;
41
42 if (scope.getBinding(localName) !== path.scope.getBinding(localName)) {
43 return;
44 }
45
46 if (path.parentPath.isExpressionStatement() && !path.isCompletionRecord()) {
47 const operator = path.node.operator == "++" ? "+=" : "-=";
48 path.replaceWith(assignmentExpression(operator, arg.node, numericLiteral(1)));
49 } else if (path.node.prefix) {
50 path.replaceWith(assignmentExpression("=", identifier(localName), binaryExpression(path.node.operator[0], unaryExpression("+", arg.node), numericLiteral(1))));
51 } else {
52 const old = path.scope.generateUidIdentifierBasedOnNode(arg.node, "old");
53 const varName = old.name;
54 path.scope.push({
55 id: old
56 });
57 const binary = binaryExpression(path.node.operator[0], identifier(varName), numericLiteral(1));
58 path.replaceWith(sequenceExpression([assignmentExpression("=", identifier(varName), unaryExpression("+", arg.node)), assignmentExpression("=", cloneNode(arg.node), binary), identifier(varName)]));
59 }
60 }
61
62 },
63 AssignmentExpression: {
64 exit(path) {
65 const {
66 scope,
67 seen,
68 bindingNames
69 } = this;
70 if (path.node.operator === "=") return;
71 if (seen.has(path.node)) return;
72 seen.add(path.node);
73 const left = path.get("left");
74 if (!left.isIdentifier()) return;
75 const localName = left.node.name;
76 if (!bindingNames.has(localName)) return;
77
78 if (scope.getBinding(localName) !== path.scope.getBinding(localName)) {
79 return;
80 }
81
82 const operator = path.node.operator.slice(0, -1);
83
84 if (LOGICAL_OPERATORS.includes(operator)) {
85 path.replaceWith(logicalExpression(operator, path.node.left, assignmentExpression("=", cloneNode(path.node.left), path.node.right)));
86 } else {
87 path.node.right = binaryExpression(operator, cloneNode(path.node.left), path.node.right);
88 path.node.operator = "=";
89 }
90 }
91
92 }
93};
Note: See TracBrowser for help on using the repository browser.