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 | |
parent | 3756806674da909ec6dc10ad25862b592792604e (diff) |
syntax: support ES2015 shorthand property names
Signed-off-by: Jo-Philipp Wich <jo@mein.io>
-rw-r--r-- | compiler.c | 19 | ||||
-rw-r--r-- | tests/00_syntax/13_object_literals | 36 |
2 files changed, 52 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) { diff --git a/tests/00_syntax/13_object_literals b/tests/00_syntax/13_object_literals index 67dbd6c..e7926ad 100644 --- a/tests/00_syntax/13_object_literals +++ b/tests/00_syntax/13_object_literals @@ -91,3 +91,39 @@ of object properties into other objects. ]), "\n"); %} -- End -- + + +ES2015 short hand property notation is supported as well. + +-- Expect stdout -- +{ "a": 123, "b": true, "c": "test" } +-- End -- + +-- Testcase -- +{% + a = 123; + b = true; + c = "test"; + + o = { a, b, c }; + + print(o, "\n"); +%} +-- End -- + +-- Expect stderr -- +Syntax error: Unexpected token +Expecting ':' +In line 2, byte 14: + + ` o = { "foo" };` + Near here ------^ + + +-- End -- + +-- Testcase -- +{% + o = { "foo" }; +%} +-- End -- |