diff options
author | Jo-Philipp Wich <jo@mein.io> | 2021-12-01 12:10:05 +0100 |
---|---|---|
committer | Jo-Philipp Wich <jo@mein.io> | 2021-12-01 12:10:05 +0100 |
commit | 07802f37ddb13489cfde314716ad6181dd9b1671 (patch) | |
tree | 0a119aafdbfdbdcc5fd7cf87f513c525c27e6d2a | |
parent | 54ef6c09116430e5ce35777bd899f750d84e4dc1 (diff) |
syntax: disallow keywords in object property shorthand notation
Signed-off-by: Jo-Philipp Wich <jo@mein.io>
-rw-r--r-- | compiler.c | 5 | ||||
-rw-r--r-- | include/ucode/lexer.h | 2 | ||||
-rw-r--r-- | lexer.c | 15 | ||||
-rw-r--r-- | tests/custom/00_syntax/13_object_literals | 16 |
4 files changed, 38 insertions, 0 deletions
@@ -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 * @@ -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. |