فهرست منبع

Fixed detection of await expressions in function parameter lists

Dmitry Panov 2 سال پیش
والد
کامیت
8f6e415ca4
4فایلهای تغییر یافته به همراه23 افزوده شده و 4 حذف شده
  1. 3 0
      parser/expression.go
  2. 1 0
      parser/scope.go
  3. 7 4
      parser/statement.go
  4. 12 0
      runtime_test.go

+ 3 - 0
parser/expression.go

@@ -851,6 +851,9 @@ func (self *_parser) parseUnaryExpression() ast.Expression {
 					To:   self.idx,
 				}
 			}
+			if self.scope.inFuncParams {
+				self.error(idx, "Illegal await-expression in formal parameters of async function")
+			}
 			return &ast.AwaitExpression{
 				Await:    idx,
 				Argument: self.parseUnaryExpression(),

+ 1 - 0
parser/scope.go

@@ -11,6 +11,7 @@ type _scope struct {
 	allowLet        bool
 	inIteration     bool
 	inSwitch        bool
+	inFuncParams    bool
 	inFunction      bool
 	inAsync         bool
 	allowAwait      bool

+ 7 - 4
parser/statement.go

@@ -167,16 +167,19 @@ func (self *_parser) parseFunctionParameterList() *ast.ParameterList {
 	opening := self.expect(token.LEFT_PARENTHESIS)
 	var list []*ast.Binding
 	var rest ast.Expression
+	if !self.scope.inFuncParams {
+		self.scope.inFuncParams = true
+		defer func() {
+			self.scope.inFuncParams = false
+		}()
+	}
 	for self.token != token.RIGHT_PARENTHESIS && self.token != token.EOF {
 		if self.token == token.ELLIPSIS {
 			self.next()
 			rest = self.reinterpretAsDestructBindingTarget(self.parseAssignmentExpression())
 			break
 		}
-		item := self.parseVariableDeclaration(&list)
-		if _, ok := item.Initializer.(*ast.AwaitExpression); ok {
-			self.error(item.Idx0(), "Illegal await-expression in formal parameters of async function")
-		}
+		self.parseVariableDeclaration(&list)
 		if self.token != token.RIGHT_PARENTHESIS {
 			self.expect(token.COMMA)
 		}

+ 12 - 0
runtime_test.go

@@ -2790,6 +2790,18 @@ func TestPanicPropagation(t *testing.T) {
 	t.Fatal("Expected panic")
 }
 
+func TestAwaitInParameters(t *testing.T) {
+	_, err := Compile("", `
+	async function g() {
+	    async function inner(a = 1 + await 1) {
+	    }
+	}
+	`, false)
+	if err == nil {
+		t.Fatal("Expected error")
+	}
+}
+
 /*
 func TestArrayConcatSparse(t *testing.T) {
 function foo(a,b,c)