Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions src/CompilerOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export interface LuaPluginImport {
[option: string]: any;
}

export type CompilerOptions = OmitIndexSignature<ts.CompilerOptions> & {
export interface TypeScriptToLuaOptions {
buildMode?: BuildMode;
extension?: string;
luaBundle?: string;
Expand All @@ -35,8 +35,13 @@ export type CompilerOptions = OmitIndexSignature<ts.CompilerOptions> & {
plugins?: Array<ts.PluginImport | TransformerImport>;
sourceMapTraceback?: boolean;
tstlVerbose?: boolean;
[option: string]: any;
};
lua51AllowTryCatchInAsyncAwait?: boolean;
}

export type CompilerOptions = OmitIndexSignature<ts.CompilerOptions> &
TypeScriptToLuaOptions & {
[option: string]: any;
};

export enum LuaLibImportKind {
None = "none",
Expand Down
5 changes: 5 additions & 0 deletions src/cli/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,11 @@ export const optionDeclarations: CommandLineOption[] = [
description: "An array of paths that tstl should not resolve and keep as-is.",
type: "array",
},
{
name: "lua51AllowTryCatchInAsyncAwait",
description: "Always allow try/catch in async/await functions for Lua 5.1.",
type: "boolean",
},
];

export function updateParsedConfigFile(parsedConfigFile: ts.ParsedCommandLine): ParsedCommandLine {
Expand Down
15 changes: 13 additions & 2 deletions src/transformation/utils/diagnostics.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as ts from "typescript";
import { LuaTarget } from "../../CompilerOptions";
import { LuaTarget, TypeScriptToLuaOptions } from "../../CompilerOptions";
import { createSerialDiagnosticFactory } from "../../utils";
import { AnnotationKind } from "./annotations";

Expand Down Expand Up @@ -84,12 +84,23 @@ export const unsupportedRightShiftOperator = createErrorDiagnosticFactory(
"Right shift operator is not supported for target Lua 5.3. Use `>>>` instead."
);

type NonUniversalTarget = Exclude<LuaTarget, LuaTarget.Universal>;

const getLuaTargetName = (version: LuaTarget) => (version === LuaTarget.LuaJIT ? "LuaJIT" : `Lua ${version}`);
export const unsupportedForTarget = createErrorDiagnosticFactory(
(functionality: string, version: Exclude<LuaTarget, LuaTarget.Universal>) =>
(functionality: string, version: NonUniversalTarget) =>
`${functionality} is/are not supported for target ${getLuaTargetName(version)}.`
);

export const unsupportedForTargetButOverrideAvailable = createErrorDiagnosticFactory(
(functionality: string, version: NonUniversalTarget, optionName: keyof TypeScriptToLuaOptions) =>
`As a precaution, ${functionality} is/are not supported for target ${getLuaTargetName(
version
)} due to language features/limitations. ` +
`However "--${optionName}" can be used to bypass this precaution. ` +
"See https://typescripttolua.github.io/docs/configuration for more information."
);

export const unsupportedProperty = createErrorDiagnosticFactory(
(parentName: string, property: string) => `${parentName}.${property} is unsupported.`
);
Expand Down
17 changes: 14 additions & 3 deletions src/transformation/visitors/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as ts from "typescript";
import { LuaTarget } from "../..";
import * as lua from "../../LuaAST";
import { FunctionVisitor } from "../context";
import { unsupportedForTarget } from "../utils/diagnostics";
import { unsupportedForTarget, unsupportedForTargetButOverrideAvailable } from "../utils/diagnostics";
import { createUnpackCall } from "../utils/lua-ast";
import { ScopeType } from "../utils/scope";
import { isInAsyncFunction, isInGeneratorFunction } from "../utils/typescript";
Expand All @@ -14,8 +14,19 @@ import { createReturnStatement } from "./return";
export const transformTryStatement: FunctionVisitor<ts.TryStatement> = (statement, context) => {
const [tryBlock, tryScope] = transformScopeBlock(context, statement.tryBlock, ScopeType.Try);

if (context.options.luaTarget === LuaTarget.Lua51 && isInAsyncFunction(statement)) {
context.diagnostics.push(unsupportedForTarget(statement, "try/catch inside async functions", LuaTarget.Lua51));
if (
context.options.luaTarget === LuaTarget.Lua51 &&
isInAsyncFunction(statement) &&
!context.options.lua51AllowTryCatchInAsyncAwait
) {
context.diagnostics.push(
unsupportedForTargetButOverrideAvailable(
statement,
"try/catch inside async functions",
LuaTarget.Lua51,
"lua51AllowTryCatchInAsyncAwait"
)
);
return tryBlock.statements;
}

Expand Down
12 changes: 8 additions & 4 deletions test/unit/builtins/async-await.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { ModuleKind, ScriptTarget } from "typescript";
import { LuaTarget } from "../../../src";
import { awaitMustBeInAsyncFunction, unsupportedForTarget } from "../../../src/transformation/utils/diagnostics";
import { unsupportedForTargetButOverrideAvailable } from "../../../src/transformation/utils/diagnostics";
import { awaitMustBeInAsyncFunction } from "../../../src/transformation/utils/diagnostics";
import * as util from "../../util";

const promiseTestLib = `
Expand Down Expand Up @@ -540,7 +541,8 @@ describe("try/catch in async function", () => {
// Cannot execute LuaJIT with test runner
{
...util.expectEachVersionExceptJit(builder => builder.expectToEqual({ result: 4 })),
[LuaTarget.Lua51]: builder => builder.expectToHaveDiagnostics([unsupportedForTarget.code]),
[LuaTarget.Lua51]: builder =>
builder.expectToHaveDiagnostics([unsupportedForTargetButOverrideAvailable.code]),
}
);

Expand All @@ -563,7 +565,8 @@ describe("try/catch in async function", () => {
...util.expectEachVersionExceptJit(builder =>
builder.expectToEqual({ reason: "an error occurred in the async function: test error" })
),
[LuaTarget.Lua51]: builder => builder.expectToHaveDiagnostics([unsupportedForTarget.code]),
[LuaTarget.Lua51]: builder =>
builder.expectToHaveDiagnostics([unsupportedForTargetButOverrideAvailable.code]),
}
);

Expand All @@ -590,7 +593,8 @@ describe("try/catch in async function", () => {
...util.expectEachVersionExceptJit(builder =>
builder.expectToEqual({ reason: "an error occurred in the async function: test error" })
),
[LuaTarget.Lua51]: builder => builder.expectToHaveDiagnostics([unsupportedForTarget.code]),
[LuaTarget.Lua51]: builder =>
builder.expectToHaveDiagnostics([unsupportedForTargetButOverrideAvailable.code]),
}
);
});