Skip to content

Commit f90bd1b

Browse files
committed
Added block transformation
1 parent 7c04fa7 commit f90bd1b

File tree

4 files changed

+47
-29
lines changed

4 files changed

+47
-29
lines changed

src/Transformer.ts

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -239,36 +239,52 @@ export class LuaTransformer {
239239

240240
const result: ts.Node[] = [];
241241

242-
let declarationNameExpression: ts.Expression;
243242
if (this.currentNamespace) {
243+
// outerNS.innerNS = outerNS.innerNS or {};
244+
// local innerNS = outerNS.innerNS
244245
const declarationNameExpression =
245246
ts.createPropertyAccess(this.currentNamespace.name, node.name as ts.Identifier);
246247
const declarationAssignment = ts.createAssignment(
247248
declarationNameExpression, ts.createLogicalOr(declarationNameExpression, ts.createObjectLiteral()));
248249

249250
result.push(declarationAssignment);
250-
// outerNS.innerNS = outerNS.innerNS or {};
251-
// local innerNS = outerNS.innerNS
251+
252+
const localDeclaration =
253+
transformHelper.createLuaVariableStatement(node.name as ts.Identifier, declarationNameExpression);
254+
255+
result.push(localDeclaration);
252256
} else if (this.isModule && (ts.getCombinedModifierFlags(node) & ts.ModifierFlags.Export)) {
253-
declarationNameExpression =
254-
ts.createPropertyAccess(ts.createIdentifier("export"), node.name as ts.Identifier);
255257
// exports.NS = exports.NS or {}
256258
// local NS = exports.NS
259+
const declarationNameExpression =
260+
ts.createPropertyAccess(ts.createIdentifier("exports"), node.name as ts.Identifier);
261+
const declarationAssignment = ts.createAssignment(
262+
declarationNameExpression, ts.createLogicalOr(declarationNameExpression, ts.createObjectLiteral()));
263+
264+
result.push(declarationAssignment);
265+
266+
const localDeclaration =
267+
transformHelper.createLuaVariableStatement(node.name as ts.Identifier, declarationNameExpression);
268+
269+
result.push(localDeclaration);
257270
} else {
258-
declarationNameExpression = node.name;
259-
// NS = NS or {}
260-
// local NS = NS
271+
// local NS = NS or {}
272+
const declarationNameExpression = node.name;
273+
const declarationAssignment = ts.createAssignment(
274+
declarationNameExpression, ts.createLogicalOr(declarationNameExpression, ts.createObjectLiteral()));
275+
276+
result.push(declarationAssignment);
261277
}
262278

263279
// Set current namespace for nested NS
264280
// Keep previous currentNS to reset after block transpilation
265281
const previousNamespace = this.currentNamespace;
266282
this.currentNamespace = node;
267283

268-
// Transform moduleblock to block and transform it
269-
if (ts.isModuleBlock(node.body)) {
284+
// Transform moduleblock to block and visit it
285+
if (node.body && ts.isModuleBlock(node.body)) {
270286
const bodyBlock = this.visitBlock(ts.createBlock(node.body.statements)) as ts.Block;
271-
result.push(bodyBlock);
287+
// result.push(bodyBlock);
272288
}
273289

274290
this.currentNamespace = previousNamespace;
@@ -430,8 +446,8 @@ export class LuaTransformer {
430446
public visitComputedPropertyName(node: ts.ComputedPropertyName): ts.VisitResult<ts.ComputedPropertyName> {
431447
return node;
432448
}
433-
public visitBlock(node: ts.Block): ts.VisitResult<ts.Block> {
434-
return node;
449+
public visitBlock(node: ts.Block): ts.Block {
450+
return ts.updateBlock(node, node.statements.map(s => this.visitor(s)) as ts.Statement[]);
435451
}
436452
public visitModuleBlock(node: ts.ModuleBlock): ts.VisitResult<ts.ModuleBlock> {
437453
return node;

src/Transpiler.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -705,6 +705,7 @@ export abstract class LuaTranspiler {
705705
this.popIndent();
706706
return ret;
707707
default:
708+
console.log(node);
708709
throw TSTLErrors.UnsupportedKind("expression", node.kind, node);
709710
}
710711
}

test/runner.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ fs.copyFileSync(
2323
testRunner.outputStream
2424
// this will use alsatian's default output if you remove this
2525
// you'll get TAP or you can add your favourite TAP reporter in it's place
26-
.pipe(TapBark.create().getPipeable())
26+
// .pipe(TapBark.create().getPipeable())
2727
// pipe to the console
2828
.pipe(process.stdout);
2929

test/translation/builder.spec.ts

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,48 @@
1-
import { Expect, Test, TestCases } from "alsatian";
1+
import { Expect, FocusTest, Test, TestCases } from "alsatian";
22

33
import * as util from "../src/util";
44

55
import * as fs from "fs";
66
import * as path from "path";
77

8-
let files: string[][] = [];
9-
let fileContents: {[key: string]: Buffer} = {};
8+
const files: string[][] = [];
9+
const fileContents: {[key: string]: Buffer} = {};
1010

11-
let tsPath = path.join(__dirname, "./ts/");
12-
let luaPath = path.join(__dirname, "./lua/")
11+
const tsPath = path.join(__dirname, "./ts/");
12+
const luaPath = path.join(__dirname, "./lua/");
1313

14-
let tsFiles = fs.readdirSync(tsPath);
15-
let luaFiles = fs.readdirSync(luaPath);
14+
const tsFiles = fs.readdirSync(tsPath);
15+
const luaFiles = fs.readdirSync(luaPath);
1616

1717
tsFiles.forEach(
1818
(tsFile, i) => {
1919
// ignore non ts files
20-
if (path.extname(tsFile) !== '.ts') {
20+
if (path.extname(tsFile) !== ".ts") {
2121
return;
2222
}
23-
let luaPart = luaFiles.indexOf(tsFile.replace('.ts', '.lua'));
23+
const luaPart = luaFiles.indexOf(tsFile.replace(".ts", ".lua"));
2424
if (luaPart === -1) {
25-
throw new Error("Missing lua counter part for test file: " + tsFile)
25+
throw new Error("Missing lua counter part for test file: " + tsFile);
2626
}
27-
let luaFile = luaFiles[luaPart];
28-
let luaFileAbsolute = path.join(luaPath, luaFile);
29-
let tsFileAbsolute = path.join(tsPath, tsFile);
27+
const luaFile = luaFiles[luaPart];
28+
const luaFileAbsolute = path.join(luaPath, luaFile);
29+
const tsFileAbsolute = path.join(tsPath, tsFile);
3030
files.push([tsFile, luaFile]);
3131
fileContents[tsFile] = fs.readFileSync(tsFileAbsolute);
3232
fileContents[luaFile] = fs.readFileSync(luaFileAbsolute);
3333
}
3434
);
3535

3636
function BufferToTestString(b: Buffer): string {
37-
return b.toString().trim().split("\r\n").join("\n")
37+
return b.toString().trim().split("\r\n").join("\n");
3838
}
3939

4040
export class FileTests {
4141

4242
@TestCases(files)
4343
@Test("Transformation Tests")
44-
public transformationTests(tsFile: string, luaFile:string) {
44+
@FocusTest
45+
public transformationTests(tsFile: string, luaFile: string) {
4546
Expect(util.transpileString(BufferToTestString(fileContents[tsFile])))
4647
.toEqual(BufferToTestString(fileContents[luaFile]));
4748
}

0 commit comments

Comments
 (0)