跳到主要内容

7.2.0 Released: Private Instance Methods

· 阅读需 3 分钟

We just released a new minor version of Babel!

This release includes support for private instance methods and a bunch of bug fixes regarding Flow and TypeScript types. You can read the whole changelog on GitHub.

A lot of new contributors fixed bugs or implemented new features in this release: thanks to Gcaufy, Grigory Moroz, Paul Happ, Tim McClure and Veaceslav Cotruta!

A big shout out to Bloomberg for sponsoring the implementation of private class elements! This support for private instance methods is a follow-up to private static fields released in Babel 7.1.0.

If you or your company wants to support Babel and the evolution of JavaScript, but aren't sure how, you can donate to us on OpenCollective and, better yet, work with us on the implementation of new ECMAScript proposals directly!

Private Instance Methods (#8654)

JavaScript
class Person {
#age = 19;

#increaseAge() {
this.#age++;
}

birthday() {
this.#increaseAge();
alert("Happy Birthday!");
}
}

Thanks to Tim for implementing this proposal, and to Nicolò and Justin for the reviews!

You can test private methods by adding the @babel/plugin-proposal-private-methods plugin to your Babel configuration, or enabling the stage-3 preset in the repl.

Private accessors are also coming, and we have done some big internal refactoring that allows us to add support for private elements to decorators soon 🎉.

"Smart" Pipeline Operator Parsing (#8289)

Thanks to the work of James DiGioia and J. S. Choi, @babel/parser now also can parse the Smart Pipeline Operator, in addition to the minimal version.

We currently only support the "core" of the smart pipeline proposal, and not any additional features. We also currently support only # as a preliminary placeholder. The actual placeholder has not yet been decided, and other possibilities such as ?, @, and % may be experimentally supported by @babel/parser in the future.

JavaScript
// "Smart"
const result = 2 |> double |> 3 + # |> toStringBase(2, #); // "111"

// "Simple"
const result = 2 |> double |> (x => 3 + x) |> (x => toStringBase(2, x));

Babel implements multiple variants of this proposal to help TC39 test and gather feedback from the community. As with all proposals, expect changes in the future.

If you are directly using @babel/parser and you want to test this proposal, you can pass the proposal: "smart" option to the pipeline plugin:

JavaScript
const ast = babel.parse(code, {
plugins: [
["pipelineOperator", { proposal: "smart" }]
]
})

We don't support transpiling this syntax yet, but it will come soon.

Plugin Names (#8769)

Every official plugin now provides Babel its name. Although this doesn't affect normal Babel use, it provides a consistent identifier for each plugin. This is particularly useful for things like Time Travel, which allows you to see exactly what each plugin is doing to your code. You can see this in effect via our repl:


Discuss on Twitter