Skip to content

Commit 5d4af44

Browse files
committed
Improved binding pattern handling
1 parent 15b52f8 commit 5d4af44

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

src/Compiler.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ export function compileFilesWithOptions(fileNames: string[], options: CompilerOp
5353
try {
5454
const rootDir = options.rootDir;
5555

56+
console.log(`Transpiling ${sourceFile.fileName}...`);
57+
5658
// Transpile AST
5759
const lua = createTranspiler(checker, options, sourceFile).transpileSourceFile();
5860

src/Transpiler.ts

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -473,7 +473,6 @@ export abstract class LuaTranspiler {
473473
public transpileForOf(node: ts.ForOfStatement): string {
474474
// Get variable identifier
475475
const variable = (node.initializer as ts.VariableDeclarationList).declarations[0];
476-
const identifier = variable.name as ts.Identifier;
477476

478477
// Transpile expression
479478
const expression = this.transpileExpression(node.expression);
@@ -483,7 +482,15 @@ export abstract class LuaTranspiler {
483482
const pairs = isArray ? "ipairs" : "pairs";
484483

485484
// Make header
486-
let result = this.indent + `for _, ${this.transpileIdentifier(identifier)} in ${pairs}(${expression}) do\n`;
485+
let result = "";
486+
if (ts.isIdentifier(variable.name)) {
487+
result = this.indent + `for _, ${this.transpileIdentifier(variable.name)} in ${pairs}(${expression}) do\n`;
488+
} else if (ts.isArrayBindingPattern(variable.name)) {
489+
const valueVar = "__forOfValue" + this.genVarCounter;
490+
result = this.indent + `for _, ${valueVar} in ${pairs}(${expression}) do\n`;
491+
const declaration = ts.createVariableDeclaration(variable.name, undefined, ts.createIdentifier(valueVar));
492+
result += this.indent + this.transpileVariableDeclaration(declaration);
493+
}
487494

488495
// For body
489496
this.pushIndent();
@@ -1217,6 +1224,17 @@ export abstract class LuaTranspiler {
12171224
return escapedText;
12181225
}
12191226

1227+
public transpileArrayBindingElement(name: ts.ArrayBindingElement): string {
1228+
if (ts.isOmittedExpression(name)) {
1229+
return "_";
1230+
} else if (ts.isIdentifier(name)) {
1231+
return this.transpileIdentifier(name);
1232+
} else {
1233+
const kind = tsHelper.enumName(name.kind, ts.SyntaxKind);
1234+
throw new TranspileError(`Encountered not-supported array binding element kind: ${kind}`, name);
1235+
}
1236+
}
1237+
12201238
public transpileTypeOfExpression(node: ts.TypeOfExpression): string {
12211239
const expression = this.transpileExpression(node.expression);
12221240
return `(type(${expression}) == "table" and "object" or type(${expression}))`;

0 commit comments

Comments
 (0)