Browse Source

Correctly handle class declarations inside functions. Fixes #530.

Dmitry Panov 2 years ago
parent
commit
3dbe69dd2b
2 changed files with 14 additions and 0 deletions
  1. 2 0
      compiler.go
  2. 12 0
      compiler_test.go

+ 2 - 0
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)
 		}
 	}
 }

+ 12 - 0
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")