Nicolò Ribaudo 8c7d4b55c9
Add plugins name (#8769)
* Add plugins name

* Add missing names found by the plugin

* Add eslint plugin
2018-11-18 23:02:58 +01:00

51 lines
1.4 KiB
JavaScript

import { declare } from "@babel/helper-plugin-utils";
import syntaxNullishCoalescingOperator from "@babel/plugin-syntax-nullish-coalescing-operator";
import { types as t } from "@babel/core";
export default declare((api, { loose = false }) => {
api.assertVersion(7);
return {
name: "proposal-nullish-coalescing-operator",
inherits: syntaxNullishCoalescingOperator,
visitor: {
LogicalExpression(path) {
const { node, scope } = path;
if (node.operator !== "??") {
return;
}
const ref = scope.generateUidIdentifierBasedOnNode(node.left);
scope.push({ id: ref });
const assignment = t.assignmentExpression(
"=",
t.cloneNode(ref),
node.left,
);
path.replaceWith(
t.conditionalExpression(
// We cannot use `!= null` in spec mode because
// `document.all == null` and `document.all` is not "nullish".
loose
? t.binaryExpression("!=", assignment, t.nullLiteral())
: t.logicalExpression(
"&&",
t.binaryExpression("!==", assignment, t.nullLiteral()),
t.binaryExpression(
"!==",
t.cloneNode(ref),
scope.buildUndefinedNode(),
),
),
t.cloneNode(ref),
node.right,
),
);
},
},
};
});