Quellcode durchsuchen

Fixed binding order for global function declarations when there are duplicate names. Fixes #344.

Dmitry Panov vor 3 Jahren
Ursprung
Commit
b5faa82cee
2 geänderte Dateien mit 33 neuen und 1 gelöschten Zeilen
  1. 5 1
      compiler.go
  2. 28 0
      compiler_test.go

+ 5 - 1
compiler.go

@@ -823,9 +823,13 @@ func (c *compiler) compileFunctionsGlobal(list []*ast.FunctionDeclaration) {
 			m[name] = i
 		}
 	}
+	idx := 0
 	for i, decl := range list {
-		if m[decl.Function.Name.Name] == i {
+		name := decl.Function.Name.Name
+		if m[name] == i {
 			c.compileFunctionLiteral(decl.Function, false).emitGetter(true)
+			c.scope.bindings[idx] = c.scope.boundNames[name]
+			idx++
 		} else {
 			leave := c.enterDummyMode()
 			c.compileFunctionLiteral(decl.Function, false).emitGetter(false)

+ 28 - 0
compiler_test.go

@@ -4194,6 +4194,7 @@ func TestArrowBoxedThis(t *testing.T) {
 
 	testScript1(SCRIPT, valueTrue, t)
 }
+
 func TestParameterOverride(t *testing.T) {
 	const SCRIPT = `
 	function f(arg) {
@@ -4264,6 +4265,33 @@ func TestTaggedTemplate(t *testing.T) {
 	testScript1(SCRIPT, valueTrue, t)
 }
 
+func TestDuplicateGlobalFunc(t *testing.T) {
+	const SCRIPT = `
+	function a(){}
+	function b(){ return "b" }
+	function c(){ return "c" }
+	function a(){}
+	b();
+	`
+
+	testScript1(SCRIPT, asciiString("b"), t)
+}
+
+func TestDuplicateFunc(t *testing.T) {
+	const SCRIPT = `
+	function f() {
+		function a(){}
+		function b(){ return "b" }
+		function c(){ return "c" }
+		function a(){}
+		return b();
+	}
+	f();
+	`
+
+	testScript1(SCRIPT, asciiString("b"), t)
+}
+
 /*
 func TestBabel(t *testing.T) {
 	src, err := ioutil.ReadFile("babel7.js")