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