From c4372a3a2b940b940c761dbecb7eaaaed40b9f27 Mon Sep 17 00:00:00 2001 From: hongzhidao Date: Thu, 11 Apr 2019 21:13:03 +0800 Subject: [PATCH] Checking for duplicate function parameter names. This closes #80 issue on Github. --- njs/njs_parser.c | 6 ++++++ njs/test/njs_unit_test.c | 21 +++++++++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/njs/njs_parser.c b/njs/njs_parser.c index 5a2394f3..d53cfab7 100644 --- a/njs/njs_parser.c +++ b/njs/njs_parser.c @@ -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)); diff --git a/njs/test/njs_unit_test.c b/njs/test/njs_unit_test.c index b1903164..ede03192 100644 --- a/njs/test/njs_unit_test.c +++ b/njs/test/njs_unit_test.c @@ -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") }, -- 2.47.3