]> git.kaiwu.me - njs.git/commitdiff
Checking for duplicate function parameter names.
authorhongzhidao <hongzhidao@gmail.com>
Thu, 11 Apr 2019 13:13:03 +0000 (21:13 +0800)
committerhongzhidao <hongzhidao@gmail.com>
Thu, 11 Apr 2019 13:13:03 +0000 (21:13 +0800)
This closes #80 issue on Github.

njs/njs_parser.c
njs/test/njs_unit_test.c

index 5a2394f31692d8f661b314d7e73d6d465431c956..d53cfab76679f8905b89deca406cd712698e7531 100644 (file)
@@ -837,6 +837,12 @@ njs_parser_lambda_argument(njs_vm_t *vm, njs_parser_t *parser,
         return NJS_TOKEN_ERROR;
     }
 
+    if (arg->index > 0) {
+        njs_parser_syntax_error(vm, parser, "Duplicate parameter names");
+
+        return NJS_TOKEN_ILLEGAL;
+    }
+
     arg->index = index;
 
     ret = njs_name_copy(vm, &arg->name, njs_parser_text(parser));
index b1903164e2f98faceb8c8726fec75e7b1eb913cd..ede03192a4d01c91b76abc4a5ea5ad45b9a8be80 100644 (file)
@@ -5716,6 +5716,27 @@ static njs_unit_test_t  njs_test[] =
                  "binded.length"),
       nxt_string("0") },
 
+    { nxt_string("function f(a,a) { };"),
+      nxt_string("SyntaxError: Duplicate parameter names in 1") },
+
+    { nxt_string("function f(a,b,a) { };"),
+      nxt_string("SyntaxError: Duplicate parameter names in 1") },
+
+    { nxt_string("function f(a, ...a) { };"),
+      nxt_string("SyntaxError: Duplicate parameter names in 1") },
+
+    { nxt_string("(function(a,a) { })"),
+      nxt_string("SyntaxError: Duplicate parameter names in 1") },
+
+    { nxt_string("(function(a,...a) { })"),
+      nxt_string("SyntaxError: Duplicate parameter names in 1") },
+
+    { nxt_string("(function f(a,a) { })"),
+      nxt_string("SyntaxError: Duplicate parameter names in 1") },
+
+    { nxt_string("(function f(a,...a) { })"),
+      nxt_string("SyntaxError: Duplicate parameter names in 1") },
+
     { nxt_string("function f(a,b) { }; f.length"),
       nxt_string("2") },