Babel is a tool many JavaScript developers use to enable writing modern language features on all platforms. It’s incredibly useful, but for most of us it feels like a mysterious black box. We just drop in preset-env and we’re set! It’s great that it’s so easy to get started with Babel, but there’s also value in understanding how it works.
I got to spend a week contributing to the upcoming private class fields feature for Babel. During that week, I went from knowing nothing about how Babel works to being able to fix some failing tests and implement new edge cases. As part of this process, I had to get up to speed quickly on Babel’s plugin and transformation systems. And I’d like to share what I learned with you.
30. What I did learn to do was
• Rebase the PR off of master
• Fix some failing tests
• Handle some more edge cases
30 Newbie's Guide to Contributing to Babel - @CodingItWrong - JSCamp Chicago 2018
31. What I had to learn was:
1. How can we simulate private fields?
2. How does Babel transform in general?
3. How can Babel transform private fields?
31 Newbie's Guide to Contributing to Babel - @CodingItWrong - JSCamp Chicago 2018
32. 1. How can we
simulate
private fields?
32 Newbie's Guide to Contributing to Babel - @CodingItWrong - JSCamp Chicago 2018
33. WeakMap
const map = new WeakMap();
map.set('foo', 'bar');
const keyObject = {};
map.set(keyObject, 'baz');
33 Newbie's Guide to Contributing to Babel - @CodingItWrong - JSCamp Chicago 2018
34. export default class Countdown {
#counter;
#action;
constructor(counter, action) {
this.#counter = counter;
this.#action = action;
}
decrement() {
if (this.#counter < 1) return;
this.#counter--;
if (this.#counter === 0) {
this.#action();
}
}
}
34 Newbie's Guide to Contributing to Babel - @CodingItWrong - JSCamp Chicago 2018
35. export default class Countdown {
constructor(counter, action) {
this.#counter = counter;
this.#action = action;
}
decrement() {
if (this.#counter < 1) return;
this.#counter--;
if (this.#counter === 0) {
this.#action();
}
}
}
const _counterFields = new WeakMap();
const _actionFields = new WeakMap();
35 Newbie's Guide to Contributing to Babel - @CodingItWrong - JSCamp Chicago 2018
36. export default class Countdown {
constructor(counter, action) {
_counterFields.set(this, counter);
_actionFields.set(this, action);
}
decrement() {
if (this.#counter < 1) return;
this.#counter--;
if (this.#counter === 0) {
this.#action();
}
}
}
const _counterFields = new WeakMap();
const _actionFields = new WeakMap();
36 Newbie's Guide to Contributing to Babel - @CodingItWrong - JSCamp Chicago 2018
37. export default class Countdown {
constructor(counter, action) {
_counterFields.set(this, counter);
_actionFields.set(this, action);
}
decrement() {
let counter = _counterFields.get(this);
if (counter < 1) return;
this.#counter--;
if (this.#counter === 0) {
this.#action();
}
}
}
const _counterFields = new WeakMap();
const _actionFields = new WeakMap();
37 Newbie's Guide to Contributing to Babel - @CodingItWrong - JSCamp Chicago 2018
38. export default class Countdown {
constructor(counter, action) {
_counterFields.set(this, counter);
_actionFields.set(this, action);
}
decrement() {
let counter = _counterFields.get(this);
if (counter < 1) return;
counter--;
_counterFields.set(this, counter);
if (this.#counter === 0) {
this.#action();
}
}
}
const _counterFields = new WeakMap();
const _actionFields = new WeakMap();
38 Newbie's Guide to Contributing to Babel - @CodingItWrong - JSCamp Chicago 2018
39. export default class Countdown {
constructor(counter, action) {
_counterFields.set(this, counter);
_actionFields.set(this, action);
}
decrement() {
let counter = _counterFields.get(this);
if (counter < 1) return;
counter--;
_counterFields.set(this, counter);
if (counter === 0) {
_actionFields.get(this)();
}
}
}
const _counterFields = new WeakMap();
const _actionFields = new WeakMap();
39 Newbie's Guide to Contributing to Babel - @CodingItWrong - JSCamp Chicago 2018
40. export default class Countdown {
constructor(counter, action) {
_counterFields.set(this, counter);
_actionFields.set(this, action);
}
decrement() {
let counter = _counterFields.get(this);
if (counter < 1) return;
counter--;
_counterFields.set(this, counter);
if (counter === 0) {
_actionFields.get(this)();
}
}
}
const _counterFields = new WeakMap();
const _actionFields = new WeakMap();
40 Newbie's Guide to Contributing to Babel - @CodingItWrong - JSCamp Chicago 2018
41. 2. How does Babel
transform
in general?
41 Newbie's Guide to Contributing to Babel - @CodingItWrong - JSCamp Chicago 2018
42. Abstract Syntax Tree (AST)
A data structure to represent code that is easy to inspect and
transform
42 Newbie's Guide to Contributing to Babel - @CodingItWrong - JSCamp Chicago 2018
43. Abstract Syntax Tree (AST)
function square(n) {
return n ** 2;
}
43 Newbie's Guide to Contributing to Babel - @CodingItWrong - JSCamp Chicago 2018
44. Abstract Syntax Tree (AST)
44 Newbie's Guide to Contributing to Babel - @CodingItWrong - JSCamp Chicago 2018
45. Abstract Syntax Tree (AST)
45 Newbie's Guide to Contributing to Babel - @CodingItWrong - JSCamp Chicago 2018
46. Abstract Syntax Tree (AST)
46 Newbie's Guide to Contributing to Babel - @CodingItWrong - JSCamp Chicago 2018
47. Abstract Syntax Tree (AST)
47 Newbie's Guide to Contributing to Babel - @CodingItWrong - JSCamp Chicago 2018
48. Abstract Syntax Tree (AST)
48 Newbie's Guide to Contributing to Babel - @CodingItWrong - JSCamp Chicago 2018
49. Abstract Syntax Tree (AST)
49 Newbie's Guide to Contributing to Babel - @CodingItWrong - JSCamp Chicago 2018
50. Abstract Syntax Tree (AST)
50 Newbie's Guide to Contributing to Babel - @CodingItWrong - JSCamp Chicago 2018
51. Abstract Syntax Tree (AST)
51 Newbie's Guide to Contributing to Babel - @CodingItWrong - JSCamp Chicago 2018
52. Abstract Syntax Tree (AST)
52 Newbie's Guide to Contributing to Babel - @CodingItWrong - JSCamp Chicago 2018
55. Babel Steps
1. Read source JS files into an AST
2. Pass the AST to plugins to transform it
3. Write transformed AST back out to output JS files
55 Newbie's Guide to Contributing to Babel - @CodingItWrong - JSCamp Chicago 2018
56. 3. How does
Babel transform
private fields?
56 Newbie's Guide to Contributing to Babel - @CodingItWrong - JSCamp Chicago 2018