diff options
author | Jo-Philipp Wich <jo@mein.io> | 2021-02-17 14:45:54 +0100 |
---|---|---|
committer | Jo-Philipp Wich <jo@mein.io> | 2021-02-17 14:58:05 +0100 |
commit | 6d7662f846e031ac52d609dc69349a1816d8e100 (patch) | |
tree | 9a4821814a8e7abad4f8e810f05fbd9caac24446 /compiler.c | |
parent | 3756806674da909ec6dc10ad25862b592792604e (diff) |
syntax: support ES2015 shorthand property names
Signed-off-by: Jo-Philipp Wich <jo@mein.io>
Diffstat (limited to 'compiler.c')
-rw-r--r-- | compiler.c | 19 |
1 files changed, 16 insertions, 3 deletions
@@ -1648,10 +1648,23 @@ uc_compiler_compile_object(uc_compiler *compiler, bool assignable) uc_compiler_emit_constant(compiler, compiler->parser->prev.pos, compiler->parser->prev.val); - uc_compiler_parse_consume(compiler, TK_COLON); + /* If the property name is a plain label followed by a comma or + * closing curly brace, treat it as ES2015 property shorthand + * notation... */ + if (compiler->parser->prev.type == TK_LABEL && + (uc_compiler_parse_check(compiler, TK_COMMA) || + uc_compiler_parse_check(compiler, TK_RBRACE))) { + uc_compiler_emit_variable_rw(compiler, + compiler->parser->prev.val, 0); + } - /* parse value expression */ - uc_compiler_parse_precedence(compiler, P_ASSIGN); + /* ... otherwise treat it as ordinary `key: value` tuple */ + else { + uc_compiler_parse_consume(compiler, TK_COLON); + + /* parse value expression */ + uc_compiler_parse_precedence(compiler, P_ASSIGN); + } /* set items on stack so far... */ if (len >= 0xfffffffe) { |