Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add doc for private-property-in-object transform #2235

Merged
merged 5 commits into from May 24, 2020
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
123 changes: 123 additions & 0 deletions docs/plugin-proposal-private-property-in-object.md
@@ -0,0 +1,123 @@
---
id: babel-plugin-proposal-private-property-in-object
title: @babel/plugin-proposal-private-property-in-object
sidebar_label: proposal-private-property-in-object
---

## Example

**In**

```javascript
class Foo {
#bar = "bar";

test(obj) {
return #bar in obj;
}
}
```

**Out**

```javascript
class Foo {
constructor() {
_bar.set(this, {
writable: true,
value: "bar"
});
}

test() {
return _bar.has(this);
}

}

var _bar = new WeakMap();
```

## Installation

```sh
npm install --save-dev @babel/plugin-property-private-property-in-object
```

## Usage

### With a configuration file (Recommended)

```json
{
"plugins": ["@babel/plugin-property-private-property-in-object
jridgewell marked this conversation as resolved.
Show resolved Hide resolved
}
```

### Via CLI

```sh
babel --plugins @babel/plugin-property-private-property-in-object
```

### Via Node API

```javascript
require("@babel/core").transform("code", {
plugins: ["@babel/plugin-property-private-property-in-object
jridgewell marked this conversation as resolved.
Show resolved Hide resolved
});
```

## Options

### `loose`

`boolean`, defaults to `false`.

> Note: The `loose` mode configuration setting _must_ be the same as [`@babel/proposal-class-properties`](plugin-proposal-class-properties.md).

When true, private property `in` expressions will check for a own property on the object, instead of checking for presence inside a `WeakSet`. This results in improved
jridgewell marked this conversation as resolved.
Show resolved Hide resolved
performance and debugging (normal property access vs `.get()`) at the expense
of potentially leaking "privates" via things like `Object.getOwnPropertyNames`.


#### Example

**In**

```javascript
class Foo {
#bar = "bar";

test(obj) {
return #bar in obj;
}
}
```

**Out**

```javascript
class Foo {
constructor() {
Object.defineProperty(this, _bar, {
writable: true,
value: "bar"
});
}

test() {
return Object.prototype.hasOwnProperty.call(this, _bar);
}

}

var _bar = babelHelpers.classPrivateFieldLooseKey("bar");
```

> You can read more about configuring plugin options [here](https://babeljs.io/docs/en/plugins#plugin-options)

## References

* [Proposal: Ergonomic brand checks for Private Fields](https://github.com/tc39/proposal-private-fields-in-in)

1 change: 1 addition & 0 deletions docs/plugins.md
Expand Up @@ -93,6 +93,7 @@ These plugins apply transformations to your code.
- [pipeline-operator](plugin-proposal-pipeline-operator.md)
- [private-methods](plugin-proposal-private-methods.md)
- [throw-expressions](plugin-proposal-throw-expressions.md)
- [private-property-in-object](plugin-proposal-private-property-in-object.md)

### Minification

Expand Down