From 12ba662ffd4d849040383ca9c430023b52ceb6d6 Mon Sep 17 00:00:00 2001 From: Ivan Kopeykin Date: Mon, 18 Jul 2022 13:02:01 +0300 Subject: [PATCH] support static block in parser --- lib/javascript/JavascriptParser.js | 5 +++++ test/cases/parsing/es2022/counter.js | 4 ++++ test/cases/parsing/es2022/es2022.js | 20 ++++++++++++++++++++ test/cases/parsing/es2022/index.js | 7 +++++++ 4 files changed, 36 insertions(+) create mode 100644 test/cases/parsing/es2022/counter.js create mode 100644 test/cases/parsing/es2022/es2022.js create mode 100644 test/cases/parsing/es2022/index.js diff --git a/lib/javascript/JavascriptParser.js b/lib/javascript/JavascriptParser.js index 0909f48ede1..6c584a34e38 100644 --- a/lib/javascript/JavascriptParser.js +++ b/lib/javascript/JavascriptParser.js @@ -1447,6 +1447,11 @@ class JavascriptParser extends Parser { this.walkExpression(classElement.value); this.scope.topLevelScope = wasTopLevel; } + } else if (classElement.type === "StaticBlock") { + const wasTopLevel = this.scope.topLevelScope; + this.scope.topLevelScope = false; + this.walkBlockStatement(classElement); + this.scope.topLevelScope = wasTopLevel; } } } diff --git a/test/cases/parsing/es2022/counter.js b/test/cases/parsing/es2022/counter.js new file mode 100644 index 00000000000..befe6cdde9d --- /dev/null +++ b/test/cases/parsing/es2022/counter.js @@ -0,0 +1,4 @@ +let value = 0; +const add = () => value++; + +export { value, add } diff --git a/test/cases/parsing/es2022/es2022.js b/test/cases/parsing/es2022/es2022.js new file mode 100644 index 00000000000..2e382cee754 --- /dev/null +++ b/test/cases/parsing/es2022/es2022.js @@ -0,0 +1,20 @@ +import { add } from './counter'; + +export default class Foo { + static { + new Foo(add); + } + + constructor(fn) { + this.#foo = fn; + this.#add(); + } + + #foo = undefined; + + #add() { + if (#foo in this && this.#foo) { + this.#foo(); + } + } +} diff --git a/test/cases/parsing/es2022/index.js b/test/cases/parsing/es2022/index.js new file mode 100644 index 00000000000..1050bdd8a2d --- /dev/null +++ b/test/cases/parsing/es2022/index.js @@ -0,0 +1,7 @@ +import { value, add } from "./counter"; +import Foo from "./es2022"; + +it("should compile and run", () => { + new Foo(add); + expect(value).toBe(2); +});