• Stars
    star
    117
  • Rank 301,828 (Top 6 %)
  • Language
    JavaScript
  • License
    BSD 2-Clause "Sim...
  • Created over 9 years ago
  • Updated over 4 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Plugin for extensible destructuring in Babel

babel-plugin-extensible-destructuring

Babel plugin that enables extensible destructuring, inspired by vacuumlabs/es-proposals.

Circle CI Join the chat at https://gitter.im/vacuumlabs/babel-destructuring

Install

npm install --save-dev babel-plugin-extensible-destructuring
npm install --save extensible-runtime

Usage (for version 4.x.x and Babel 6)

Add the plugin to your .babelrc configuration:

{
  "presets": ["es2015"],
  "plugins": ["extensible-destructuring"]
}

Or directly from code:

var babel = require("babel-core");
babel.transform("code", {
    presets: ['es2015'],
    plugins: ['extensible-destructuring']
})

Usage for babel 7

Add the plugin to your .babelrc configuration using the full name:

{
  "presets": ["@babel/preset-env"],
  "plugins": ["babel-plugin-extensible-destructuring"]
}

What it does

The plugin gives you more explicit control of what exactly happens in the process of destructuring. The plugin transforms all destructuring assignments such as:

var {a = 10} = o

to:

var a = __extensible_get__(o, a, 10)

Function __extensible_get__ gets automatically required from extensible-runtime package (this is a separate package and needs to be installed alongside this one. This package currently defines three different versions of __extensible_get__ you can choose from using the impl plugin option (see the section Plugin Options below).

  • normal: standard ES6 compatible: no magic here
  • immutable: the one that you can use with Immutable.js to destructure its Maps and Lists
  • safe: prevent returning values from being undefined. Also includes features of immutable

Option safe is the default one. Typically, there is no reason to use this plugin with normal option.

Check out the example folder for a working example; this is a standalone npm package with all the configuration necessary.

Plugin Options

In case you don't know, babel allows to specify options for each plugin. The syntax looks such as:

{
  "presets": ["es2015"],
  "plugins": [["extensible-destructuring", {"mode": "optout", "impl": "safe"}]]
}

Semantics of these options is:

mode

either 'optin' or 'optout'. In optin (default) mode, the destructuring assignments are transformed only if "use extensible" directive is present at the beginning of the file. OTOH, in optout mode the destructuring assignments are transformed unless you use "use !extensible" directive at the beginning of the file. You can change (default) optin in your .babelrc (note that plugins now becomes nested array)

This machinery is cool for already existing bigger projects in which you may want to start using safe mode gradually: You can use safe destructuring, but optin only those files, that are already written with no-undefined-policy in mind.

impl

Either 'normal', 'immutable' or 'safe'; specifies what version of __extensible_get__ to use.

Immutable destructuring example

Use .babelrc such as:

{
  "presets": ["es2015"],
  "plugins": [["extensible-destructuring", {"mode": "optout", "impl": "immutable"}]]
}

This code works as expected:

import {fromJS} from 'immutable';
const map = fromJS({author: {name: {first: "John", last: "Doe"}, birthdate: "10-10-2010"}});
const {author: {name: {first, last}, birthdate}} = map;

Safe destructuring example

Use .babelrc such as:

{
  "presets": ["es2015"],
  "plugins": [["extensible-destructuring", {"mode": "optout", "impl": "safe"}]]
}

Now, the code:

import {fromJS} from 'immutable';
const map = {a: 10, b: 20}
const {a, b, c} = map;

logs the error to the console:

Error: Key Error: object with keys [ "a", "b" ] does not contain property c

On the other hand

const {a, b, c = null} = map;

is perfectly OK.

Differences from version 3

  • no need to patch your code at the entry-point, no need to define global __extensible_get__. We see this as a bad practice and this was the most relevant reason for doing version 4.

  • option default was renamed to normal (default is sort-of reserved keyword when dealing with ES6 import/export)

  • 'polyfill' renamed to 'runtime', which is much more accurate naming

Using your own implementation of __extensible_get__

Although we believe the existing implementations of __extensible_get__ are quite sufficient, you still might want to use your own. In such a case:

In the .babelrc, set {impl: "mymyget"}, mymyget being the name of your newly created resolver. Then in the entry-point of your code you monkey-patch it inside the package (yes, this makes it accessible also for other parts of your project):

var extensibleRuntime = require('extensible-runtime');
const mymyget = () => ...;
extensibleRuntime.mymyget = mymyget;

Under The Hood

The plugin uses global __extensible_get__ function to destructure the assignment. The number of calls to __extensible_get__ is minimized, so if one writes:

const {a: {b: {c, d, e}}} = map;

plugin uses the unique temporary variable to get result such as:

var __extensible_get__ = require('extensible-runtime').safe
var _tmp = __extensible_get__(__extensible_get__(map, "a"), "b");
var c = __extensible_get__(_tmp, "c");
var d = __extensible_get__(_tmp, "d");
var e = __extensible_get__(_tmp, "e");

More Repositories

1

adalite

A lightweight web wallet for Cardano cryptocurrency with Trezor, Ledger and BitBox02 support. Please note that the only valid domain for our wallet is adalite.io
TypeScript
236
star
2

react-custom-validation

JavaScript
131
star
3

cardano-hw-cli

Cardano CLI tool for hardware wallets
TypeScript
70
star
4

yacol

JavaScript
26
star
5

cardano-crypto.js

Cardano cryptography from the official cardano-crypto transpiled to js with emscripten
JavaScript
26
star
6

firebase-transactions

JavaScript
25
star
7

cardano-ctf

A game where Cardano developers and enthusiasts can try to exploit purposely vulnerable smart contracts and learn about the most common security issues and how to prevent them.
TypeScript
25
star
8

es-proposals

Proposals for EcmaScript language
24
star
9

persistent

Efficient Persistent Data Structures for Dart
Dart
18
star
10

ledger-app-cardano-shelley

update of Ledger Nano Cardano app code from Byron to Shelley
C
17
star
11

data-provider

Data providers for React
JavaScript
10
star
12

ledgerjs-cardano-shelley

update of Ledger Nano javascript code to Cardano Shelley
TypeScript
9
star
13

contract-generator

Automatic contract generation based on templates.
CSS
5
star
14

vlux

Very simple flux
JavaScript
4
star
15

cardano-hw-interop-lib

Library to make CBOR encoded Cardano transactions comply with CIP-0021
TypeScript
4
star
16

obedbot

JavaScript
4
star
17

audits

Vacuumlabs Auditing - Reports
3
star
18

cryptofund-utilities

Python
3
star
19

ledger-cardano-app

Ledger NanoS implementation of Cardano cryptowallet
C
3
star
20

invoicer

Invoice generator for VacuumLabs
JavaScript
3
star
21

laneplot

LanePlot - gantt chart component for jQuery
JavaScript
2
star
22

guild-mobile-dev

A guild for all developers who create mobile apps - Android, iOS, React Native, Flutter …
Dart
2
star
23

babel-plugin-superstrict

JavaScript
2
star
24

vacuumlabs-threading

JavaScript
2
star
25

report-bot

Scrape reports from slack and provide them in a meaningful format.
JavaScript
2
star
26

immutable-utils

Useful extension to immutable.js
JavaScript
2
star
27

tribal-components-ui

TypeScript
1
star
28

amazon_s3

Dart
1
star
29

spring-example

Example project with Spring Boot
Kotlin
1
star
30

js-jail

JavaScript
1
star
31

ledgerjs-cardano

JavaScript
1
star
32

wolf-society

Website for Wolf Society Foundation
TypeScript
1
star
33

eslint-config-vacuumlabs

ESLint shareable config for the Vacuumlabs JavaScript style guide
JavaScript
1
star
34

internalapps-cicd

Repository to store global build and deploy Github Actions configs
1
star
35

officebot

JavaScript
1
star
36

eshop-api

unified api to common eshops
JavaScript
1
star
37

programmable-tokens-research

Bringing on-transfer functionalities to Cardano native tokens.
1
star
38

bug-bounty

TypeScript
1
star
39

settlebeef

On-chain Beef Settlement Protocol πŸ₯©πŸ’°
TypeScript
1
star