diff --git a/jerry-core/parser/js/js-parser-expr.c b/jerry-core/parser/js/js-parser-expr.c index 5f61d86263..83f76fe91d 100644 --- a/jerry-core/parser/js/js-parser-expr.c +++ b/jerry-core/parser/js/js-parser-expr.c @@ -788,6 +788,9 @@ parser_parse_object_literal (parser_context_t *context_p) /**< context */ parser_append_object_literal_item (context_p, literal_index, PARSER_OBJECT_PROPERTY_VALUE); +#else /* !CONFIG_DISABLE_ES2015_OBJECT_INITIALIZER */ + parser_line_counter_t start_line = context_p->token.line; + parser_line_counter_t start_column = context_p->token.column; #endif /* CONFIG_DISABLE_ES2015_OBJECT_INITIALIZER */ lexer_next_token (context_p); @@ -802,6 +805,35 @@ parser_parse_object_literal (parser_context_t *context_p) /**< context */ context_p->last_cbc.value = literal_index; break; } + + if (context_p->token.type == LEXER_RIGHT_BRACE + || context_p->token.type == LEXER_COMMA) + { + /* Re-parse the literal as common identifier. */ + context_p->source_p = context_p->token.lit_location.char_p; + context_p->line = start_line; + context_p->column = start_column; + + lexer_next_token (context_p); + + if (context_p->token.type != LEXER_LITERAL + || context_p->token.lit_location.type != LEXER_IDENT_LITERAL) + { + parser_raise_error (context_p, PARSER_ERR_IDENTIFIER_EXPECTED); + } + + lexer_construct_literal_object (context_p, + &context_p->token.lit_location, + context_p->token.lit_location.type); + + parser_emit_cbc_literal_from_token (context_p, CBC_PUSH_LITERAL); + + context_p->last_cbc_opcode = CBC_SET_LITERAL_PROPERTY; + context_p->last_cbc.value = literal_index; + + lexer_next_token (context_p); + break; + } #endif /* !CONFIG_DISABLE_ES2015_OBJECT_INITIALIZER */ if (context_p->token.type != LEXER_COLON) diff --git a/jerry-core/parser/js/js-parser-scanner.c b/jerry-core/parser/js/js-parser-scanner.c index 8635d097dc..17adefc4cb 100644 --- a/jerry-core/parser/js/js-parser-scanner.c +++ b/jerry-core/parser/js/js-parser-scanner.c @@ -959,6 +959,18 @@ parser_scan_until (parser_context_t *context_p, /**< context */ mode = SCAN_MODE_FUNCTION_ARGUMENTS; continue; } + + if (context_p->token.type == LEXER_COMMA) + { + continue; + } + + if (context_p->token.type == LEXER_RIGHT_BRACE) + { + parser_stack_pop_uint8 (context_p); + mode = SCAN_MODE_POST_PRIMARY_EXPRESSION; + break; + } #endif /* !CONFIG_DISABLE_ES2015_OBJECT_INITIALIZER */ if (context_p->token.type != LEXER_COLON) diff --git a/tests/jerry/es2015/object-initializer.js b/tests/jerry/es2015/object-initializer.js index bd3aa51f37..26a7c38669 100644 --- a/tests/jerry/es2015/object-initializer.js +++ b/tests/jerry/es2015/object-initializer.js @@ -27,3 +27,39 @@ default: assert(o.func() === 244); assert(o.ab() === 446); + +switch (1) { +default: + var ab = 5; + var cd = 6; + o = { + ab, + cd: 8, + cd + } +} + +assert(o.ab === 5); +assert(o.cd === 6); + +function exception_expected(str) { + try { + eval(str); + assert(false); + } catch (e) { + assert(e instanceof SyntaxError); + } +} + +// These forms are invalid. +exception_expected('({ true })'); +exception_expected('({ 13 })'); +exception_expected('({ "x" })'); + +switch (1) { +default: + // These forms are valid. + ({ true: true }); + ({ 13: 13 }); + ({ "x": "x" }); +}