1 | "use strict";
|
---|
2 |
|
---|
3 | Object.defineProperty(exports, "__esModule", {
|
---|
4 | value: true
|
---|
5 | });
|
---|
6 | exports.default = void 0;
|
---|
7 |
|
---|
8 | var _helperPluginUtils = require("@babel/helper-plugin-utils");
|
---|
9 |
|
---|
10 | var _pluginSyntaxLogicalAssignmentOperators = require("@babel/plugin-syntax-logical-assignment-operators");
|
---|
11 |
|
---|
12 | var _core = require("@babel/core");
|
---|
13 |
|
---|
14 | var _default = (0, _helperPluginUtils.declare)(api => {
|
---|
15 | api.assertVersion(7);
|
---|
16 | return {
|
---|
17 | name: "proposal-logical-assignment-operators",
|
---|
18 | inherits: _pluginSyntaxLogicalAssignmentOperators.default,
|
---|
19 | visitor: {
|
---|
20 | AssignmentExpression(path) {
|
---|
21 | const {
|
---|
22 | node,
|
---|
23 | scope
|
---|
24 | } = path;
|
---|
25 | const {
|
---|
26 | operator,
|
---|
27 | left,
|
---|
28 | right
|
---|
29 | } = node;
|
---|
30 | const operatorTrunc = operator.slice(0, -1);
|
---|
31 |
|
---|
32 | if (!_core.types.LOGICAL_OPERATORS.includes(operatorTrunc)) {
|
---|
33 | return;
|
---|
34 | }
|
---|
35 |
|
---|
36 | const lhs = _core.types.cloneNode(left);
|
---|
37 |
|
---|
38 | if (_core.types.isMemberExpression(left)) {
|
---|
39 | const {
|
---|
40 | object,
|
---|
41 | property,
|
---|
42 | computed
|
---|
43 | } = left;
|
---|
44 | const memo = scope.maybeGenerateMemoised(object);
|
---|
45 |
|
---|
46 | if (memo) {
|
---|
47 | left.object = memo;
|
---|
48 | lhs.object = _core.types.assignmentExpression("=", _core.types.cloneNode(memo), object);
|
---|
49 | }
|
---|
50 |
|
---|
51 | if (computed) {
|
---|
52 | const memo = scope.maybeGenerateMemoised(property);
|
---|
53 |
|
---|
54 | if (memo) {
|
---|
55 | left.property = memo;
|
---|
56 | lhs.property = _core.types.assignmentExpression("=", _core.types.cloneNode(memo), property);
|
---|
57 | }
|
---|
58 | }
|
---|
59 | }
|
---|
60 |
|
---|
61 | path.replaceWith(_core.types.logicalExpression(operatorTrunc, lhs, _core.types.assignmentExpression("=", left, right)));
|
---|
62 | }
|
---|
63 |
|
---|
64 | }
|
---|
65 | };
|
---|
66 | });
|
---|
67 |
|
---|
68 | exports.default = _default; |
---|