Socket
Socket
Sign inDemoInstall

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

Package Overview
Dependencies
53
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

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

`_` is the new `_`!


Version published
Weekly downloads
2
Maintainers
1
Install size
8.79 kB
Created
Weekly downloads
 

Readme

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

FAQs

Last updated on 21 Dec 2018

Did you know?

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

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc