跳到主要内容

@babel/plugin-transform-logical-assignment-operators

信息

This plugin is included in @babel/preset-env, in ES2021

Example

In

JavaScript
a ||= b;
obj.a.b ||= c;

a &&= b;
obj.a.b &&= c;

Out

JavaScript
var _obj$a, _obj$a2;

a || (a = b);
(_obj$a = obj.a).b || (_obj$a.b = c);

a && (a = b);
(_obj$a2 = obj.a).b && (_obj$a2.b = c);

With Nullish Coalescing

While using the @babel/plugin-transform-nullish-coalescing-operator plugin (included in @babel/preset-env)

JavaScript
a ??= b;
obj.a.b ??= c;
JavaScript
var _a, _obj$a, _obj$a$b;

(_a = a) !== null && _a !== void 0 ? _a : (a = b);
(_obj$a$b = (_obj$a = obj.a).b) !== null && _obj$a$b !== void 0
? _obj$a$b
: (_obj$a.b = c);

Installation

npm install --save-dev @babel/plugin-transform-logical-assignment-operators

Usage

babel.config.json
{
"plugins": ["@babel/plugin-transform-logical-assignment-operators"]
}

Via CLI

Shell
babel --plugins @babel/plugin-transform-logical-assignment-operators script.js

Via Node API

JavaScript
require("@babel/core").transformSync("code", {
plugins: ["@babel/plugin-transform-logical-assignment-operators"],
});

References