Skip to content

Commit

Permalink
syntax: disallow keywords in object property shorthand notation
Browse files Browse the repository at this point in the history
Signed-off-by: Jo-Philipp Wich <jo@mein.io>
  • Loading branch information
jow- committed Dec 1, 2021
1 parent 54ef6c0 commit 07802f3
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 0 deletions.
5 changes: 5 additions & 0 deletions compiler.c
Expand Up @@ -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);
}
Expand Down
2 changes: 2 additions & 0 deletions include/ucode/lexer.h
Expand Up @@ -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 *
Expand Down
15 changes: 15 additions & 0 deletions lexer.c
Expand Up @@ -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;
}
16 changes: 16 additions & 0 deletions tests/custom/00_syntax/13_object_literals
Expand Up @@ -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.

Expand Down

0 comments on commit 07802f3

Please sign in to comment.