Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

babel-plugin-transform-nullish-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-nullish-operator

A Babel plugin which emulates the `a?` existence check from CoffeeScript using the `??` syntax

  • 1.0.2
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

babel-plugin-transform-nullish-operator NPM Version Build Status Coverage Status

This package is a babel plugin which can be used to emulate the Coffeescript existence operator (a?) in Babel using the new coalescing nullish operator (Stage 4 proposal).

This is completely non-standard, experimental and could break at any time.

Example

In

var fooExists = object.foo ?? EXISTS;

Out

var _object$foo;

var fooExists = (_object$foo = object.foo) !== null && _object$foo !== void 0;

NOTE: We cannot use != null here because document.all == null and document.all has been deemed not "nullish".

Installation

npm install --save-dev babel-plugin-transform-nullish-operator

Usage

{
  "plugins": ["babel-plugin-transform-nullish-operator"]
}

Via CLI

babel --plugins babel-plugin-transform-nullish-operator script.js

Via Node API

require("@babel/core").transform("code", {
  plugins: ["babel-plugin-transform-nullish-operator"]
});

Options

identifier

string, defaults to EXISTS.

Can be used to change the identifier which is used to trigger this alternative behavior of the nullish coalescing operator.

Example with {"identifier": "DO_YOU_EXIST"}

In

var fooExists = object.foo ?? DO_YOU_EXIST;

Out

var _object$foo;

var fooExists = (_object$foo = object.foo) !== null && _object$foo !== void 0;

Linting

Your linter might EXISTS as undefined (which is true, the symbol doesn't actually exists, it is only a placeholder), in that case you will need to explicitely add it in your config.

Example using eslint:

{
  ...
  "globals": {
    "EXISTS": "readonly"
  },
  ...
}

References & Thanks

This is project is heavily based on Babel's @babel/plugin-proposal-nullish-coalescing-operator (including code, tests, usage, etc).

License

MIT, Copyright (c) 2020-2020 Louis Brunner

Keywords

FAQs

Package last updated on 31 Aug 2022

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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc