Skip to content

Commit

Permalink
feat: add support for short key value in object expr
Browse files Browse the repository at this point in the history
  • Loading branch information
koladilip committed Sep 19, 2023
1 parent 66f8dfe commit 844d4bf
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 11 deletions.
38 changes: 32 additions & 6 deletions src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -828,20 +828,46 @@ export class JsonTemplateParser {
return key;
}

private parseObjectPropExpr(): ObjectPropExpression {
let key: Expression | string | undefined;
if (!this.lexer.matchSpread()) {
key = this.parseObjectKeyExpr();
this.lexer.expect(':');
private parseShortKeyValueObjectPropExpr(): ObjectPropExpression | undefined {
if (this.lexer.matchID() && (this.lexer.match(',', 1) || this.lexer.match('}', 1))) {
const key = this.lexer.lookahead().value;
const value = this.parseBaseExpr();
return {
type: SyntaxType.OBJECT_PROP_EXPR,
key,
value,
};
}
const value = this.parseSpreadExpr();
}

private parseSpreadObjectPropExpr(): ObjectPropExpression | undefined {
if (this.lexer.matchSpread()) {
return {
type: SyntaxType.OBJECT_PROP_EXPR,
value: this.parseSpreadExpr(),
};
}
}

private parseNormalObjectPropExpr(): ObjectPropExpression {
const key = this.parseObjectKeyExpr();
this.lexer.expect(':');
const value = this.parseBaseExpr();
return {
type: SyntaxType.OBJECT_PROP_EXPR,
key,
value,
};
}

private parseObjectPropExpr(): ObjectPropExpression {
return (
this.parseSpreadObjectPropExpr() ??
this.parseShortKeyValueObjectPropExpr() ??
this.parseNormalObjectPropExpr()
);
}

private parseObjectExpr(): ObjectExpression {
this.lexer.expect('{');
const props = this.parseCommaSeparatedElements('}', () => this.parseObjectPropExpr());
Expand Down
10 changes: 5 additions & 5 deletions test/scenarios/objects/template.jt
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
let c = "c key";
let d = 3;
let b = 2;
let a = {
// short form for "a"
a: 1,
"b": 2,
"a": 1,
b,
// [c] coverts to "c key"
[c]: {
// this coverts to d: 3
d: d
}
d
},
};
a.({
a: .a,
Expand Down

0 comments on commit 844d4bf

Please sign in to comment.