babel/packages/babel-plugin-transform-nullish-coalescing-operator
Lucas Azzola cd4f0ae393 Add loose mode for nullish coalescing operator (#6531)
* Add loose mode for nullish-coalescing

* Remove unneeded SequenceExpression
2017-10-22 13:25:29 +02:00
..

@babel/plugin-transform-nullish-coalescing-operator

Replace ?? with an inline helper.

Example

In

var foo = object.foo ?? "default";

Out

var _object$foo;

var foo = (_object$foo = object.foo) !== null && _object$foo !== void 0 ? _object$foo : "default";

NOTE: We cannot use != null here because document.all == null and document.all has been deemed not "nullish".

Installation

npm install --save-dev @babel/plugin-transform-nullish-coalescing-operator

Usage

.babelrc

{
  "plugins": ["transform-nullish-coalescing-operator"]
}

Via CLI

babel --plugins transform-nullish-coalescing-operator script.js

Via Node API

require("@babel/core").transform("code", {
  plugins: ["transform-nullish-coalescing-operator"]
});

Options

loose

boolean, defaults to false.

When true, this transform will pretend document.all does not exist, and perform loose equality checks with null instead of string equality checks against both null and undefined.

Example

In

var foo = object.foo ?? "default";

Out

var _object$foo;

var foo = (_object$foo = object.foo) != null ? _object$foo : "default";

References