Support class-static-block proposal (#9476)

* Support class-static-block proposal

* Update changelog
This commit is contained in:
Sosuke Suzuki authored and GitHub committed 2020-10-23 20:09:07 +09:00
1 parent d9f5630ff5
commit 13f3336bba
9 files changed
+172 -4

No files matched your search

+17 -1
View File
@@ -1,4 +1,4 @@
#### Update to `@babel/parser` 7.12 (#9408 by @sosukesuzuki)
#### Update to `@babel/parser` 7.12 (#9408, #9476 by @sosukesuzuki)
Updated the JavaScript parser to [`@babel/parser` 7.12](https://babeljs.io/blog/2020/10/15/7.12.0). This fixes several bugs and supports some new syntax.
@@ -20,3 +20,19 @@ Due to a [bug in `@babel/parser`](https://github.com/babel/babel/issues/12209),
let happy = "happy";
export { happy as "😃" };
```
##### Support class static blocks
```js
class C {
static #x = 42;
static y;
static {
try {
this.y = doSomethingWith(this.#x);
} catch {
this.y = "unknown";
}
}
}
```
+1
View File
@@ -39,6 +39,7 @@ function babelOptions({ sourceType, extraPlugins = [] }) {
["recordAndTuple", { syntaxType: "hash" }],
"decimal",
"moduleStringNames",
"classStaticBlock",
...extraPlugins,
],
tokens: true,
+9 -3
View File
@@ -894,11 +894,16 @@ function printPathNoParens(path, options, print, args) {
case "Import":
return "import";
case "TSModuleBlock":
case "BlockStatement": {
case "BlockStatement":
case "StaticBlock": {
const naked = path.call((bodyPath) => {
return printStatementSequence(bodyPath, options, print);
}, "body");
if (n.type === "StaticBlock") {
parts.push("static ");
}
const hasContent = n.body.some((node) => node.type !== "EmptyStatement");
const hasDirectives = n.directives && n.directives.length > 0;
@@ -920,9 +925,10 @@ function printPathNoParens(path, options, print, args) {
parent.type === "DoExpression" ||
(parent.type === "CatchClause" && !parentParent.finalizer) ||
parent.type === "TSModuleDeclaration" ||
parent.type === "TSDeclareFunction")
parent.type === "TSDeclareFunction" ||
n.type === "StaticBlock")
) {
return "{}";
return concat([...parts, "{}"]);
}
parts.push("{");
@@ -356,6 +356,40 @@ console.log(Bork.staticFunction()); // > "babelIsCool"
================================================================================
`;
exports[`class-static-block.js format 1`] = `
====================================options=====================================
parsers: ["babel", "babel-ts", "babel-flow"]
printWidth: 80
| printWidth
=====================================input======================================
class C {
static #x = 42;
static y;
static {
try {
this.y = doSomethingWith(this.#x);
} catch {
this.y = "unknown";
}
}
}
=====================================output=====================================
class C {
static #x = 42;
static y;
static {
try {
this.y = doSomethingWith(this.#x);
} catch {
this.y = "unknown";
}
}
}
================================================================================
`;
exports[`decimal.js format 1`] = `
====================================options=====================================
parsers: ["babel", "babel-ts", "babel-flow"]
@@ -0,0 +1,11 @@
class C {
static #x = 42;
static y;
static {
try {
this.y = doSomethingWith(this.#x);
} catch {
this.y = "unknown";
}
}
}
+1
View File
@@ -27,6 +27,7 @@ run_spec(__dirname, ["babel", "babel-ts", "babel-flow"], {
"v8intrinsic.js",
"optional-chaining.js",
"module-string-names.js",
"class-static-block.js",
],
},
});
@@ -0,0 +1,69 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`class-static-block.js format 1`] = `
====================================options=====================================
parsers: ["babel"]
printWidth: 80
| printWidth
=====================================input======================================
class C {
static #x = 42;
static y;
static {
try {
this.y = doSomethingWith(this.#x);
} catch {
this.y = "unknown";
}
}
}
class Foo {
static {}
}
class A1 {
static {
foo;
}
}
class A2 {
static {
foo;
bar;
}
}
=====================================output=====================================
class C {
static #x = 42;
static y;
static {
try {
this.y = doSomethingWith(this.#x);
} catch {
this.y = "unknown";
}
}
}
class Foo {
static {}
}
class A1 {
static {
foo;
}
}
class A2 {
static {
foo;
bar;
}
}
================================================================================
`;
@@ -0,0 +1,29 @@
class C {
static #x = 42;
static y;
static {
try {
this.y = doSomethingWith(this.#x);
} catch {
this.y = "unknown";
}
}
}
class Foo {
static {}
}
class A1 {
static {
foo;
}
}
class A2 {
static {
foo;
bar;
}
}
@@ -0,0 +1 @@
run_spec(__dirname, ["babel"], { errors: { espree: true } });