Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
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
- Host: GitHub
- URL: https://github.com/sam-parsons/babel-plugin-transform-walrus-operator
- Owner: sam-parsons
- Created: 2021-03-14T22:35:02.000Z (almost 4 years ago)
- Default Branch: main
- Last Pushed: 2022-03-29T22:10:23.000Z (almost 3 years ago)
- Last Synced: 2024-11-16T16:48:11.984Z (about 2 months ago)
- Topics: babel, babel-plugin, walrus-operator
- Language: JavaScript
- Homepage: https://www.npmjs.com/package/babel-plugin-transform-walrus-operator
- Size: 231 KB
- Stars: 3
- Watchers: 1
- Forks: 0
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
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