Skip to content

Commit e3f5d46

Browse files
authored
Merge pull request #87 from Perryvw/for-loop-fix
Replaced numerical fors with while loops
2 parents f1c2d70 + c20be88 commit e3f5d46

17 files changed

+83
-154
lines changed

src/CommandLineParser.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ export class CLIError extends Error {
1919

2020
const optionDeclarations: { [key: string]: yargs.Options } = {
2121
addHeader: {
22-
alias: "ah",
22+
alias: ["ah", "header"],
2323
default: true,
2424
describe: "Specify if a header will be added to compiled files.",
2525
type: "boolean",
@@ -154,9 +154,9 @@ function runDiagnostics(commandLine: ts.ParsedCommandLine) {
154154
}
155155

156156
/** Find configFile, function from ts api seems to be broken? */
157-
function findConfigFile(commandLine: ts.ParsedCommandLine) {
157+
export function findConfigFile(commandLine: ts.ParsedCommandLine) {
158158
if (!commandLine.options.project) {
159-
return;
159+
throw new CLIError(`error no base path provided, could not find config.`);
160160
}
161161
let configPath;
162162
if (path.isAbsolute(commandLine.options.project)) {

src/ForHelper.ts

Lines changed: 0 additions & 98 deletions
This file was deleted.

src/TSHelper.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ export class TSHelper {
9797
public static isTupleReturnFunction(type: ts.Type, checker: ts.TypeChecker): boolean {
9898
return type.symbol
9999
&& ((type.symbol.flags & ts.SymbolFlags.Function) !== 0)
100-
&& this.hasCustomDecorator(type, checker, "!TupleReturn");
100+
&& this.hasCustomDecorator(type, checker, "!TupleReturn");
101101
}
102102

103103
public static hasCustomDecorator(type: ts.Type, checker: ts.TypeChecker, decorator: string): boolean {

src/Transpiler.ts

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import * as ts from "typescript";
22

33
import { CompilerOptions } from "./CommandLineParser";
4-
import { ForHelper } from "./ForHelper";
54
import { TSHelper as tsEx } from "./TSHelper";
65

76
import * as path from "path";
@@ -362,24 +361,22 @@ export class LuaTranspiler {
362361
}
363362

364363
public transpileFor(node: ts.ForStatement): string {
365-
// Get iterator variable
366-
const variable = (node.initializer as ts.VariableDeclarationList).declarations[0];
367-
const identifier = variable.name as ts.Identifier;
368-
369-
// Populate three components of lua numeric for loop:
370-
const start = this.transpileExpression(variable.initializer);
371-
const end = ForHelper.GetForEnd(node.condition, this);
372-
const step = ForHelper.GetForStep(node.incrementor, this);
373-
374364
// Add header
375-
let result = this.indent + `for ${identifier.escapedText}=${start},${end},${step} do\n`;
365+
let result = "";
366+
for (const variableDeclaration of (node.initializer as ts.VariableDeclarationList).declarations) {
367+
result += this.transpileVariableDeclaration(variableDeclaration);
368+
}
369+
result += this.indent + `while(${this.transpileExpression(node.condition)}) do\n`;
376370

377371
// Add body
378372
this.pushIndent();
379373
result += this.transpileStatement(node.statement);
374+
result += this.indent + this.transpileExpression(node.incrementor) + "\n";
380375
this.popIndent();
381376

382-
return result + this.indent + "end\n";
377+
result += this.indent + "end\n";
378+
379+
return result;
383380
}
384381

385382
public transpileForOf(node: ts.ForOfStatement): string {

test/compiler/project.spec.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ export class CompilerProjectTests {
3232
"typescript_lualib.lua",
3333
"test_src/test_lib/file.lua",
3434
"test_src/main.lua")
35+
@TestCase(".",
36+
"typescript_lualib.lua",
37+
"test_src/test_lib/file.lua",
38+
"test_src/main.lua")
3539
@TestCase("test_src/main.ts",
3640
"typescript_lualib.lua",
3741
"test_src/test_lib/file.lua",
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
function TestClass.myFunction(self)
2+
end
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
MyClass = MyClass or {}
2+
MyClass.__index = MyClass
3+
function MyClass.new(construct, ...)
4+
local instance = setmetatable({}, MyClass)
5+
if construct and MyClass.constructor then MyClass.constructor(instance, ...) end
6+
return instance
7+
end
8+
MyClass.test = 0
9+
function MyClass.constructor(self)
10+
end
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
local test1 = 10
2+
3+
local test2 = 10

0 commit comments

Comments
 (0)