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 _pluginSyntaxNullishCoalescingOperator = require("@babel/plugin-syntax-nullish-coalescing-operator");
|
---|
11 |
|
---|
12 | var _core = require("@babel/core");
|
---|
13 |
|
---|
14 | var _default = (0, _helperPluginUtils.declare)((api, {
|
---|
15 | loose = false
|
---|
16 | }) => {
|
---|
17 | var _api$assumption;
|
---|
18 |
|
---|
19 | api.assertVersion(7);
|
---|
20 | const noDocumentAll = (_api$assumption = api.assumption("noDocumentAll")) != null ? _api$assumption : loose;
|
---|
21 | return {
|
---|
22 | name: "proposal-nullish-coalescing-operator",
|
---|
23 | inherits: _pluginSyntaxNullishCoalescingOperator.default,
|
---|
24 | visitor: {
|
---|
25 | LogicalExpression(path) {
|
---|
26 | const {
|
---|
27 | node,
|
---|
28 | scope
|
---|
29 | } = path;
|
---|
30 |
|
---|
31 | if (node.operator !== "??") {
|
---|
32 | return;
|
---|
33 | }
|
---|
34 |
|
---|
35 | let ref;
|
---|
36 | let assignment;
|
---|
37 |
|
---|
38 | if (scope.isStatic(node.left)) {
|
---|
39 | ref = node.left;
|
---|
40 | assignment = _core.types.cloneNode(node.left);
|
---|
41 | } else if (scope.path.isPattern()) {
|
---|
42 | path.replaceWith(_core.template.ast`(() => ${path.node})()`);
|
---|
43 | return;
|
---|
44 | } else {
|
---|
45 | ref = scope.generateUidIdentifierBasedOnNode(node.left);
|
---|
46 | scope.push({
|
---|
47 | id: _core.types.cloneNode(ref)
|
---|
48 | });
|
---|
49 | assignment = _core.types.assignmentExpression("=", ref, node.left);
|
---|
50 | }
|
---|
51 |
|
---|
52 | path.replaceWith(_core.types.conditionalExpression(noDocumentAll ? _core.types.binaryExpression("!=", assignment, _core.types.nullLiteral()) : _core.types.logicalExpression("&&", _core.types.binaryExpression("!==", assignment, _core.types.nullLiteral()), _core.types.binaryExpression("!==", _core.types.cloneNode(ref), scope.buildUndefinedNode())), _core.types.cloneNode(ref), node.right));
|
---|
53 | }
|
---|
54 |
|
---|
55 | }
|
---|
56 | };
|
---|
57 | });
|
---|
58 |
|
---|
59 | exports.default = _default; |
---|