@babel/plugin-transform-nullish-coalescing-operator
情報
このプラグインは@babel/preset-env
に含まれており、ES2020にあります。
例
入力
JavaScript
var foo = object.foo ?? "default";
出力
JavaScript
var _object$foo;
var foo =
(_object$foo = object.foo) !== null && _object$foo !== void 0
? _object$foo
: "default";
注記
ここでは!= null
を使用できません。なぜなら、document.all == null
であり、document.all
は「nullish」とはみなされていないからです。
インストール
- npm
- Yarn
- pnpm
npm install --save-dev @babel/plugin-transform-nullish-coalescing-operator
yarn add --dev @babel/plugin-transform-nullish-coalescing-operator
pnpm add --save-dev @babel/plugin-transform-nullish-coalescing-operator
使い方
設定ファイルを使用する(推奨)
babel.config.json
{
"plugins": ["@babel/plugin-transform-nullish-coalescing-operator"]
}
CLI経由
Shell
babel --plugins @babel/plugin-transform-nullish-coalescing-operator script.js
Node API経由
JavaScript
require("@babel/core").transformSync("code", {
plugins: ["@babel/plugin-transform-nullish-coalescing-operator"],
});
オプション
loose
boolean
、デフォルトはfalse
です。
true
の場合、この変換はdocument.all
が存在しないものとして扱い、null
とundefined
の両方に対する厳密な等価性チェックではなく、null
との緩い等価性チェックを実行します。
注意
トップレベルのnoDocumentAll
のアサンプションへの移行を検討してください。
babel.config.json
{
"assumptions": {
"noDocumentAll": true
}
}
例
入力
JavaScript
var foo = object.foo ?? "default";
出力
JavaScript
var _object$foo;
var foo = (_object$foo = object.foo) != null ? _object$foo : "default";
ヒント
プラグインオプションの設定について詳しくはこちらをご覧ください。