Skip to content

Commit 0c497e1

Browse files
committed
added tsHelper.isDefaultArrayCallExpression()
1 parent 258d4cf commit 0c497e1

File tree

2 files changed

+47
-7
lines changed

2 files changed

+47
-7
lines changed

src/TSHelper.ts

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,4 +247,47 @@ export class TSHelper {
247247
}
248248
return [false, null, null];
249249
}
250+
251+
public static isDefaultArrayCallExpression(node: ts.CallExpression,
252+
transpileIdentifier: (identifier: ts.Identifier) => string ): boolean {
253+
const expression = node.expression as ts.PropertyAccessExpression;
254+
const expressionName = transpileIdentifier(expression.name);
255+
switch (expressionName) {
256+
case "concat":
257+
return true;
258+
case "push":
259+
return true;
260+
case "reverse":
261+
return true;
262+
case "shift":
263+
return true;
264+
case "unshift":
265+
return true;
266+
case "sort":
267+
return true;
268+
case "pop":
269+
return true;
270+
case "forEach":
271+
return true;
272+
case "indexOf":
273+
return true;
274+
case "map":
275+
return true;
276+
case "filter":
277+
return true;
278+
case "some":
279+
return true;
280+
case "every":
281+
return true;
282+
case "slice":
283+
return true;
284+
case "splice":
285+
return true;
286+
case "join":
287+
return true;
288+
default:
289+
return false;
290+
}
291+
}
292+
250293
}

src/Transpiler.ts

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1209,11 +1209,9 @@ export abstract class LuaTranspiler {
12091209
}
12101210

12111211
// if ownerType inherits from an array, use array calls where appropriate
1212-
if (tsHelper.isArrayType(ownerType, this.checker)) {
1213-
const transpiledExpression = this.transpileArrayCallExpression(node, true);
1214-
if (transpiledExpression !== "") {
1215-
return transpiledExpression;
1216-
}
1212+
if (tsHelper.isArrayType(ownerType, this.checker)
1213+
&& tsHelper.isDefaultArrayCallExpression(node, expression => this.transpileIdentifier(expression))) {
1214+
return this.transpileArrayCallExpression(node);
12171215
}
12181216

12191217
// Get the type of the function
@@ -1312,7 +1310,7 @@ export abstract class LuaTranspiler {
13121310
}
13131311
}
13141312

1315-
public transpileArrayCallExpression(node: ts.CallExpression, noException?: boolean ): string {
1313+
public transpileArrayCallExpression(node: ts.CallExpression): string {
13161314
const expression = node.expression as ts.PropertyAccessExpression;
13171315
const params = this.transpileArguments(node.arguments);
13181316
const caller = this.transpileExpression(expression.expression);
@@ -1356,7 +1354,6 @@ export abstract class LuaTranspiler {
13561354
return `table.concat(${caller}, ${params})`;
13571355
}
13581356
default:
1359-
if (noException) { return ""; }
13601357
throw TSTLErrors.UnsupportedProperty("array", expressionName, node);
13611358
}
13621359
}

0 commit comments

Comments
 (0)