babel/packages/babel-plugin-transform-nullish-coalescing-operator
Lucas Azzola 99be60b53d Implement transform for nullish-coalescing operator (#6483)
* Implement transform for nullish-coalescing operator

* Update example output

* Switch from BinaryExpression to LogicalExpression

* Address review comments

- Use generateUidIdentifierBasedOnNode
- Inline "??"
- Clone ref node
- Move "??" to LogicalExpression in babel-types

* Fix reference to @babel/helper-plugin-test-runner

* Fix reference to @babel/plugin-syntax-nullish-coalescing-operator

* Don't use parent scope

* Remove .vscode from .gitignore, change 'lib/index.js' to 'lib'

* Ensure `document.all ?? 0 === document.all`

* Fix note and copy to an inline comment
2017-10-18 03:10:05 -04: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, _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"]
});