compile the walrus operator
:=
to an IIFE
assignment statement
- assigns 42 to x
x = 42;
assignment expression
- assigns 42 to x
- returns 42
(x := 42)
y = func(x);
const arr = [y, y ** 2, y ** 4, y ** 6];
can become
const arr = [y := func(x), y ** 2, y ** 4, y ** 6]
if (x := 2) alert();
becomes
if (function (x) {
if (typeof x === 'undefined') throw new Error();
x = 2;
return x;
}(x)) alert();
this plugin relies upon the :=
token, it will have to be manually added to babel's parser
follow Tan Li Hau's guide, you will fork babel and add a token to the parser
$ npm install --save-dev babel-plugin-transform-walrus-operator
.babelrc
{
"plugins": ["transform-walrus-operator"]
}
$ babel --plugins transform-walrus-operator script.js
require('babel-core').transform('code', {
plugins: ['transform-walrus-operator'],
});
MIT