Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(INT-667): add support for short key value in object expr #40

Merged
merged 2 commits into from
Sep 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading