changeset 879:4df50ca085e7

Checking for duplicate function parameter names. This closes #80 issue on Github.
author hongzhidao <hongzhidao@gmail.com>
date Thu, 11 Apr 2019 21:13:03 +0800
parents b9d619068453
children bcf988ed8c08
files njs/njs_parser.c njs/test/njs_unit_test.c
diffstat 2 files changed, 27 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/njs/njs_parser.c	Thu Apr 11 16:32:06 2019 +0300
+++ b/njs/njs_parser.c	Thu Apr 11 21:13:03 2019 +0800
@@ -837,6 +837,12 @@
         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));
--- a/njs/test/njs_unit_test.c	Thu Apr 11 16:32:06 2019 +0300
+++ b/njs/test/njs_unit_test.c	Thu Apr 11 21:13:03 2019 +0800
@@ -5716,6 +5716,27 @@
                  "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") },