Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/sam-parsons/babel-plugin-transform-walrus-operator

return a value from an assignment expression
https://github.com/sam-parsons/babel-plugin-transform-walrus-operator

babel babel-plugin walrus-operator

Last synced: about 2 months ago
JSON representation

return a value from an assignment expression

Awesome Lists containing this project

README

        

[npm]: https://img.shields.io/npm/v/babel-plugin-transform-walrus-operator
[npm-url]: https://www.npmjs.com/package/babel-plugin-transform-walrus-operator
[size]: https://packagephobia.now.sh/badge?p=babel-plugin-transform-walrus-operator
[size-url]: https://packagephobia.now.sh/result?p=babel-plugin-transform-walrus-operator

[![npm][npm]][npm-url]
[![size][size]][size-url]
[![libera manifesto](https://img.shields.io/badge/libera-manifesto-lightgrey.svg)](https://liberamanifesto.com)

# babel-plugin-transform-walrus-operator

> compile the walrus operator ```:=``` to an IIFE

## About

assignment statement

- assigns 42 to x
- returns undefined

```js
x = 42;
```

assignment expression

- assigns 42 to x
- returns 42

```js
(x := 42)
```


#### Example use case

```js
y = func(x);
const arr = [y, y ** 2, y ** 4, y ** 6];
```
can become
```js
const arr = [y := func(x), y ** 2, y ** 4, y ** 6]
```


#### Transformation

```js
if (x := 2) alert();
```
generally becomes
```js
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. see my babel fork commit

## Installation

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

## Usage

### Via `.babelrc` (Recommended)

**.babelrc**

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

### Via CLI

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

### Via Node API

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

# License

MIT