Njs: function arguments are not checked for duplicates

Created on 23 Dec 2018  路  2Comments  路  Source: nginx/njs

>> var v = function(x, x) { console.log(x); };
undefined

there should be a SyntaxError in strict mode.

>> var v = function(x, ...x) { console.log(x); };
undefined
>> v(1,2,3,4)
[2,3,4]

there should always be a SyntaxError.

Extracted from #21.

bug minor

Most helpful comment

@xeioex take a look.

Checked for duplicate function arguments.

# HG changeset patch
# User hongzhidao <[email protected]>
# Date 1554988383 -28800
# Node ID 8642d0535a5d073c30c04a46fbbac05771eb92c4
# Parent  0c8a6246a4afee28887badccf43c29e9fa4a27db
Checked for duplicate function arguments.

diff -r 0c8a6246a4af -r 8642d0535a5d njs/njs_parser.c
--- a/njs/njs_parser.c  Thu Apr 11 15:00:17 2019 +0300
+++ b/njs/njs_parser.c  Thu Apr 11 21:13:03 2019 +0800
@@ -837,6 +837,13 @@ njs_parser_lambda_argument(njs_vm_t *vm,
         return NJS_TOKEN_ERROR;
     }

+    if (arg->index > 0) {
+        njs_parser_syntax_error(vm, parser,
+                        "Duplicate parameter name not allowed in this context");
+
+        return NJS_TOKEN_ILLEGAL;
+    }
+
     arg->index = index;

     ret = njs_name_copy(vm, &arg->name, njs_parser_text(parser));
diff -r 0c8a6246a4af -r 8642d0535a5d njs/test/njs_unit_test.c
--- a/njs/test/njs_unit_test.c  Thu Apr 11 15:00:17 2019 +0300
+++ b/njs/test/njs_unit_test.c  Thu Apr 11 21:13:03 2019 +0800
@@ -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 name not allowed in this context in 1") },
+
+    { nxt_string("function f(a,b,a) { };"),
+      nxt_string("SyntaxError: Duplicate parameter name not allowed in this context in 1") },
+
+    { nxt_string("function f(a, ...a) { };"),
+      nxt_string("SyntaxError: Duplicate parameter name not allowed in this context in 1") },
+
+    { nxt_string("(function(a,a) { })"),
+      nxt_string("SyntaxError: Duplicate parameter name not allowed in this context in 1") },
+
+    { nxt_string("(function(a,...a) { })"),
+      nxt_string("SyntaxError: Duplicate parameter name not allowed in this context in 1") },
+
+    { nxt_string("(function f(a,a) { })"),
+      nxt_string("SyntaxError: Duplicate parameter name not allowed in this context in 1") },
+
+    { nxt_string("(function f(a,...a) { })"),
+      nxt_string("SyntaxError: Duplicate parameter name not allowed in this context in 1") },
+
     { nxt_string("function f(a,b) { }; f.length"),
       nxt_string("2") },

Refer to http://www.ecma-international.org/ecma-262/6.0/#sec-functiondeclarationinstantiation
NOTE Early errors ensure that duplicate parameter names can only occur in non-strict functions that do not have parameter default values or rest parameters.

@drsm welcome to test.

All 2 comments

@xeioex take a look.

Checked for duplicate function arguments.

# HG changeset patch
# User hongzhidao <[email protected]>
# Date 1554988383 -28800
# Node ID 8642d0535a5d073c30c04a46fbbac05771eb92c4
# Parent  0c8a6246a4afee28887badccf43c29e9fa4a27db
Checked for duplicate function arguments.

diff -r 0c8a6246a4af -r 8642d0535a5d njs/njs_parser.c
--- a/njs/njs_parser.c  Thu Apr 11 15:00:17 2019 +0300
+++ b/njs/njs_parser.c  Thu Apr 11 21:13:03 2019 +0800
@@ -837,6 +837,13 @@ njs_parser_lambda_argument(njs_vm_t *vm,
         return NJS_TOKEN_ERROR;
     }

+    if (arg->index > 0) {
+        njs_parser_syntax_error(vm, parser,
+                        "Duplicate parameter name not allowed in this context");
+
+        return NJS_TOKEN_ILLEGAL;
+    }
+
     arg->index = index;

     ret = njs_name_copy(vm, &arg->name, njs_parser_text(parser));
diff -r 0c8a6246a4af -r 8642d0535a5d njs/test/njs_unit_test.c
--- a/njs/test/njs_unit_test.c  Thu Apr 11 15:00:17 2019 +0300
+++ b/njs/test/njs_unit_test.c  Thu Apr 11 21:13:03 2019 +0800
@@ -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 name not allowed in this context in 1") },
+
+    { nxt_string("function f(a,b,a) { };"),
+      nxt_string("SyntaxError: Duplicate parameter name not allowed in this context in 1") },
+
+    { nxt_string("function f(a, ...a) { };"),
+      nxt_string("SyntaxError: Duplicate parameter name not allowed in this context in 1") },
+
+    { nxt_string("(function(a,a) { })"),
+      nxt_string("SyntaxError: Duplicate parameter name not allowed in this context in 1") },
+
+    { nxt_string("(function(a,...a) { })"),
+      nxt_string("SyntaxError: Duplicate parameter name not allowed in this context in 1") },
+
+    { nxt_string("(function f(a,a) { })"),
+      nxt_string("SyntaxError: Duplicate parameter name not allowed in this context in 1") },
+
+    { nxt_string("(function f(a,...a) { })"),
+      nxt_string("SyntaxError: Duplicate parameter name not allowed in this context in 1") },
+
     { nxt_string("function f(a,b) { }; f.length"),
       nxt_string("2") },

Refer to http://www.ecma-international.org/ecma-262/6.0/#sec-functiondeclarationinstantiation
NOTE Early errors ensure that duplicate parameter names can only occur in non-strict functions that do not have parameter default values or rest parameters.

@drsm welcome to test.

@hongzhidao

Thanks! Committed with minor changes.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

pavelsevcik picture pavelsevcik  路  4Comments

fishioon picture fishioon  路  3Comments

axipo picture axipo  路  3Comments

xeioex picture xeioex  路  3Comments

porunov picture porunov  路  4Comments