Skip to content

Commit b9ba68b

Browse files
committed
for/if/while shortcut statements fixes
1 parent b4ea5e8 commit b9ba68b

2 files changed

Lines changed: 36 additions & 20 deletions

File tree

dist/Transpiler.js

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -163,12 +163,12 @@ var LuaTranspiler = /** @class */ (function () {
163163
var condition = this.transpileExpression(node.expression);
164164
var result = this.indent + ("if " + condition + " then\n");
165165
this.pushIndent();
166-
result += this.transpileBlock(node.thenStatement);
166+
result += this.transpileStatement(node.thenStatement);
167167
this.popIndent();
168168
if (node.elseStatement) {
169169
result += this.indent + "else\n";
170170
this.pushIndent();
171-
result += this.transpileBlock(node.elseStatement);
171+
result += this.transpileStatement(node.elseStatement);
172172
this.popIndent();
173173
}
174174
return result + this.indent + "end\n";
@@ -177,7 +177,7 @@ var LuaTranspiler = /** @class */ (function () {
177177
var condition = this.transpileExpression(node.expression);
178178
var result = this.indent + ("while " + condition + " do\n");
179179
this.pushIndent();
180-
result += this.transpileBlock(node.statement);
180+
result += this.transpileStatement(node.statement);
181181
this.popIndent();
182182
return result + this.indent + "end\n";
183183
};
@@ -193,7 +193,7 @@ var LuaTranspiler = /** @class */ (function () {
193193
var result = this.indent + ("for " + identifier.escapedText + "=" + start + "," + end + "," + step + " do\n");
194194
// Add body
195195
this.pushIndent();
196-
result += this.transpileBlock(node.statement);
196+
result += this.transpileStatement(node.statement);
197197
this.popIndent();
198198
return result + this.indent + "end\n";
199199
};
@@ -204,13 +204,13 @@ var LuaTranspiler = /** @class */ (function () {
204204
// Transpile expression
205205
var expression = this.transpileExpression(node.expression);
206206
// Use ipairs for array types, pairs otherwise
207-
var isArray = this.checker.getTypeAtLocation(node.expression).symbol.escapedName == "Array";
207+
var isArray = TSHelper_1.TSHelper.isArrayType(this.checker.getTypeAtLocation(node.expression));
208208
var pairs = isArray ? "ipairs" : "pairs";
209209
// Make header
210210
var result = this.indent + ("for _, " + identifier.escapedText + " in " + pairs + "(" + expression + ") do\n");
211211
// For body
212212
this.pushIndent();
213-
result += this.transpileBlock(node.statement);
213+
result += this.transpileStatement(node.statement);
214214
this.popIndent();
215215
return result + this.indent + "end\n";
216216
};
@@ -221,16 +221,24 @@ var LuaTranspiler = /** @class */ (function () {
221221
// Transpile expression
222222
var expression = this.transpileExpression(node.expression);
223223
// Use ipairs for array types, pairs otherwise
224-
var isArray = this.checker.getTypeAtLocation(node.expression).symbol.escapedName == "Array";
224+
var isArray = TSHelper_1.TSHelper.isArrayType(this.checker.getTypeAtLocation(node.expression));
225225
var pairs = isArray ? "ipairs" : "pairs";
226226
// Make header
227227
var result = this.indent + ("for " + identifier.escapedText + ", _ in " + pairs + "(" + expression + ") do\n");
228228
// For body
229229
this.pushIndent();
230-
result += this.transpileBlock(node.statement);
230+
result += this.transpileStatement(node.statement);
231231
this.popIndent();
232232
return result + this.indent + "end\n";
233233
};
234+
LuaTranspiler.prototype.transpileStatement = function (node) {
235+
if (ts.isBlock(node)) {
236+
return this.transpileBlock(node);
237+
}
238+
else {
239+
return this.transpileNode(node);
240+
}
241+
};
234242
LuaTranspiler.prototype.transpileSwitch = function (node) {
235243
var _this = this;
236244
var expression = this.transpileExpression(node.expression, true);
@@ -697,7 +705,7 @@ var LuaTranspiler = /** @class */ (function () {
697705
this.pushIndent();
698706
result += this.transpileBlock(node.body);
699707
this.popIndent();
700-
return result + this.indent + "end ";
708+
return result + this.indent + "end\n";
701709
};
702710
LuaTranspiler.prototype.transpileArrowFunction = function (node) {
703711
// Build parameter string
@@ -710,7 +718,7 @@ var LuaTranspiler = /** @class */ (function () {
710718
this.pushIndent();
711719
result += this.transpileBlock(node.body);
712720
this.popIndent();
713-
return result + this.indent + "end ";
721+
return result + this.indent + "end\n";
714722
}
715723
else {
716724
return "function(" + paramNames.join(",") + ") return " + this.transpileExpression(node.body) + " end";

src/Transpiler.ts

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -165,13 +165,13 @@ export class LuaTranspiler {
165165

166166
let result = this.indent + `if ${condition} then\n`;
167167
this.pushIndent();
168-
result += this.transpileBlock(node.thenStatement);
168+
result += this.transpileStatement(node.thenStatement);
169169
this.popIndent();
170170

171171
if (node.elseStatement) {
172172
result += this.indent + "else\n";
173173
this.pushIndent();
174-
result += this.transpileBlock(node.elseStatement);
174+
result += this.transpileStatement(node.elseStatement);
175175
this.popIndent();
176176
}
177177

@@ -183,7 +183,7 @@ export class LuaTranspiler {
183183

184184
let result = this.indent + `while ${condition} do\n`;
185185
this.pushIndent();
186-
result += this.transpileBlock(node.statement);
186+
result += this.transpileStatement(node.statement);
187187
this.popIndent();
188188
return result + this.indent + "end\n";
189189
}
@@ -203,7 +203,7 @@ export class LuaTranspiler {
203203

204204
// Add body
205205
this.pushIndent();
206-
result += this.transpileBlock(node.statement);
206+
result += this.transpileStatement(node.statement);
207207
this.popIndent();
208208

209209
return result + this.indent + "end\n";
@@ -218,15 +218,15 @@ export class LuaTranspiler {
218218
const expression = this.transpileExpression(node.expression);
219219

220220
// Use ipairs for array types, pairs otherwise
221-
const isArray = this.checker.getTypeAtLocation(node.expression).symbol.escapedName == "Array";
221+
const isArray = tsEx.isArrayType(this.checker.getTypeAtLocation(node.expression));
222222
const pairs = isArray ? "ipairs" : "pairs";
223223

224224
// Make header
225225
let result = this.indent + `for _, ${identifier.escapedText} in ${pairs}(${expression}) do\n`;
226226

227227
// For body
228228
this.pushIndent();
229-
result += this.transpileBlock(node.statement);
229+
result += this.transpileStatement(node.statement);
230230
this.popIndent();
231231

232232
return result + this.indent + "end\n";
@@ -241,20 +241,28 @@ export class LuaTranspiler {
241241
const expression = this.transpileExpression(node.expression);
242242

243243
// Use ipairs for array types, pairs otherwise
244-
const isArray = this.checker.getTypeAtLocation(node.expression).symbol.escapedName == "Array";
244+
const isArray = tsEx.isArrayType(this.checker.getTypeAtLocation(node.expression));
245245
const pairs = isArray ? "ipairs" : "pairs";
246246

247247
// Make header
248248
let result = this.indent + `for ${identifier.escapedText}, _ in ${pairs}(${expression}) do\n`;
249249

250250
// For body
251251
this.pushIndent();
252-
result += this.transpileBlock(node.statement);
252+
result += this.transpileStatement(node.statement);
253253
this.popIndent();
254254

255255
return result + this.indent + "end\n";
256256
}
257257

258+
transpileStatement(node: ts.Statement): string {
259+
if (ts.isBlock(node)) {
260+
return this.transpileBlock(node);
261+
} else {
262+
return this.transpileNode(node);
263+
}
264+
}
265+
258266
transpileSwitch(node: ts.SwitchStatement): string {
259267
const expression = this.transpileExpression(node.expression, true);
260268
const clauses = node.caseBlock.clauses;
@@ -784,7 +792,7 @@ export class LuaTranspiler {
784792
this.pushIndent();
785793
result += this.transpileBlock(node.body);
786794
this.popIndent();
787-
return result + this.indent + "end ";
795+
return result + this.indent + "end\n";
788796
}
789797

790798
transpileArrowFunction(node: ts.ArrowFunction): string {
@@ -799,7 +807,7 @@ export class LuaTranspiler {
799807
this.pushIndent();
800808
result += this.transpileBlock(node.body);
801809
this.popIndent();
802-
return result + this.indent + "end ";
810+
return result + this.indent + "end\n";
803811
} else {
804812
return `function(${paramNames.join(",")}) return ` + this.transpileExpression(node.body) + " end";
805813
}

0 commit comments

Comments
 (0)