🚀 Launch Week Day 5:Introducing Immutable Scans.Learn More →
Socket
Book a DemoInstallSign in
Socket

babel-plugin-transform-walrus-operator

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

babel-plugin-transform-walrus-operator

compile walrus operator into an IIFE

latest
Source
npmnpm
Version
0.0.3
Version published
Maintainers
1
Created
Source

babel-plugin-transform-walrus-operator

compile the walrus operator := to an IIFE

About

assignment statement

  • assigns 42 to x
x = 42;

assignment expression

  • assigns 42 to x
  • returns 42
(x := 42)

Example use case

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]

Transformation

if (x := 2) alert();

becomes

if (function (x) {
  if (typeof x === 'undefined') throw new Error();
  x = 2;
  return x;
}(x)) alert();

Babel Setup

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

Installation

$ npm install --save-dev babel-plugin-transform-walrus-operator

Usage

.babelrc

{
  "plugins": ["transform-walrus-operator"]
}

Via CLI

$ babel --plugins transform-walrus-operator script.js

Via Node API

require('babel-core').transform('code', {
  plugins: ['transform-walrus-operator'],
});

License

MIT

FAQs

Package last updated on 22 Mar 2021

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts