From 3dbe69dd2b8e3dd9d697dc766f94ef992d06b0e6 Mon Sep 17 00:00:00 2001 From: Dmitry Panov Date: Mon, 28 Aug 2023 21:28:09 +0100 Subject: [PATCH] Correctly handle class declarations inside functions. Fixes #530. --- compiler.go | 2 ++ compiler_test.go | 12 ++++++++++++ 2 files changed, 14 insertions(+) diff --git a/compiler.go b/compiler.go index 2d0539dc..cbe0a99c 100644 --- a/compiler.go +++ b/compiler.go @@ -1214,6 +1214,8 @@ func (c *compiler) compileLexicalDeclarationsFuncBody(list []ast.Statement, call c.createLexicalIdBindingFuncBody(name, isConst, offset, calleeBinding) }) } + } else if cls, ok := st.(*ast.ClassDeclaration); ok { + c.createLexicalIdBindingFuncBody(cls.Class.Name.Name, false, int(cls.Class.Name.Idx)-1, calleeBinding) } } } diff --git a/compiler_test.go b/compiler_test.go index 691517fd..cb604997 100644 --- a/compiler_test.go +++ b/compiler_test.go @@ -5814,6 +5814,18 @@ func TestGeneratorMethods(t *testing.T) { testScriptWithTestLib(SCRIPT, _undefined, t) } +func TestFunctionBodyClassDecl(t *testing.T) { + const SCRIPT = ` + function as(requiredArgument = {}) { + class something { } + }; + ` + _, err := Compile("", SCRIPT, false) + if err != nil { + t.Fatal(err) + } +} + /* func TestBabel(t *testing.T) { src, err := os.ReadFile("babel7.js")