Skip to content

Commit 133c0df

Browse files
committed
オブジェクトリテラル内で省略記法に対応
1 parent d8d9c09 commit 133c0df

4 files changed

Lines changed: 28 additions & 4 deletions

File tree

src/parser/syntaxes/expressions.ts

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -577,7 +577,7 @@ function parseReference(s: ITokenStream): Ast.Identifier {
577577

578578
/**
579579
* ```abnf
580-
* Object = "{" [ObjectKey ":" Expr *(SEP IDENT ":" Expr) [SEP]] "}"
580+
* Object = "{" [ObjectKey [":" Expr] *(SEP ObjectKey [":" Expr]) [SEP]] "}"
581581
* ```
582582
*/
583583
function parseObject(s: ITokenStream, isStatic: boolean): Ast.Obj {
@@ -592,16 +592,23 @@ function parseObject(s: ITokenStream, isStatic: boolean): Ast.Obj {
592592

593593
const map = new Map<string, Ast.Expression>();
594594
while (!s.is(TokenKind.CloseBrace)) {
595+
const startPos = s.getPos();
596+
595597
const k = parseObjectKey(s);
596598
if (map.has(k)) {
597599
throw new AiScriptSyntaxError(`Key ${k} is duplicated.`, s.getPos());
598600
}
599601
s.next();
600602

601-
s.expect(TokenKind.Colon);
602-
s.next();
603+
let v: Ast.Expression;
603604

604-
const v = parseExpr(s, isStatic);
605+
if (s.is(TokenKind.Colon)){
606+
s.next();
607+
608+
v = parseExpr(s, isStatic);
609+
} else {
610+
v = NODE("identifier", { name: k }, startPos, s.getPos());
611+
}
605612

606613
map.set(k, v);
607614

test/literals.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,14 @@ describe('literal', () => {
243243
});
244244
});
245245

246+
test.concurrent('obj (shorthand)', async () => {
247+
const res = await exe(`
248+
let a = 1
249+
<: { a }
250+
`);
251+
eq(res, OBJ(new Map([['a', NUM(1)]])));
252+
});
253+
246254
test.concurrent('obj (escaped reserved word as key)', async () => {
247255
await expect(async () => await exe(`
248256
<: {

test/syntax.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -635,6 +635,13 @@ describe('Variable declaration', () => {
635635
`);
636636
eq(res, ARR([NUM(1), NUM(2)]));
637637
});
638+
test.concurrent('destructuring declaration with shorthand', async () => {
639+
const res = await exe(`
640+
let { value } = { value: 1 }
641+
<: value
642+
`);
643+
eq(res, NUM(1));
644+
});
638645
test.concurrent('empty function', async () => {
639646
const res = await exe(`
640647
@hoge() { }
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
- オブジェクトリテラルとオブジェクトの分割代入構文内で、キーと同名の変数を参照する省略記法を使用できるようになりました。
2+
- 例えば`{ hoge }`は`{ hoge: hoge }`と等価です。

0 commit comments

Comments
 (0)