>> 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.
@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.
Most helpful comment
@xeioex take a look.
Checked for duplicate function arguments.
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.