Skip to content

Commit b328760

Browse files
committed
Added remaining implementations for transforms
Transfrom & Transpile works now but there are still soem feature missing Many tests (especially translation) will fail aswell.
1 parent 100ead4 commit b328760

File tree

1 file changed

+112
-31
lines changed

1 file changed

+112
-31
lines changed

src/Transformer.ts

Lines changed: 112 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -106,28 +106,20 @@ export class LuaTransformer {
106106
case ts.SyntaxKind.FunctionExpression:
107107
case ts.SyntaxKind.ArrowFunction:
108108
case ts.SyntaxKind.NewExpression:
109+
// Identifier
109110
case ts.SyntaxKind.Identifier:
110-
return this.visitExpression(node as ts.Expression);
111-
// Literal
111+
// Literals
112112
case ts.SyntaxKind.StringLiteral:
113-
return this.visitStringLiteral(node as ts.StringLiteral);
114113
case ts.SyntaxKind.NoSubstitutionTemplateLiteral:
115-
return this.visitNoSubstitutionTemplateLiteral(node as ts.NoSubstitutionTemplateLiteral);
116114
case ts.SyntaxKind.NumericLiteral:
117-
return this.visitNumericLiteral(node as ts.NumericLiteral);
118115
// Keywords
119116
case ts.SyntaxKind.TrueKeyword:
120-
return this.visitTrueKeyword(node as ts.BooleanLiteral);
121117
case ts.SyntaxKind.FalseKeyword:
122-
return this.visitFalseKeyword(node as ts.BooleanLiteral);
123118
case ts.SyntaxKind.NullKeyword:
124-
return this.visitNullKeyword(node as ts.KeywordTypeNode);
125119
case ts.SyntaxKind.UndefinedKeyword:
126-
return this.visitUndefinedKeyword(node as ts.KeywordTypeNode);
127120
case ts.SyntaxKind.ThisKeyword:
128-
return this.visitThisKeyword(node as ts.KeywordTypeNode);
129121
case ts.SyntaxKind.SuperKeyword:
130-
return this.visitSuperKeyword(node as ts.KeywordTypeNode);
122+
return this.visitExpression(node as ts.Expression);
131123
// ComputedPropertyName
132124
case ts.SyntaxKind.ComputedPropertyName:
133125
return this.visitComputedPropertyName(node as ts.ComputedPropertyName);
@@ -228,6 +220,24 @@ export class LuaTransformer {
228220
return this.visitNewExpression(node as ts.NewExpression);
229221
case ts.SyntaxKind.Identifier:
230222
return this.visitIdentifier(node as ts.Identifier);
223+
case ts.SyntaxKind.StringLiteral:
224+
return this.visitStringLiteral(node as ts.StringLiteral);
225+
case ts.SyntaxKind.NoSubstitutionTemplateLiteral:
226+
return this.visitNoSubstitutionTemplateLiteral(node as ts.NoSubstitutionTemplateLiteral);
227+
case ts.SyntaxKind.NumericLiteral:
228+
return this.visitNumericLiteral(node as ts.NumericLiteral);
229+
case ts.SyntaxKind.TrueKeyword:
230+
return this.visitTrueKeyword(node as ts.BooleanLiteral);
231+
case ts.SyntaxKind.FalseKeyword:
232+
return this.visitFalseKeyword(node as ts.BooleanLiteral);
233+
case ts.SyntaxKind.NullKeyword:
234+
return this.visitNullKeyword(node as ts.NullLiteral);
235+
case ts.SyntaxKind.UndefinedKeyword:
236+
return this.visitUndefinedKeyword(node as ts.LiteralExpression);
237+
case ts.SyntaxKind.ThisKeyword:
238+
return this.visitThisKeyword(node as ts.ThisExpression);
239+
case ts.SyntaxKind.SuperKeyword:
240+
return this.visitSuperKeyword(node as ts.SuperExpression);
231241
default:
232242
throw TSTLErrors.UnsupportedKind("Expression", node.kind, node);
233243
}
@@ -280,8 +290,62 @@ export class LuaTransformer {
280290
throw TSTLErrors.UnsupportedImportType(imports);
281291
}
282292
}
283-
public visitClassDeclaration(node: ts.ClassDeclaration): ts.VisitResult<ts.ClassDeclaration> {
284-
return node;
293+
public visitClassDeclaration(node: ts.ClassDeclaration): ts.ClassDeclaration {
294+
// TODO this should actually be converted to lua nodes
295+
return ts.updateClassDeclaration(node,
296+
node.decorators,
297+
node.modifiers,
298+
node.name,
299+
node.typeParameters,
300+
node.heritageClauses,
301+
node.members.map(elem => this.visitClassElement(elem) as ts.ClassElement));
302+
// TODO make member visitor more specific
303+
}
304+
public visitClassElement(node: ts.ClassElement): ts.ClassElement {
305+
switch (node.kind) {
306+
case ts.SyntaxKind.PropertyDeclaration:
307+
return this.visitPropertyDeclaration(node as ts.PropertyDeclaration);
308+
case ts.SyntaxKind.MethodDeclaration:
309+
return this.visitMethodDeclaration(node as ts.MethodDeclaration);
310+
case ts.SyntaxKind.Constructor:
311+
return this.visitConstructorDeclaration(node as ts.ConstructorDeclaration);
312+
}
313+
}
314+
public visitPropertyDeclaration(node: ts.PropertyDeclaration): ts.PropertyDeclaration {
315+
let updatedInitializer: ts.Expression;
316+
if (node.initializer) {
317+
updatedInitializer = this.visitExpression(node.initializer);
318+
}
319+
return ts.updateProperty(node,
320+
node.decorators,
321+
node.modifiers,
322+
node.name,
323+
node.questionToken || node.exclamationToken,
324+
node.type,
325+
updatedInitializer);
326+
}
327+
public visitMethodDeclaration(node: ts.MethodDeclaration): ts.MethodDeclaration {
328+
let updatedBody: ts.Block;
329+
if (node.body) {
330+
updatedBody = this.visitBlock(node.body);
331+
}
332+
return ts.updateMethod(node,
333+
node.decorators,
334+
node.modifiers,
335+
node.asteriskToken,
336+
node.name,
337+
node.questionToken,
338+
node.typeParameters,
339+
node.parameters,
340+
node.type,
341+
updatedBody);
342+
}
343+
public visitConstructorDeclaration(node: ts.ConstructorDeclaration): ts.ConstructorDeclaration {
344+
let updatedBody: ts.Block;
345+
if (node.body) {
346+
updatedBody = this.visitBlock(node.body);
347+
}
348+
return ts.updateConstructor(node, node.decorators, node.modifiers, node.parameters, updatedBody);
285349
}
286350
// previously transpileNamespace
287351
public visitModuleDeclaration(node: ts.ModuleDeclaration): ts.VisitResult<ts.Node> {
@@ -349,7 +413,15 @@ export class LuaTransformer {
349413
return node;
350414
}
351415
public visitFunctionDeclaration(node: ts.FunctionDeclaration): ts.VisitResult<ts.FunctionDeclaration> {
352-
return node;
416+
return ts.updateFunctionDeclaration(node,
417+
node.decorators,
418+
node.modifiers,
419+
node.asteriskToken,
420+
node.name,
421+
node.typeParameters,
422+
node.parameters,
423+
node.type,
424+
this.visitBlock(node.body));
353425
}
354426
public visitTypeAliasDeclaration(node: ts.TypeAliasDeclaration): ts.VisitResult<ts.TypeAliasDeclaration> {
355427
return undefined;
@@ -366,20 +438,29 @@ export class LuaTransformer {
366438
node.declarations.map(decl => this.visitVariableDeclaration(decl)));
367439
}
368440
public visitVariableDeclaration(node: ts.VariableDeclaration): ts.VariableDeclaration {
369-
// TODO
370-
return node;
441+
let initializer: ts.Expression;
442+
if (node.initializer) {
443+
initializer = this.visitExpression(node.initializer);
444+
}
445+
return ts.updateVariableDeclaration(node, node.name, node.type, initializer);
371446
}
372447
public visitExpressionStatement(node: ts.ExpressionStatement): ts.ExpressionStatement {
373-
return node;
448+
return ts.updateStatement(node, this.visitExpression(node.expression));
374449
}
375450
public visitReturn(node: ts.ReturnStatement): ts.ReturnStatement {
376-
return ts.updateReturn(node, this.visitExpression(node.expression));
451+
let updatedExpression: ts.Expression;
452+
if (node.expression) {
453+
updatedExpression = this.visitExpression(node.expression);
454+
}
455+
return ts.updateReturn(node, updatedExpression);
377456
}
378457
public visitIfStatement(node: ts.IfStatement): ts.IfStatement {
379-
return ts.updateIf(node,
380-
this.visitExpression(node.expression),
381-
this.visitStatement(node.thenStatement),
382-
this.visitStatement(node.elseStatement));
458+
let elseStatement: ts.Statement;
459+
if (node.elseStatement) {
460+
elseStatement = this.visitStatement(node.elseStatement);
461+
}
462+
return ts.updateIf(
463+
node, this.visitExpression(node.expression), this.visitStatement(node.thenStatement), elseStatement);
383464
}
384465
public visitWhileStatement(node: ts.WhileStatement): ts.WhileStatement {
385466
return ts.updateWhile(node, this.visitExpression(node.expression), this.visitStatement(node.statement));
@@ -572,32 +653,32 @@ export class LuaTransformer {
572653

573654
return node;
574655
}
575-
public visitStringLiteral(node: ts.StringLiteral): ts.VisitResult<ts.StringLiteral> {
656+
public visitStringLiteral(node: ts.StringLiteral): ts.StringLiteral {
576657
return node;
577658
}
578659
public visitNoSubstitutionTemplateLiteral(node: ts.NoSubstitutionTemplateLiteral):
579-
ts.VisitResult<ts.NoSubstitutionTemplateLiteral> {
660+
ts.NoSubstitutionTemplateLiteral {
580661
return node;
581662
}
582-
public visitNumericLiteral(node: ts.NumericLiteral): ts.VisitResult<ts.NumericLiteral> {
663+
public visitNumericLiteral(node: ts.NumericLiteral): ts.NumericLiteral {
583664
return node;
584665
}
585-
public visitTrueKeyword(node: ts.BooleanLiteral): ts.VisitResult<ts.BooleanLiteral> {
666+
public visitTrueKeyword(node: ts.BooleanLiteral): ts.BooleanLiteral {
586667
return node;
587668
}
588-
public visitFalseKeyword(node: ts.BooleanLiteral): ts.VisitResult<ts.BooleanLiteral> {
669+
public visitFalseKeyword(node: ts.BooleanLiteral): ts.BooleanLiteral {
589670
return node;
590671
}
591-
public visitNullKeyword(node: ts.KeywordTypeNode): ts.VisitResult<ts.KeywordTypeNode> {
672+
public visitNullKeyword(node: ts.NullLiteral): ts.NullLiteral {
592673
return node;
593674
}
594-
public visitUndefinedKeyword(node: ts.KeywordTypeNode): ts.VisitResult<ts.KeywordTypeNode> {
675+
public visitUndefinedKeyword(node: ts.LiteralExpression): ts.LiteralExpression {
595676
return node;
596677
}
597-
public visitThisKeyword(node: ts.KeywordTypeNode): ts.VisitResult<ts.KeywordTypeNode> {
678+
public visitThisKeyword(node: ts.ThisExpression): ts.ThisExpression {
598679
return node;
599680
}
600-
public visitSuperKeyword(node: ts.KeywordTypeNode): ts.VisitResult<ts.KeywordTypeNode> {
681+
public visitSuperKeyword(node: ts.SuperExpression): ts.SuperExpression {
601682
return node;
602683
}
603684
public visitComputedPropertyName(node: ts.ComputedPropertyName): ts.VisitResult<ts.ComputedPropertyName> {

0 commit comments

Comments
 (0)