Skip to content

Commit ca8442c

Browse files
rerobikaLaszloLango
authored andcommitted
Add duplicated argument check for function rest parameter (#2740)
Also add a missing word from `PARSER_ERR_FORMAL_PARAM_AFTER_REST_PARAMETER` error message. JerryScript-DCO-1.0-Signed-off-by: Robert Fancsik [email protected]
1 parent 1ff322b commit ca8442c

File tree

4 files changed

+16
-5
lines changed

4 files changed

+16
-5
lines changed

jerry-core/parser/js/js-parser-util.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1038,16 +1038,18 @@ parser_error_to_string (parser_error_t error) /**< error code */
10381038
{
10391039
return "Duplicated label.";
10401040
}
1041-
#ifndef CONFIG_DISABLE_ES2015_FUNCTION_PARAMETER_INITIALIZER
1041+
#if (!defined (CONFIG_DISABLE_ES2015_FUNCTION_PARAMETER_INITIALIZER) \
1042+
|| !defined (CONFIG_DISABLE_ES2015_FUNCTION_REST_PARAMETER))
10421043
case PARSER_ERR_DUPLICATED_ARGUMENT_NAMES:
10431044
{
10441045
return "Duplicated function argument names are not allowed here.";
10451046
}
1046-
#endif /* !CONFIG_DISABLE_ES2015_FUNCTION_PARAMETER_INITIALIZER */
1047+
#endif /* (!defined (CONFIG_DISABLE_ES2015_FUNCTION_PARAMETER_INITIALIZER)
1048+
|| !defined (CONFIG_DISABLE_ES2015_FUNCTION_REST_PARAMETER)) */
10471049
#ifndef CONFIG_DISABLE_ES2015_FUNCTION_PARAMETER_INITIALIZER
10481050
case PARSER_ERR_FORMAL_PARAM_AFTER_REST_PARAMETER:
10491051
{
1050-
return "Rest parameter must be last formal parameter.";
1052+
return "Rest parameter must be the last formal parameter.";
10511053
}
10521054
case PARSER_ERR_REST_PARAMETER_DEFAULT_INITIALIZER:
10531055
{

jerry-core/parser/js/js-parser.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2191,6 +2191,12 @@ parser_parse_function_arguments (parser_context_t *context_p, /**< context */
21912191
else if (context_p->token.type == LEXER_THREE_DOTS)
21922192
{
21932193
lexer_expect_identifier (context_p, LEXER_IDENT_LITERAL);
2194+
2195+
if (context_p->literal_count == literal_count)
2196+
{
2197+
parser_raise_error (context_p, PARSER_ERR_DUPLICATED_ARGUMENT_NAMES);
2198+
}
2199+
21942200
context_p->status_flags |= PARSER_FUNCTION_HAS_REST_PARAM;
21952201
}
21962202
#endif /* !CONFIG_DISABLE_ES2015_FUNCTION_REST_PARAMETER */

jerry-core/parser/js/js-parser.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,9 +119,11 @@ typedef enum
119119
PARSER_ERR_INVALID_RETURN, /**< return must be inside a function */
120120
PARSER_ERR_INVALID_RIGHT_SQUARE, /**< right square must terminate a block */
121121
PARSER_ERR_DUPLICATED_LABEL, /**< duplicated label */
122-
#ifndef CONFIG_DISABLE_ES2015_FUNCTION_PARAMETER_INITIALIZER
122+
#if (!defined (CONFIG_DISABLE_ES2015_FUNCTION_PARAMETER_INITIALIZER) \
123+
|| !defined (CONFIG_DISABLE_ES2015_FUNCTION_REST_PARAMETER))
123124
PARSER_ERR_DUPLICATED_ARGUMENT_NAMES, /**< duplicated argument names */
124-
#endif /* !CONFIG_DISABLE_ES2015_FUNCTION_PARAMETER_INITIALIZER */
125+
#endif /* (!defined (CONFIG_DISABLE_ES2015_FUNCTION_PARAMETER_INITIALIZER)
126+
|| !defined (CONFIG_DISABLE_ES2015_FUNCTION_REST_PARAMETER)) */
125127
#ifndef CONFIG_DISABLE_ES2015_FUNCTION_REST_PARAMETER
126128
PARSER_ERR_FORMAL_PARAM_AFTER_REST_PARAMETER, /**< formal parameter after rest parameter */
127129
PARSER_ERR_REST_PARAMETER_DEFAULT_INITIALIZER, /**< rest parameter default initializer */

tests/jerry/es2015/function-rest-parameter.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ function CheckSyntaxError (str)
3333
CheckSyntaxError ('function x (a, b, ...c, d) {}');
3434
CheckSyntaxError ('function x (... c = 5) {}');
3535
CheckSyntaxError ('function x (...) {}');
36+
CheckSyntaxError ('function x (a, a, ...a) {}');
3637
CheckSyntaxError ('"use strict" function x (...arguments) {}');
3738

3839
rest_params = ['hello', true, 7, {}, [], function () {}];

0 commit comments

Comments
 (0)