Implement object literal parsing; Instantiate classes from object literals

Essentially, if the contextual type is a class with a constructor that takes zero arguments or doesn't have a constructor at all, an object literal can be used to initialize a new instance of that class with preset values.
This commit is contained in:
dcodeIO
2018-07-14 04:00:04 +02:00
parent 72cb1e9008
commit 9e508de69a
15 changed files with 946 additions and 12 deletions

View File

@ -39,6 +39,8 @@ import {
UnaryPostfixExpression,
UnaryExpression,
UnaryPrefixExpression,
ClassExpression,
ObjectLiteralExpression,
Statement,
BlockStatement,
@ -76,8 +78,7 @@ import {
ParameterNode,
ParameterKind,
ExportMember,
SwitchCase,
ClassExpression
SwitchCase
} from "../ast";
import {
@ -411,7 +412,8 @@ export class ASTBuilder {
// expressions
visitIdentifierExpression(node: IdentifierExpression): void {
this.sb.push(node.text);
if (node.is(CommonFlags.QUOTED)) this.visitStringLiteral(node.text);
else this.sb.push(node.text);
}
visitArrayLiteralExpression(node: ArrayLiteralExpression): void {
@ -429,6 +431,33 @@ export class ASTBuilder {
sb.push("]");
}
visitObjectLiteralExpression(node: ObjectLiteralExpression): void {
var sb = this.sb;
var names = node.names;
var values = node.values;
var numElements = names.length;
assert(numElements == values.length);
if (numElements) {
sb.push("{\n");
indent(sb, ++this.indentLevel);
this.visitNode(names[0]);
sb.push(": ");
this.visitNode(values[0]);
for (let i = 1; i < numElements; ++i) {
sb.push(",\n");
indent(sb, this.indentLevel);
this.visitNode(names[i]);
sb.push(": ");
this.visitNode(values[i]);
}
sb.push("\n");
indent(sb, --this.indentLevel);
sb.push("}");
} else {
sb.push("{}");
}
}
visitAssertionExpression(node: AssertionExpression): void {
var sb = this.sb;
if (node.assertionKind == AssertionKind.PREFIX) {
@ -542,10 +571,10 @@ export class ASTBuilder {
this.visitArrayLiteralExpression(<ArrayLiteralExpression>node);
break;
}
// case LiteralKind.OBJECT: {
// this.serializeObjectLiteralExpression(<ObjectLiteralExpression>node);
// break;
// }
case LiteralKind.OBJECT: {
this.visitObjectLiteralExpression(<ObjectLiteralExpression>node);
break;
}
default: {
assert(false);
break;