Introducing Socket Firewall: Free, Proactive Protection for Your Software Supply Chain.Learn More
Socket
Book a DemoInstallSign in
Socket

babel-plugin-private-class-fields-to-public

Package Overview
Dependencies
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

babel-plugin-private-class-fields-to-public

`_` is the new `_`!

latest
Source
npmnpm
Version
1.0.0
Version published
Weekly downloads
0
-100%
Maintainers
1
Weekly downloads
 
Created
Source

_ is the new _!

Description

There's this TC39 proposal to add private properties to javascript classes, which are "hard private" - you can't access them from other scopes.

This is a babel plugin which adds getters and setters for all private properties, so now you can access them from other scopes. Given a class like:

class MyClass {
    #secret = "you can't see me!";
}

This plugin will transform the class to:

class MyClass {
    #secret = "you can't see me!";
    get _secret() {return this.#secret;}
    set _secret(secret) {this.#secret = secret;}
}

Uses

  • Use this in your development builds to easily access private properties from tests and when debugging.
  • Compile third party dependencies with babel to gain access to private properties.

Pitfalls

At present, the proposal is still stage 3, so this plugin is really a "proof of concept". A package author could guard against this plugin by doing something like redefining the _secret property in the constructor. We could get around this by tring to detect it and picking a different name, or by allowing a custom prefix for the accessor functions in the plugin configuration.

Motivation

There's a lot of decisions being made in the TC 39 class fields proposal based on the notion that private fields should be "hard private". There are pros and cons to "hard private" fields, but unfortunately I don't think Javascript is capable of supporting such a notion. This plugin is meant as a proof-of-concept to demonstrate an easy way to turn "hard private" into "soft private".

Keywords

babel

FAQs

Package last updated on 21 Dec 2018

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