From 07802f37ddb13489cfde314716ad6181dd9b1671 Mon Sep 17 00:00:00 2001 From: Jo-Philipp Wich Date: Wed, 1 Dec 2021 12:10:05 +0100 Subject: syntax: disallow keywords in object property shorthand notation Signed-off-by: Jo-Philipp Wich --- compiler.c | 5 +++++ include/ucode/lexer.h | 2 ++ lexer.c | 15 +++++++++++++++ tests/custom/00_syntax/13_object_literals | 16 ++++++++++++++++ 4 files changed, 38 insertions(+) diff --git a/compiler.c b/compiler.c index 6c89eb9..e7bbd7f 100644 --- a/compiler.c +++ b/compiler.c @@ -1878,6 +1878,11 @@ uc_compiler_compile_object(uc_compiler_t *compiler) if (compiler->parser->prev.type == TK_LABEL && (uc_compiler_parse_check(compiler, TK_COMMA) || uc_compiler_parse_check(compiler, TK_RBRACE))) { + /* disallow keywords in this case */ + if (uc_lexer_is_keyword(compiler->parser->prev.uv)) + uc_compiler_syntax_error(compiler, compiler->parser->prev.pos, + "Invalid identifier"); + uc_compiler_emit_variable_rw(compiler, compiler->parser->prev.uv, 0); } diff --git a/include/ucode/lexer.h b/include/ucode/lexer.h index 05f5336..30849e0 100644 --- a/include/ucode/lexer.h +++ b/include/ucode/lexer.h @@ -170,6 +170,8 @@ void uc_lexer_free(uc_lexer_t *lex); uc_token_t *uc_lexer_next_token(uc_lexer_t *lex); +bool uc_lexer_is_keyword(uc_value_t *label); + bool utf8enc(char **out, int *rem, int code); const char * diff --git a/lexer.c b/lexer.c index fc2c685..c5314ae 100644 --- a/lexer.c +++ b/lexer.c @@ -1273,3 +1273,18 @@ uc_tokenname(unsigned type) return "?"; } + +bool +uc_lexer_is_keyword(uc_value_t *label) +{ + size_t i; + + if (ucv_type(label) != UC_STRING) + return false; + + for (i = 0; i < ARRAY_SIZE(reserved_words); i++) + if (!strcmp(reserved_words[i].pat, ucv_string_get(label))) + return true; + + return false; +} diff --git a/tests/custom/00_syntax/13_object_literals b/tests/custom/00_syntax/13_object_literals index 18fbbed..5ceab43 100644 --- a/tests/custom/00_syntax/13_object_literals +++ b/tests/custom/00_syntax/13_object_literals @@ -128,6 +128,22 @@ In line 2, byte 14: %} -- End -- +-- Expect stderr -- +Syntax error: Invalid identifier +In line 2, byte 8: + + ` o = { function };` + ^-- Near here + + +-- End -- + +-- Testcase -- +{% + o = { function }; +%} +-- End -- + ES2015 computed property names are supported. -- cgit v1.2.3