Skip to content

Commit d0f7a1d

Browse files
committed
Array concat and spread operator
Import filter for extension/metaextension classes
1 parent ef6ad0e commit d0f7a1d

7 files changed

Lines changed: 51 additions & 9 deletions

File tree

src/Errors.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ export class TSTLErrors {
3434
public static InvalidExtensionMetaExtension = (node: ts.Node) =>
3535
new TranspileError(`Cannot use both '!Extension' and '!MetaExtension' decorators on the same class.`, node)
3636

37+
public static InvalidNewExpressionOnExtension = (node: ts.Node) =>
38+
new TranspileError(`Cannot construct classes with decorator '!Extension' or '!MetaExtension'.`, node)
39+
3740
public static InvalidPropertyCall = (node: ts.Node) =>
3841
new TranspileError(`Tried to transpile a non-property call as property call.`, node)
3942

src/Transpiler.ts

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ export enum LuaTarget {
2020
}
2121

2222
export enum LuaLibFeature {
23+
ArrayConcat = "ArrayConcat",
2324
ArrayEvery = "ArrayEvery",
2425
ArrayFilter = "ArrayFilter",
2526
ArrayForEach = "ArrayForEach",
@@ -329,7 +330,16 @@ export abstract class LuaTranspiler {
329330
let result = `local ${fileImportTable} = ${reqKeyword}(${resolvedImportPath})\n`;
330331
this.importCount++;
331332

332-
imports.elements.forEach(element => {
333+
const filteredElements = imports.elements.filter(e => {
334+
const decs = tsHelper.getCustomDecorators(this.checker.getTypeAtLocation(e), this.checker);
335+
return !decs.has(DecoratorKind.Extension) && !decs.has(DecoratorKind.MetaExtension);
336+
});
337+
338+
if (filteredElements.length === 0) {
339+
return "";
340+
}
341+
342+
filteredElements.forEach(element => {
333343
const nameText = this.transpileIdentifier(element.name);
334344
if (element.propertyName) {
335345
const propertyText = this.transpileIdentifier(element.propertyName);
@@ -793,7 +803,9 @@ export abstract class LuaTranspiler {
793803
case ts.SyntaxKind.TypeOfExpression:
794804
return this.transpileTypeOfExpression(node as ts.TypeOfExpression);
795805
case ts.SyntaxKind.EmptyStatement:
796-
return "";
806+
return "";
807+
case ts.SyntaxKind.SpreadElement:
808+
return this.transpileSpreadElement(node as ts.SpreadElement);
797809
default:
798810
throw TSTLErrors.UnsupportedKind("expression", node.kind, node);
799811
}
@@ -1036,6 +1048,10 @@ export abstract class LuaTranspiler {
10361048

10371049
this.checkForLuaLibType(type);
10381050

1051+
if (classDecorators.has(DecoratorKind.Extension) || classDecorators.has(DecoratorKind.MetaExtension)) {
1052+
throw TSTLErrors.InvalidNewExpressionOnExtension(node);
1053+
}
1054+
10391055
if (classDecorators.has(DecoratorKind.CustomConstructor)) {
10401056
const customDecorator = classDecorators.get(DecoratorKind.CustomConstructor);
10411057
if (!customDecorator.args[0]) {
@@ -1193,6 +1209,8 @@ export abstract class LuaTranspiler {
11931209
const caller = this.transpileExpression(expression.expression);
11941210
const expressionName = this.transpileIdentifier(expression.name);
11951211
switch (expressionName) {
1212+
case "concat":
1213+
return this.transpileLuaLibFunction(LuaLibFeature.ArrayConcat, caller, params);
11961214
case "push":
11971215
return this.transpileLuaLibFunction(LuaLibFeature.ArrayPush, caller, params);
11981216
case "forEach":
@@ -1371,6 +1389,10 @@ export abstract class LuaTranspiler {
13711389
return escapedText;
13721390
}
13731391

1392+
public transpileSpreadElement(node: ts.SpreadElement): string {
1393+
return "unpack(" + this.transpileExpression(node.expression) + ")";
1394+
}
1395+
13741396
public transpileArrayBindingElement(name: ts.ArrayBindingElement): string {
13751397
if (ts.isOmittedExpression(name)) {
13761398
return "__";

src/lualib/ArrayConcat.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
function __TS__ArrayConcat<T>(arr1: T[], arr2: T[]): T[] {
2+
const out: T[] = [];
3+
for (let i = 0; i < arr1.length; i++) {
4+
out[i] = arr1[i];
5+
}
6+
for (let i = 0; i < arr2.length; i++) {
7+
out[i] = arr2[i];
8+
}
9+
return out;
10+
}

src/lualib/ArrayPush.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
function __TS__ArrayPush<T>(arr: T[], ...items: T[]): number {
2-
for (const item of items) {
3-
arr[arr.length] = item;
2+
/* tslint:disable */
3+
for (let i = 0; i < items.length; i++) {
4+
/* tslint:enable */
5+
arr[arr.length] = items[i];
46
}
57
return arr.length;
68
}

src/targets/Transpiler.52.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,4 +63,9 @@ export class LuaTranspiler52 extends LuaTranspiler51 {
6363
public transpileDestructingAssignmentValue(node: ts.Expression): string {
6464
return `table.unpack(${this.transpileExpression(node)})`;
6565
}
66+
67+
/** @override */
68+
public transpileSpreadElement(node: ts.SpreadElement): string {
69+
return "table.unpack(" + this.transpileExpression(node.expression) + ")";
70+
}
6671
}

src/targets/Transpiler.GLua.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import { LuaTranspiler } from "../Transpiler";
1+
import { LuaTranspilerJIT } from "./Transpiler.JIT";
22

33
import * as path from "path";
44

5-
export class LuaTranspilerGLua extends LuaTranspiler {
5+
export class LuaTranspilerGLua extends LuaTranspilerJIT {
66
/** @override */
77
public getImportPath(relativePath: string): string {
88
if (path.isAbsolute(relativePath)) {

tslint.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,9 @@
3131
"interface-name": false,
3232
"radix": false,
3333
"typedef": [
34-
true,
35-
"call-signature",
36-
"property-declaration"
34+
true,
35+
"call-signature",
36+
"property-declaration"
3737
]
3838
},
3939
"rulesDirectory": []

0 commit comments

Comments
 (0)