Skip to content

Commit

Permalink
enhance: オブジェクトリテラルのキーに文字列を書けるように (#894)
Browse files Browse the repository at this point in the history
* オブジェクトリテラルのキーに文字列を書けるように

* CHANGELOG

* issueリンク
  • Loading branch information
takejohn authored Jan 4, 2025
1 parent 97b9d15 commit d53cb31
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/parser/syntaxes/expressions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -565,7 +565,10 @@ function parseObject(s: ITokenStream, isStatic: boolean): Ast.Obj {

const map = new Map<string, Ast.Expression>();
while (!s.is(TokenKind.CloseBrace)) {
s.expect(TokenKind.Identifier);
const keyTokenKind = s.getTokenKind();
if (keyTokenKind !== TokenKind.Identifier && keyTokenKind !== TokenKind.StringLiteral) {
throw unexpectedTokenError(keyTokenKind, s.getPos());
}
const k = s.getTokenValue();
s.next();

Expand Down
2 changes: 2 additions & 0 deletions test/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,8 @@ describe('Object', () => {
});

/* 未実装
* see also: test/literals.ts > literal > obj (string key)
* issue: https://github.com/aiscript-dev/aiscript/issues/62
test.concurrent('string key', async () => {
const res = await exe(`
let obj = {
Expand Down
9 changes: 9 additions & 0 deletions test/literals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,15 @@ describe('literal', () => {
eq(res, OBJ(new Map([['a', NUM(1)], ['b', NUM(2)], ['c', NUM(3)]])));
});

test.concurrent('obj (string key)', async () => {
const res = await exe(`
<: {
"藍": 42,
}
`);
eq(res, OBJ(new Map([['藍', NUM(42)]])));
});

test.concurrent('obj and arr (separated by line break)', async () => {
const res = await exe(`
<: {
Expand Down
1 change: 1 addition & 0 deletions unreleased/object-key-string.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- オブジェクトリテラルのキーに文字列リテラルを記述できるようになりました。

0 comments on commit d53cb31

Please sign in to comment.