From 8fc37618fc22ff9e6336801071a207375c3fc321 Mon Sep 17 00:00:00 2001 From: apemanzilla Date: Wed, 6 Jun 2018 19:18:45 -0400 Subject: [PATCH 1/2] Tuples are also arrays --- src/TSHelper.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/TSHelper.ts b/src/TSHelper.ts index 980a7bfa8..c65361b47 100644 --- a/src/TSHelper.ts +++ b/src/TSHelper.ts @@ -47,7 +47,7 @@ export class TSHelper { public static isArrayType(type: ts.Type, checker: ts.TypeChecker): boolean { const typeNode = checker.typeToTypeNode(type); - return typeNode && typeNode.kind === ts.SyntaxKind.ArrayType; + return typeNode && (typeNode.kind === ts.SyntaxKind.ArrayType || typeNode.kind === ts.SyntaxKind.TupleType); } public static isTupleType(type: ts.Type, checker: ts.TypeChecker): boolean { From b3775fcc2b9f877f6a721c165c9de7f54ed61f45 Mon Sep 17 00:00:00 2001 From: apemanzilla Date: Wed, 6 Jun 2018 19:36:00 -0400 Subject: [PATCH 2/2] Remove now obsolete isTupleType call, add test --- src/Transpiler.ts | 2 +- test/translation/lua/tupleArrayUses.lua | 8 ++++++++ test/translation/ts/tupleArrayUses.ts | 8 ++++++++ 3 files changed, 17 insertions(+), 1 deletion(-) create mode 100644 test/translation/lua/tupleArrayUses.lua create mode 100644 test/translation/ts/tupleArrayUses.ts diff --git a/src/Transpiler.ts b/src/Transpiler.ts index 24000ca33..f69336651 100644 --- a/src/Transpiler.ts +++ b/src/Transpiler.ts @@ -1158,7 +1158,7 @@ export class LuaTranspiler { const index = this.transpileExpression(node.argumentExpression); const type = this.checker.getTypeAtLocation(node.expression); - if (tsHelper.isArrayType(type, this.checker) || tsHelper.isTupleType(type, this.checker)) { + if (tsHelper.isArrayType(type, this.checker)) { return `${element}[${index}+1]`; } else if (tsHelper.isStringType(type)) { return `string.sub(${element},${index}+1,${index}+1)`; diff --git a/test/translation/lua/tupleArrayUses.lua b/test/translation/lua/tupleArrayUses.lua new file mode 100644 index 000000000..396b53995 --- /dev/null +++ b/test/translation/lua/tupleArrayUses.lua @@ -0,0 +1,8 @@ +for _, value in ipairs(tuple) do +end +TS_forEach(tuple, function(v) +end +) +local num = tuple[1+1] + +local len = #tuple \ No newline at end of file diff --git a/test/translation/ts/tupleArrayUses.ts b/test/translation/ts/tupleArrayUses.ts new file mode 100644 index 000000000..e4fb21307 --- /dev/null +++ b/test/translation/ts/tupleArrayUses.ts @@ -0,0 +1,8 @@ +// test cases where tuples are used as arrays +declare const tuple: [string, number, boolean]; + +// tslint:disable:no-empty no-unused-expression +for (const value of tuple) {} // for-of loop +tuple.forEach(v => {}); // Array.prototype.forEach +const num = tuple[1]; // array access +const len = tuple.length; // array length