Skip to content

Commit c055cea

Browse files
committed
Add arrayIndexModification option (tests do not work like this) + customize extension name and version
1 parent ba317ff commit c055cea

7 files changed

Lines changed: 32 additions & 4 deletions

File tree

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
2-
"name": "typescript-to-lua",
3-
"version": "1.33.2",
2+
"name": "its-typescript-to-lua",
3+
"version": "1.33.2-2",
44
"description": "A generic TypeScript to Lua transpiler. Write your code in TypeScript and publish Lua!",
55
"repository": "https://github.com/TypeScriptToLua/TypeScriptToLua",
66
"homepage": "https://typescripttolua.github.io/",

src/CompilerOptions.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ export interface TypeScriptToLuaOptions {
4646
tstlVerbose?: boolean;
4747
lua51AllowTryCatchInAsyncAwait?: boolean;
4848
measurePerformance?: boolean;
49+
arrayIndexModification?: ArrayIndexModification;
4950
}
5051

5152
export type CompilerOptions = OmitIndexSignature<ts.CompilerOptions> &
@@ -76,9 +77,17 @@ export enum BuildMode {
7677
Library = "library",
7778
}
7879

80+
export enum ArrayIndexModification {
81+
Never = "never",
82+
Always = "always",
83+
}
84+
7985
export const isBundleEnabled = (options: CompilerOptions) =>
8086
options.luaBundle !== undefined && options.luaBundleEntry !== undefined;
8187

88+
export const arrayIndexModificationEnabled = (options: CompilerOptions) =>
89+
options.arrayIndexModification !== ArrayIndexModification.Never;
90+
8291
export function validateOptions(options: CompilerOptions): ts.Diagnostic[] {
8392
const diagnostics: ts.Diagnostic[] = [];
8493

src/cli/parse.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,11 @@ export const optionDeclarations: CommandLineOption[] = [
104104
description: "Measure performance of the tstl compiler.",
105105
type: "boolean",
106106
},
107+
{
108+
name: "arrayIndexModification",
109+
description: "If array indexing operations should be modified (0 based to 1 based).",
110+
type: "string",
111+
},
107112
];
108113

109114
export function updateParsedConfigFile(parsedConfigFile: ts.ParsedCommandLine): ParsedCommandLine {

src/transformation/visitors/access.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import {
2525
import { SyntaxKind } from "typescript";
2626
import { getCustomNameFromSymbol } from "./identifier";
2727
import { getSymbolExportScope, isSymbolExported } from "../utils/export";
28+
import { arrayIndexModificationEnabled } from "../../CompilerOptions";
2829

2930
function addOneToArrayAccessArgument(
3031
context: TransformationContext,
@@ -34,7 +35,10 @@ function addOneToArrayAccessArgument(
3435
const type = context.checker.getTypeAtLocation(node.expression);
3536
const argumentType = context.checker.getTypeAtLocation(node.argumentExpression);
3637
if (isArrayType(context, type) && isNumberType(context, argumentType)) {
37-
return addToNumericExpression(index, 1);
38+
const options = context.program.getCompilerOptions();
39+
if (arrayIndexModificationEnabled(options)) {
40+
return addToNumericExpression(index, 1);
41+
}
3842
}
3943
return index;
4044
}

src/transformation/visitors/delete.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { unsupportedProperty } from "../utils/diagnostics";
66
import { isArrayType, isNumberType } from "../utils/typescript";
77
import { addToNumericExpression } from "../utils/lua-ast";
88
import { transformOptionalDeleteExpression } from "./optional-chaining";
9+
import { arrayIndexModificationEnabled } from "../../CompilerOptions";
910

1011
export const transformDeleteExpression: FunctionVisitor<ts.DeleteExpression> = (node, context) => {
1112
if (ts.isOptionalChain(node.expression)) {
@@ -27,7 +28,10 @@ export const transformDeleteExpression: FunctionVisitor<ts.DeleteExpression> = (
2728
const argumentType = context.checker.getTypeAtLocation(node.expression.argumentExpression);
2829

2930
if (isArrayType(context, type) && isNumberType(context, argumentType)) {
30-
propertyExpression = addToNumericExpression(propertyExpression, 1);
31+
const options = context.program.getCompilerOptions();
32+
if (arrayIndexModificationEnabled(options)) {
33+
propertyExpression = addToNumericExpression(propertyExpression, 1);
34+
}
3135
}
3236
}
3337

test/util.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,7 @@ export abstract class TestBuilder {
165165
moduleResolution: ts.ModuleResolutionKind.Node10,
166166
resolveJsonModule: true,
167167
sourceMap: true,
168+
arrayIndexModification: tstl.ArrayIndexModification.Never,
168169
};
169170
public setOptions(options: tstl.CompilerOptions = {}): this {
170171
this.throwIfProgramExists("setOptions");

tsconfig-schema.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,11 @@
102102
"measurePerformance": {
103103
"description": "Measure and report performance of the tstl compiler.",
104104
"type": "boolean"
105+
},
106+
"arrayIndexModification": {
107+
"description": "If array indexing operations should be modified (0 based to 1 based).",
108+
"type": "string",
109+
"enum": ["never", "always"]
105110
}
106111
},
107112
"dependencies": {

0 commit comments

Comments
 (0)