typescript-to-lua
Advanced tools
Comparing version 1.4.4 to 1.5.0
@@ -43,2 +43,7 @@ "use strict"; | ||
{ | ||
name: "noImplicitGlobalVariables", | ||
description: 'Specify to prevent implicitly turning "normal" variants into global variables in the transpiled output.', | ||
type: "boolean", | ||
}, | ||
{ | ||
name: "noImplicitSelf", | ||
@@ -45,0 +50,0 @@ description: 'If "this" is implicitly considered an any type, do not generate a self parameter.', |
@@ -26,2 +26,3 @@ import * as ts from "typescript"; | ||
luaPlugins?: LuaPluginImport[]; | ||
noImplicitGlobalVariables?: boolean; | ||
noImplicitSelf?: boolean; | ||
@@ -28,0 +29,0 @@ noHeader?: boolean; |
import * as ts from "typescript"; | ||
import * as lua from "../../LuaAST"; | ||
import { TransformationContext } from "../context"; | ||
import { PropertyCallExpression } from "../visitors/call"; | ||
export declare function transformArrayConstructorCall(context: TransformationContext, node: PropertyCallExpression): lua.Expression | undefined; | ||
export declare function transformArrayPrototypeCall(context: TransformationContext, node: PropertyCallExpression): lua.Expression | undefined; | ||
export declare function transformArrayConstructorCall(context: TransformationContext, node: ts.CallExpression, calledMethod: ts.PropertyAccessExpression): lua.Expression | undefined; | ||
export declare function transformArrayPrototypeCall(context: TransformationContext, node: ts.CallExpression, calledMethod: ts.PropertyAccessExpression): lua.Expression | undefined; | ||
export declare function transformArrayProperty(context: TransformationContext, node: ts.PropertyAccessExpression): lua.UnaryExpression | undefined; |
@@ -12,7 +12,6 @@ "use strict"; | ||
const lua_ast_1 = require("../utils/lua-ast"); | ||
function transformArrayConstructorCall(context, node) { | ||
const expression = node.expression; | ||
function transformArrayConstructorCall(context, node, calledMethod) { | ||
const signature = context.checker.getResolvedSignature(node); | ||
const params = (0, call_1.transformArguments)(context, node.arguments, signature); | ||
const expressionName = expression.name.text; | ||
const expressionName = calledMethod.name.text; | ||
switch (expressionName) { | ||
@@ -26,3 +25,3 @@ case "from": | ||
default: | ||
context.diagnostics.push((0, diagnostics_1.unsupportedProperty)(expression.name, "Array", expressionName)); | ||
context.diagnostics.push((0, diagnostics_1.unsupportedProperty)(calledMethod.name, "Array", expressionName)); | ||
} | ||
@@ -50,7 +49,6 @@ } | ||
} | ||
function transformArrayPrototypeCall(context, node) { | ||
const expression = node.expression; | ||
function transformArrayPrototypeCall(context, node, calledMethod) { | ||
const signature = context.checker.getResolvedSignature(node); | ||
const [caller, params] = (0, call_1.transformCallAndArguments)(context, expression.expression, node.arguments, signature); | ||
const expressionName = expression.name.text; | ||
const [caller, params] = (0, call_1.transformCallAndArguments)(context, calledMethod.expression, node.arguments, signature); | ||
const expressionName = calledMethod.name.text; | ||
switch (expressionName) { | ||
@@ -109,3 +107,3 @@ case "concat": | ||
case "join": | ||
const callerType = context.checker.getTypeAtLocation(expression.expression); | ||
const callerType = context.checker.getTypeAtLocation(calledMethod.expression); | ||
const elementType = context.checker.getElementTypeOfArrayType(callerType); | ||
@@ -131,3 +129,3 @@ if (elementType && ((0, typescript_1.isStringType)(context, elementType) || (0, typescript_1.isNumberType)(context, elementType))) { | ||
default: | ||
context.diagnostics.push((0, diagnostics_1.unsupportedProperty)(expression.name, "array", expressionName)); | ||
context.diagnostics.push((0, diagnostics_1.unsupportedProperty)(calledMethod.name, "array", expressionName)); | ||
} | ||
@@ -134,0 +132,0 @@ } |
@@ -0,4 +1,4 @@ | ||
import * as ts from "typescript"; | ||
import * as lua from "../../LuaAST"; | ||
import { TransformationContext } from "../context"; | ||
import { PropertyCallExpression } from "../visitors/call"; | ||
export declare function transformConsoleCall(context: TransformationContext, expression: PropertyCallExpression): lua.Expression | undefined; | ||
export declare function transformConsoleCall(context: TransformationContext, node: ts.CallExpression, calledMethod: ts.PropertyAccessExpression): lua.Expression | undefined; |
@@ -9,7 +9,6 @@ "use strict"; | ||
const isStringFormatTemplate = (node) => ts.isStringLiteral(node) && node.text.includes("%"); | ||
function transformConsoleCall(context, expression) { | ||
const method = expression.expression; | ||
const methodName = method.name.text; | ||
const signature = context.checker.getResolvedSignature(expression); | ||
const parameters = (0, call_1.transformArguments)(context, expression.arguments, signature); | ||
function transformConsoleCall(context, node, calledMethod) { | ||
const methodName = calledMethod.name.text; | ||
const signature = context.checker.getResolvedSignature(node); | ||
const parameters = (0, call_1.transformArguments)(context, node.arguments, signature); | ||
switch (methodName) { | ||
@@ -20,3 +19,3 @@ case "error": | ||
case "warn": | ||
if (expression.arguments.length > 0 && isStringFormatTemplate(expression.arguments[0])) { | ||
if (node.arguments.length > 0 && isStringFormatTemplate(node.arguments[0])) { | ||
// print(string.format([arguments])) | ||
@@ -29,3 +28,3 @@ const stringFormatCall = lua.createCallExpression(lua.createTableIndexExpression(lua.createIdentifier("string"), lua.createStringLiteral("format")), parameters); | ||
case "assert": | ||
if (expression.arguments.length > 1 && isStringFormatTemplate(expression.arguments[1])) { | ||
if (node.arguments.length > 1 && isStringFormatTemplate(node.arguments[1])) { | ||
// assert([condition], string.format([arguments])) | ||
@@ -38,3 +37,3 @@ const stringFormatCall = lua.createCallExpression(lua.createTableIndexExpression(lua.createIdentifier("string"), lua.createStringLiteral("format")), parameters.slice(1)); | ||
case "trace": | ||
if (expression.arguments.length > 0 && isStringFormatTemplate(expression.arguments[0])) { | ||
if (node.arguments.length > 0 && isStringFormatTemplate(node.arguments[0])) { | ||
// print(debug.traceback(string.format([arguments]))) | ||
@@ -49,3 +48,3 @@ const stringFormatCall = lua.createCallExpression(lua.createTableIndexExpression(lua.createIdentifier("string"), lua.createStringLiteral("format")), parameters); | ||
default: | ||
context.diagnostics.push((0, diagnostics_1.unsupportedProperty)(method.name, "console", methodName)); | ||
context.diagnostics.push((0, diagnostics_1.unsupportedProperty)(calledMethod.name, "console", methodName)); | ||
} | ||
@@ -52,0 +51,0 @@ } |
import * as ts from "typescript"; | ||
import * as lua from "../../LuaAST"; | ||
import { TransformationContext } from "../context"; | ||
import { PropertyCallExpression } from "../visitors/call"; | ||
export declare function transformFunctionPrototypeCall(context: TransformationContext, node: PropertyCallExpression): lua.CallExpression | undefined; | ||
export declare function transformFunctionPrototypeCall(context: TransformationContext, node: ts.CallExpression, calledMethod: ts.PropertyAccessExpression): lua.CallExpression | undefined; | ||
export declare function transformFunctionProperty(context: TransformationContext, node: ts.PropertyAccessExpression): lua.Expression | undefined; |
@@ -11,5 +11,4 @@ "use strict"; | ||
const call_1 = require("../visitors/call"); | ||
function transformFunctionPrototypeCall(context, node) { | ||
const expression = node.expression; | ||
const callerType = context.checker.getTypeAtLocation(expression.expression); | ||
function transformFunctionPrototypeCall(context, node, calledMethod) { | ||
const callerType = context.checker.getTypeAtLocation(calledMethod.expression); | ||
if ((0, function_context_1.getFunctionContextType)(context, callerType) === function_context_1.ContextType.Void) { | ||
@@ -19,4 +18,4 @@ context.diagnostics.push((0, diagnostics_1.unsupportedSelfFunctionConversion)(node)); | ||
const signature = context.checker.getResolvedSignature(node); | ||
const [caller, params] = (0, call_1.transformCallAndArguments)(context, expression.expression, node.arguments, signature); | ||
const expressionName = expression.name.text; | ||
const [caller, params] = (0, call_1.transformCallAndArguments)(context, calledMethod.expression, node.arguments, signature); | ||
const expressionName = calledMethod.name.text; | ||
switch (expressionName) { | ||
@@ -31,3 +30,3 @@ case "apply": | ||
case "toString": | ||
context.diagnostics.push((0, diagnostics_1.unsupportedProperty)(expression.name, "function", expressionName)); | ||
context.diagnostics.push((0, diagnostics_1.unsupportedProperty)(calledMethod.name, "function", expressionName)); | ||
} | ||
@@ -34,0 +33,0 @@ } |
@@ -6,3 +6,2 @@ "use strict"; | ||
const lua = require("../../LuaAST"); | ||
const utils_1 = require("../../utils"); | ||
const lua_ast_1 = require("../utils/lua-ast"); | ||
@@ -12,2 +11,3 @@ const lualib_1 = require("../utils/lualib"); | ||
const typescript_1 = require("../utils/typescript"); | ||
const call_1 = require("../visitors/call"); | ||
const array_1 = require("./array"); | ||
@@ -60,10 +60,9 @@ const console_1 = require("./console"); | ||
} | ||
const expression = ts.getOriginalNode(node.expression); | ||
if (!ts.isPropertyAccessExpression(expression)) { | ||
const calledMethod = ts.getOriginalNode((0, call_1.getCalledExpression)(node)); | ||
if (!ts.isPropertyAccessExpression(calledMethod)) { | ||
return; | ||
} | ||
const isOptionalAccess = expression.questionDotToken; | ||
(0, utils_1.assume)(node); | ||
const isOptionalAccess = calledMethod.questionDotToken; | ||
// If the function being called is of type owner.func, get the type of owner | ||
const ownerType = context.checker.getTypeAtLocation(expression.expression); | ||
const ownerType = context.checker.getTypeAtLocation(calledMethod.expression); | ||
if ((0, typescript_1.isStandardLibraryType)(context, ownerType, undefined)) { | ||
@@ -75,62 +74,62 @@ const symbol = ownerType.getSymbol(); | ||
return unsupportedOptionalCall(); | ||
return (0, array_1.transformArrayConstructorCall)(context, node); | ||
return (0, array_1.transformArrayConstructorCall)(context, node, calledMethod); | ||
case "Console": | ||
if (isOptionalCall || isOptionalAccess) | ||
return unsupportedOptionalCall(); | ||
return (0, console_1.transformConsoleCall)(context, node); | ||
return (0, console_1.transformConsoleCall)(context, node, calledMethod); | ||
case "Math": | ||
if (isOptionalCall || isOptionalAccess) | ||
return unsupportedOptionalCall(); | ||
return (0, math_1.transformMathCall)(context, node); | ||
return (0, math_1.transformMathCall)(context, node, calledMethod); | ||
case "StringConstructor": | ||
if (isOptionalCall || isOptionalAccess) | ||
return unsupportedOptionalCall(); | ||
return (0, string_1.transformStringConstructorCall)(context, node); | ||
return (0, string_1.transformStringConstructorCall)(context, node, calledMethod); | ||
case "ObjectConstructor": | ||
if (isOptionalCall || isOptionalAccess) | ||
return unsupportedOptionalCall(); | ||
return (0, object_1.transformObjectConstructorCall)(context, node); | ||
return (0, object_1.transformObjectConstructorCall)(context, node, calledMethod); | ||
case "SymbolConstructor": | ||
if (isOptionalCall || isOptionalAccess) | ||
return unsupportedOptionalCall(); | ||
return (0, symbol_1.transformSymbolConstructorCall)(context, node); | ||
return (0, symbol_1.transformSymbolConstructorCall)(context, node, calledMethod); | ||
case "NumberConstructor": | ||
if (isOptionalCall || isOptionalAccess) | ||
return unsupportedOptionalCall(); | ||
return (0, number_1.transformNumberConstructorCall)(context, node); | ||
return (0, number_1.transformNumberConstructorCall)(context, node, calledMethod); | ||
case "PromiseConstructor": | ||
if (isOptionalCall || isOptionalAccess) | ||
return unsupportedOptionalCall(); | ||
return (0, promise_1.transformPromiseConstructorCall)(context, node); | ||
return (0, promise_1.transformPromiseConstructorCall)(context, node, calledMethod); | ||
} | ||
} | ||
const isStringFunction = (0, typescript_1.isStringType)(context, ownerType) || | ||
(expression.questionDotToken && (0, typescript_1.isNullableType)(context, ownerType, typescript_1.isStringType)); | ||
(calledMethod.questionDotToken && (0, typescript_1.isNullableType)(context, ownerType, typescript_1.isStringType)); | ||
if (isStringFunction && (0, typescript_1.hasStandardLibrarySignature)(context, node)) { | ||
if (isOptionalCall) | ||
return unsupportedOptionalCall(); | ||
return (0, string_1.transformStringPrototypeCall)(context, node); | ||
return (0, string_1.transformStringPrototypeCall)(context, node, calledMethod); | ||
} | ||
const isNumberFunction = (0, typescript_1.isNumberType)(context, ownerType) || | ||
(expression.questionDotToken && (0, typescript_1.isNullableType)(context, ownerType, typescript_1.isNumberType)); | ||
(calledMethod.questionDotToken && (0, typescript_1.isNullableType)(context, ownerType, typescript_1.isNumberType)); | ||
if (isNumberFunction && (0, typescript_1.hasStandardLibrarySignature)(context, node)) { | ||
if (isOptionalCall) | ||
return unsupportedOptionalCall(); | ||
return (0, number_1.transformNumberPrototypeCall)(context, node); | ||
return (0, number_1.transformNumberPrototypeCall)(context, node, calledMethod); | ||
} | ||
const isArrayFunction = (0, typescript_1.isArrayType)(context, ownerType) || | ||
(expression.questionDotToken && (0, typescript_1.isNullableType)(context, ownerType, typescript_1.isArrayType)); | ||
(calledMethod.questionDotToken && (0, typescript_1.isNullableType)(context, ownerType, typescript_1.isArrayType)); | ||
if (isArrayFunction && (0, typescript_1.hasStandardLibrarySignature)(context, node)) { | ||
if (isOptionalCall) | ||
return unsupportedOptionalCall(); | ||
return (0, array_1.transformArrayPrototypeCall)(context, node); | ||
return (0, array_1.transformArrayPrototypeCall)(context, node, calledMethod); | ||
} | ||
const isFunctionFunction = (0, typescript_1.isFunctionType)(ownerType) || | ||
(expression.questionDotToken && (0, typescript_1.isNullableType)(context, ownerType, (_, t) => (0, typescript_1.isFunctionType)(t))); | ||
(calledMethod.questionDotToken && (0, typescript_1.isNullableType)(context, ownerType, (_, t) => (0, typescript_1.isFunctionType)(t))); | ||
if (isFunctionFunction && (0, typescript_1.hasStandardLibrarySignature)(context, node)) { | ||
if (isOptionalCall) | ||
return unsupportedOptionalCall(); | ||
return (0, function_1.transformFunctionPrototypeCall)(context, node); | ||
return (0, function_1.transformFunctionPrototypeCall)(context, node, calledMethod); | ||
} | ||
const objectResult = (0, object_1.transformObjectPrototypeCall)(context, node, expression); | ||
const objectResult = (0, object_1.transformObjectPrototypeCall)(context, node, calledMethod); | ||
if (objectResult) { | ||
@@ -137,0 +136,0 @@ if (isOptionalCall) |
import * as ts from "typescript"; | ||
import * as lua from "../../LuaAST"; | ||
import { TransformationContext } from "../context"; | ||
import { PropertyCallExpression } from "../visitors/call"; | ||
export declare function transformMathProperty(context: TransformationContext, node: ts.PropertyAccessExpression): lua.Expression | undefined; | ||
export declare function transformMathCall(context: TransformationContext, node: PropertyCallExpression): lua.Expression | undefined; | ||
export declare function transformMathCall(context: TransformationContext, node: ts.CallExpression, calledMethod: ts.PropertyAccessExpression): lua.Expression | undefined; |
@@ -29,8 +29,7 @@ "use strict"; | ||
exports.transformMathProperty = transformMathProperty; | ||
function transformMathCall(context, node) { | ||
const expression = node.expression; | ||
function transformMathCall(context, node, calledMethod) { | ||
const signature = context.checker.getResolvedSignature(node); | ||
const params = (0, call_1.transformArguments)(context, node.arguments, signature); | ||
const math = lua.createIdentifier("math"); | ||
const expressionName = expression.name.text; | ||
const expressionName = calledMethod.name.text; | ||
switch (expressionName) { | ||
@@ -94,3 +93,3 @@ // Lua 5.3: math.atan(y, x) | ||
default: | ||
context.diagnostics.push((0, diagnostics_1.unsupportedProperty)(expression.name, "Math", expressionName)); | ||
context.diagnostics.push((0, diagnostics_1.unsupportedProperty)(calledMethod.name, "Math", expressionName)); | ||
} | ||
@@ -97,0 +96,0 @@ } |
@@ -0,5 +1,5 @@ | ||
import ts = require("typescript"); | ||
import * as lua from "../../LuaAST"; | ||
import { TransformationContext } from "../context"; | ||
import { PropertyCallExpression } from "../visitors/call"; | ||
export declare function transformNumberPrototypeCall(context: TransformationContext, node: PropertyCallExpression): lua.Expression | undefined; | ||
export declare function transformNumberConstructorCall(context: TransformationContext, expression: PropertyCallExpression): lua.CallExpression | undefined; | ||
export declare function transformNumberPrototypeCall(context: TransformationContext, node: ts.CallExpression, calledMethod: ts.PropertyAccessExpression): lua.Expression | undefined; | ||
export declare function transformNumberConstructorCall(context: TransformationContext, node: ts.CallExpression, calledMethod: ts.PropertyAccessExpression): lua.CallExpression | undefined; |
@@ -8,8 +8,7 @@ "use strict"; | ||
const call_1 = require("../visitors/call"); | ||
function transformNumberPrototypeCall(context, node) { | ||
const expression = node.expression; | ||
function transformNumberPrototypeCall(context, node, calledMethod) { | ||
const signature = context.checker.getResolvedSignature(node); | ||
const params = (0, call_1.transformArguments)(context, node.arguments, signature); | ||
const caller = context.transformExpression(expression.expression); | ||
const expressionName = expression.name.text; | ||
const caller = context.transformExpression(calledMethod.expression); | ||
const expressionName = calledMethod.name.text; | ||
switch (expressionName) { | ||
@@ -21,17 +20,16 @@ case "toString": | ||
default: | ||
context.diagnostics.push((0, diagnostics_1.unsupportedProperty)(expression.name, "number", expressionName)); | ||
context.diagnostics.push((0, diagnostics_1.unsupportedProperty)(calledMethod.name, "number", expressionName)); | ||
} | ||
} | ||
exports.transformNumberPrototypeCall = transformNumberPrototypeCall; | ||
function transformNumberConstructorCall(context, expression) { | ||
const method = expression.expression; | ||
const parameters = (0, call_1.transformArguments)(context, expression.arguments); | ||
const methodName = method.name.text; | ||
function transformNumberConstructorCall(context, node, calledMethod) { | ||
const parameters = (0, call_1.transformArguments)(context, node.arguments); | ||
const methodName = calledMethod.name.text; | ||
switch (methodName) { | ||
case "isNaN": | ||
return (0, lualib_1.transformLuaLibFunction)(context, lualib_1.LuaLibFeature.NumberIsNaN, expression, ...parameters); | ||
return (0, lualib_1.transformLuaLibFunction)(context, lualib_1.LuaLibFeature.NumberIsNaN, node, ...parameters); | ||
case "isFinite": | ||
return (0, lualib_1.transformLuaLibFunction)(context, lualib_1.LuaLibFeature.NumberIsFinite, expression, ...parameters); | ||
return (0, lualib_1.transformLuaLibFunction)(context, lualib_1.LuaLibFeature.NumberIsFinite, node, ...parameters); | ||
default: | ||
context.diagnostics.push((0, diagnostics_1.unsupportedProperty)(method.name, "Number", methodName)); | ||
context.diagnostics.push((0, diagnostics_1.unsupportedProperty)(calledMethod.name, "Number", methodName)); | ||
} | ||
@@ -38,0 +36,0 @@ } |
import * as lua from "../../LuaAST"; | ||
import * as ts from "typescript"; | ||
import { TransformationContext } from "../context"; | ||
import { PropertyCallExpression } from "../visitors/call"; | ||
export declare function transformObjectConstructorCall(context: TransformationContext, expression: PropertyCallExpression): lua.Expression | undefined; | ||
export declare function transformObjectPrototypeCall(context: TransformationContext, node: ts.CallExpression, expression: ts.PropertyAccessExpression): lua.Expression | undefined; | ||
export declare function transformObjectConstructorCall(context: TransformationContext, node: ts.CallExpression, calledMethod: ts.PropertyAccessExpression): lua.Expression | undefined; | ||
export declare function transformObjectPrototypeCall(context: TransformationContext, node: ts.CallExpression, calledMethod: ts.PropertyAccessExpression): lua.Expression | undefined; |
@@ -8,37 +8,36 @@ "use strict"; | ||
const call_1 = require("../visitors/call"); | ||
function transformObjectConstructorCall(context, expression) { | ||
const method = expression.expression; | ||
const args = (0, call_1.transformArguments)(context, expression.arguments); | ||
const methodName = method.name.text; | ||
function transformObjectConstructorCall(context, node, calledMethod) { | ||
const args = (0, call_1.transformArguments)(context, node.arguments); | ||
const methodName = calledMethod.name.text; | ||
switch (methodName) { | ||
case "assign": | ||
return (0, lualib_1.transformLuaLibFunction)(context, lualib_1.LuaLibFeature.ObjectAssign, expression, ...args); | ||
return (0, lualib_1.transformLuaLibFunction)(context, lualib_1.LuaLibFeature.ObjectAssign, node, ...args); | ||
case "defineProperty": | ||
return (0, lualib_1.transformLuaLibFunction)(context, lualib_1.LuaLibFeature.ObjectDefineProperty, expression, ...args); | ||
return (0, lualib_1.transformLuaLibFunction)(context, lualib_1.LuaLibFeature.ObjectDefineProperty, node, ...args); | ||
case "entries": | ||
return (0, lualib_1.transformLuaLibFunction)(context, lualib_1.LuaLibFeature.ObjectEntries, expression, ...args); | ||
return (0, lualib_1.transformLuaLibFunction)(context, lualib_1.LuaLibFeature.ObjectEntries, node, ...args); | ||
case "fromEntries": | ||
return (0, lualib_1.transformLuaLibFunction)(context, lualib_1.LuaLibFeature.ObjectFromEntries, expression, ...args); | ||
return (0, lualib_1.transformLuaLibFunction)(context, lualib_1.LuaLibFeature.ObjectFromEntries, node, ...args); | ||
case "getOwnPropertyDescriptor": | ||
return (0, lualib_1.transformLuaLibFunction)(context, lualib_1.LuaLibFeature.ObjectGetOwnPropertyDescriptor, expression, ...args); | ||
return (0, lualib_1.transformLuaLibFunction)(context, lualib_1.LuaLibFeature.ObjectGetOwnPropertyDescriptor, node, ...args); | ||
case "getOwnPropertyDescriptors": | ||
return (0, lualib_1.transformLuaLibFunction)(context, lualib_1.LuaLibFeature.ObjectGetOwnPropertyDescriptors, expression, ...args); | ||
return (0, lualib_1.transformLuaLibFunction)(context, lualib_1.LuaLibFeature.ObjectGetOwnPropertyDescriptors, node, ...args); | ||
case "keys": | ||
return (0, lualib_1.transformLuaLibFunction)(context, lualib_1.LuaLibFeature.ObjectKeys, expression, ...args); | ||
return (0, lualib_1.transformLuaLibFunction)(context, lualib_1.LuaLibFeature.ObjectKeys, node, ...args); | ||
case "values": | ||
return (0, lualib_1.transformLuaLibFunction)(context, lualib_1.LuaLibFeature.ObjectValues, expression, ...args); | ||
return (0, lualib_1.transformLuaLibFunction)(context, lualib_1.LuaLibFeature.ObjectValues, node, ...args); | ||
default: | ||
context.diagnostics.push((0, diagnostics_1.unsupportedProperty)(method.name, "Object", methodName)); | ||
context.diagnostics.push((0, diagnostics_1.unsupportedProperty)(calledMethod.name, "Object", methodName)); | ||
} | ||
} | ||
exports.transformObjectConstructorCall = transformObjectConstructorCall; | ||
function transformObjectPrototypeCall(context, node, expression) { | ||
function transformObjectPrototypeCall(context, node, calledMethod) { | ||
const signature = context.checker.getResolvedSignature(node); | ||
const name = expression.name.text; | ||
const name = calledMethod.name.text; | ||
switch (name) { | ||
case "toString": | ||
const toStringIdentifier = lua.createIdentifier("tostring"); | ||
return lua.createCallExpression(toStringIdentifier, [context.transformExpression(expression.expression)], node); | ||
return lua.createCallExpression(toStringIdentifier, [context.transformExpression(calledMethod.expression)], node); | ||
case "hasOwnProperty": | ||
const expr = context.transformExpression(expression.expression); | ||
const expr = context.transformExpression(calledMethod.expression); | ||
const parameters = (0, call_1.transformArguments)(context, node.arguments, signature); | ||
@@ -45,0 +44,0 @@ const rawGetIdentifier = lua.createIdentifier("rawget"); |
import * as ts from "typescript"; | ||
import * as lua from "../../LuaAST"; | ||
import { TransformationContext } from "../context"; | ||
import { PropertyCallExpression } from "../visitors/call"; | ||
export declare function isPromiseClass(context: TransformationContext, node: ts.Identifier): boolean; | ||
export declare function createPromiseIdentifier(original: ts.Node): lua.Identifier; | ||
export declare function transformPromiseConstructorCall(context: TransformationContext, node: PropertyCallExpression): lua.Expression | undefined; | ||
export declare function transformPromiseConstructorCall(context: TransformationContext, node: ts.CallExpression, calledMethod: ts.PropertyAccessExpression): lua.Expression | undefined; | ||
export declare function createStaticPromiseFunctionAccessor(functionName: string, node: ts.Node): lua.TableIndexExpression; |
@@ -18,7 +18,6 @@ "use strict"; | ||
exports.createPromiseIdentifier = createPromiseIdentifier; | ||
function transformPromiseConstructorCall(context, node) { | ||
const expression = node.expression; | ||
function transformPromiseConstructorCall(context, node, calledMethod) { | ||
const signature = context.checker.getResolvedSignature(node); | ||
const params = (0, call_1.transformArguments)(context, node.arguments, signature); | ||
const expressionName = expression.name.text; | ||
const expressionName = calledMethod.name.text; | ||
switch (expressionName) { | ||
@@ -35,8 +34,8 @@ case "all": | ||
(0, lualib_1.importLuaLibFeature)(context, lualib_1.LuaLibFeature.Promise); | ||
return lua.createCallExpression(createStaticPromiseFunctionAccessor("resolve", expression), params, node); | ||
return lua.createCallExpression(createStaticPromiseFunctionAccessor("resolve", calledMethod), params, node); | ||
case "reject": | ||
(0, lualib_1.importLuaLibFeature)(context, lualib_1.LuaLibFeature.Promise); | ||
return lua.createCallExpression(createStaticPromiseFunctionAccessor("reject", expression), params, node); | ||
return lua.createCallExpression(createStaticPromiseFunctionAccessor("reject", calledMethod), params, node); | ||
default: | ||
context.diagnostics.push((0, diagnostics_1.unsupportedProperty)(expression.name, "Promise", expressionName)); | ||
context.diagnostics.push((0, diagnostics_1.unsupportedProperty)(calledMethod.name, "Promise", expressionName)); | ||
} | ||
@@ -43,0 +42,0 @@ } |
import * as ts from "typescript"; | ||
import * as lua from "../../LuaAST"; | ||
import { TransformationContext } from "../context"; | ||
import { PropertyCallExpression } from "../visitors/call"; | ||
export declare function transformStringPrototypeCall(context: TransformationContext, node: PropertyCallExpression): lua.Expression | undefined; | ||
export declare function transformStringConstructorCall(context: TransformationContext, node: PropertyCallExpression): lua.Expression | undefined; | ||
export declare function transformStringPrototypeCall(context: TransformationContext, node: ts.CallExpression, calledMethod: ts.PropertyAccessExpression): lua.Expression | undefined; | ||
export declare function transformStringConstructorCall(context: TransformationContext, node: ts.CallExpression, calledMethod: ts.PropertyAccessExpression): lua.Expression | undefined; | ||
export declare function transformStringProperty(context: TransformationContext, node: ts.PropertyAccessExpression): lua.UnaryExpression | undefined; |
@@ -13,7 +13,6 @@ "use strict"; | ||
} | ||
function transformStringPrototypeCall(context, node) { | ||
const expression = node.expression; | ||
function transformStringPrototypeCall(context, node, calledMethod) { | ||
const signature = context.checker.getResolvedSignature(node); | ||
const [caller, params] = (0, call_1.transformCallAndArguments)(context, expression.expression, node.arguments, signature); | ||
const expressionName = expression.name.text; | ||
const [caller, params] = (0, call_1.transformCallAndArguments)(context, calledMethod.expression, node.arguments, signature); | ||
const expressionName = calledMethod.name.text; | ||
switch (expressionName) { | ||
@@ -106,11 +105,10 @@ case "replace": | ||
default: | ||
context.diagnostics.push((0, diagnostics_1.unsupportedProperty)(expression.name, "string", expressionName)); | ||
context.diagnostics.push((0, diagnostics_1.unsupportedProperty)(calledMethod.name, "string", expressionName)); | ||
} | ||
} | ||
exports.transformStringPrototypeCall = transformStringPrototypeCall; | ||
function transformStringConstructorCall(context, node) { | ||
const expression = node.expression; | ||
function transformStringConstructorCall(context, node, calledMethod) { | ||
const signature = context.checker.getResolvedSignature(node); | ||
const params = (0, call_1.transformArguments)(context, node.arguments, signature); | ||
const expressionName = expression.name.text; | ||
const expressionName = calledMethod.name.text; | ||
switch (expressionName) { | ||
@@ -120,3 +118,3 @@ case "fromCharCode": | ||
default: | ||
context.diagnostics.push((0, diagnostics_1.unsupportedProperty)(expression.name, "String", expressionName)); | ||
context.diagnostics.push((0, diagnostics_1.unsupportedProperty)(calledMethod.name, "String", expressionName)); | ||
} | ||
@@ -123,0 +121,0 @@ } |
@@ -0,4 +1,4 @@ | ||
import ts = require("typescript"); | ||
import * as lua from "../../LuaAST"; | ||
import { TransformationContext } from "../context"; | ||
import { PropertyCallExpression } from "../visitors/call"; | ||
export declare function transformSymbolConstructorCall(context: TransformationContext, expression: PropertyCallExpression): lua.CallExpression | undefined; | ||
export declare function transformSymbolConstructorCall(context: TransformationContext, node: ts.CallExpression, calledMethod: ts.PropertyAccessExpression): lua.CallExpression | undefined; |
@@ -8,7 +8,6 @@ "use strict"; | ||
const call_1 = require("../visitors/call"); | ||
function transformSymbolConstructorCall(context, expression) { | ||
const method = expression.expression; | ||
const signature = context.checker.getResolvedSignature(expression); | ||
const parameters = (0, call_1.transformArguments)(context, expression.arguments, signature); | ||
const methodName = method.name.text; | ||
function transformSymbolConstructorCall(context, node, calledMethod) { | ||
const signature = context.checker.getResolvedSignature(node); | ||
const parameters = (0, call_1.transformArguments)(context, node.arguments, signature); | ||
const methodName = calledMethod.name.text; | ||
switch (methodName) { | ||
@@ -20,5 +19,5 @@ case "for": | ||
const functionIdentifier = lua.createIdentifier(`__TS__SymbolRegistry${upperMethodName}`); | ||
return lua.createCallExpression(functionIdentifier, parameters, expression); | ||
return lua.createCallExpression(functionIdentifier, parameters, node); | ||
default: | ||
context.diagnostics.push((0, diagnostics_1.unsupportedProperty)(method.name, "Symbol", methodName)); | ||
context.diagnostics.push((0, diagnostics_1.unsupportedProperty)(calledMethod.name, "Symbol", methodName)); | ||
} | ||
@@ -25,0 +24,0 @@ } |
@@ -107,2 +107,3 @@ "use strict"; | ||
let assignment; | ||
const noImplicitGlobalVariables = context.options.noImplicitGlobalVariables === true; | ||
const isFunctionDeclaration = tsOriginal !== undefined && ts.isFunctionDeclaration(tsOriginal); | ||
@@ -126,3 +127,3 @@ const identifiers = (0, utils_1.castArray)(lhs); | ||
const isTopLevelVariable = scope.type === scope_1.ScopeType.File; | ||
if (context.isModule || !isTopLevelVariable) { | ||
if (context.isModule || !isTopLevelVariable || noImplicitGlobalVariables) { | ||
const isLuaFunctionExpression = rhs && !Array.isArray(rhs) && lua.isFunctionExpression(rhs); | ||
@@ -168,2 +169,3 @@ const isSafeRecursiveFunctionDeclaration = isFunctionDeclaration && isLuaFunctionExpression; | ||
} | ||
setJSDocComments(context, tsOriginal, declaration, assignment); | ||
if (declaration && assignment) { | ||
@@ -183,4 +185,84 @@ return [declaration, assignment]; | ||
exports.createLocalOrExportedOrGlobalDeclaration = createLocalOrExportedOrGlobalDeclaration; | ||
/** | ||
* Apply JSDoc comments to the newly-created Lua statement, if present. | ||
* https://stackoverflow.com/questions/47429792/is-it-possible-to-get-comments-as-nodes-in-the-ast-using-the-typescript-compiler | ||
*/ | ||
function setJSDocComments(context, tsOriginal, declaration, assignment) { | ||
// Respect the vanilla TypeScript option of "removeComments": | ||
// https://www.typescriptlang.org/tsconfig#removeComments | ||
if (context.options.removeComments) { | ||
return; | ||
} | ||
const docCommentArray = getJSDocCommentFromTSNode(context, tsOriginal); | ||
if (docCommentArray === undefined) { | ||
return; | ||
} | ||
if (declaration && assignment) { | ||
declaration.leadingComments = docCommentArray; | ||
} | ||
else if (declaration) { | ||
declaration.leadingComments = docCommentArray; | ||
} | ||
else if (assignment) { | ||
assignment.leadingComments = docCommentArray; | ||
} | ||
} | ||
function getJSDocCommentFromTSNode(context, tsOriginal) { | ||
if (tsOriginal === undefined) { | ||
return undefined; | ||
} | ||
// The "name" property is only on a subset of node types; we want to be permissive and get the | ||
// comments from as many nodes as possible. | ||
const node = tsOriginal; | ||
if (node.name === undefined) { | ||
return undefined; | ||
} | ||
const symbol = context.checker.getSymbolAtLocation(node.name); | ||
if (symbol === undefined) { | ||
return undefined; | ||
} | ||
// The TypeScript compiler separates JSDoc comments into the "documentation comment" and the | ||
// "tags". The former is conventionally at the top of the comment, and the bottom is | ||
// conventionally at the bottom. We need to get both from the TypeScript API and then combine | ||
// them into one block of text. | ||
const docCommentArray = symbol.getDocumentationComment(context.checker); | ||
const docCommentText = ts.displayPartsToString(docCommentArray).trim(); | ||
const jsDocTagInfoArray = symbol.getJsDocTags(context.checker); | ||
const jsDocTagsTextLines = jsDocTagInfoArray.map(jsDocTagInfo => { | ||
let text = "@" + jsDocTagInfo.name; | ||
if (jsDocTagInfo.text !== undefined) { | ||
const tagDescriptionTextArray = jsDocTagInfo.text | ||
.filter(symbolDisplayPart => symbolDisplayPart.text.trim() !== "") | ||
.map(symbolDisplayPart => symbolDisplayPart.text.trim()); | ||
const tagDescriptionText = tagDescriptionTextArray.join(" "); | ||
text += " " + tagDescriptionText; | ||
} | ||
return text; | ||
}); | ||
const jsDocTagsText = jsDocTagsTextLines.join("\n"); | ||
const combined = (docCommentText + "\n\n" + jsDocTagsText).trim(); | ||
if (combined === "") { | ||
return undefined; | ||
} | ||
// By default, TSTL will display comments immediately next to the "--" characters. We can make | ||
// the comments look better if we separate them by a space (similar to what Prettier does in | ||
// JavaScript/TypeScript). | ||
const linesWithoutSpace = combined.split("\n"); | ||
const lines = linesWithoutSpace.map(line => ` ${line}`); | ||
// We want to JSDoc comments to map on to LDoc comments: | ||
// https://stevedonovan.github.io/ldoc/manual/doc.md.html | ||
// LDoc comments require that the first line starts with three hyphens. | ||
// Thus, need to add a hyphen to the first line. | ||
const firstLine = lines[0]; | ||
if (firstLine.startsWith(" @")) { | ||
lines.unshift("-"); | ||
} | ||
else { | ||
lines.shift(); | ||
lines.unshift("-" + firstLine); | ||
} | ||
return lines; | ||
} | ||
const createNaN = (tsOriginal) => lua.createBinaryExpression(lua.createNumericLiteral(0), lua.createNumericLiteral(0), lua.SyntaxKind.DivisionOperator, tsOriginal); | ||
exports.createNaN = createNaN; | ||
//# sourceMappingURL=lua-ast.js.map |
@@ -6,2 +6,2 @@ import * as ts from "typescript"; | ||
export declare function isAsyncFunction(declaration: ts.FunctionLikeDeclaration): boolean; | ||
export declare function wrapInAsyncAwaiter(context: TransformationContext, statements: lua.Statement[]): lua.Statement[]; | ||
export declare function wrapInAsyncAwaiter(context: TransformationContext, statements: lua.Statement[], includeResolveParameter?: boolean): lua.CallExpression; |
@@ -10,12 +10,8 @@ "use strict"; | ||
const transformAwaitExpression = (node, context) => { | ||
var _a; | ||
// Check if await is inside an async function, it is not allowed at top level or in non-async functions | ||
const containingFunction = (0, typescript_1.findFirstNodeAbove)(node, ts.isFunctionLike); | ||
if (containingFunction === undefined || | ||
!((_a = containingFunction.modifiers) === null || _a === void 0 ? void 0 : _a.some(m => m.kind === ts.SyntaxKind.AsyncKeyword))) { | ||
if (!(0, typescript_1.isInAsyncFunction)(node)) { | ||
context.diagnostics.push((0, diagnostics_1.awaitMustBeInAsyncFunction)(node)); | ||
} | ||
const expression = context.transformExpression(node.expression); | ||
const catchIdentifier = lua.createIdentifier("____catch"); | ||
return (0, lualib_1.transformLuaLibFunction)(context, lualib_1.LuaLibFeature.Await, node, catchIdentifier, expression); | ||
return (0, lualib_1.transformLuaLibFunction)(context, lualib_1.LuaLibFeature.Await, node, expression); | ||
}; | ||
@@ -28,13 +24,10 @@ exports.transformAwaitExpression = transformAwaitExpression; | ||
exports.isAsyncFunction = isAsyncFunction; | ||
function wrapInAsyncAwaiter(context, statements) { | ||
function wrapInAsyncAwaiter(context, statements, includeResolveParameter = true) { | ||
(0, lualib_1.importLuaLibFeature)(context, lualib_1.LuaLibFeature.Await); | ||
return [ | ||
lua.createReturnStatement([ | ||
lua.createCallExpression(lua.createIdentifier("__TS__AsyncAwaiter"), [ | ||
lua.createFunctionExpression(lua.createBlock(statements)), | ||
]), | ||
]), | ||
]; | ||
const parameters = includeResolveParameter ? [lua.createIdentifier("____awaiter_resolve")] : []; | ||
return lua.createCallExpression(lua.createIdentifier("__TS__AsyncAwaiter"), [ | ||
lua.createFunctionExpression(lua.createBlock(statements), parameters), | ||
]); | ||
} | ||
exports.wrapInAsyncAwaiter = wrapInAsyncAwaiter; | ||
//# sourceMappingURL=async-await.js.map |
import * as ts from "typescript"; | ||
import * as lua from "../../LuaAST"; | ||
import { FunctionVisitor, TransformationContext } from "../context"; | ||
export declare type PropertyCallExpression = ts.CallExpression & { | ||
expression: ts.PropertyAccessExpression; | ||
}; | ||
export declare function validateArguments(context: TransformationContext, params: readonly ts.Expression[], signature?: ts.Signature): void; | ||
@@ -12,1 +9,2 @@ export declare function transformArguments(context: TransformationContext, params: readonly ts.Expression[], signature?: ts.Signature, callContext?: ts.Expression): lua.Expression[]; | ||
export declare const transformCallExpression: FunctionVisitor<ts.CallExpression>; | ||
export declare function getCalledExpression(node: ts.CallExpression): ts.Expression; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.transformCallExpression = exports.transformContextualCallExpression = exports.transformCallAndArguments = exports.transformArguments = exports.validateArguments = void 0; | ||
exports.getCalledExpression = exports.transformCallExpression = exports.transformContextualCallExpression = exports.transformCallAndArguments = exports.transformArguments = exports.validateArguments = void 0; | ||
const ts = require("typescript"); | ||
@@ -85,3 +85,3 @@ const lua = require("../../LuaAST"); | ||
} | ||
const left = ts.isCallExpression(node) ? node.expression : node.tag; | ||
const left = ts.isCallExpression(node) ? getCalledExpression(node) : node.tag; | ||
let [argPrecedingStatements, transformedArguments] = (0, preceding_statements_1.transformInPrecedingStatementScope)(context, () => transformArguments(context, args, signature)); | ||
@@ -117,5 +117,5 @@ if (ts.isPropertyAccessExpression(left) && | ||
exports.transformContextualCallExpression = transformContextualCallExpression; | ||
function transformPropertyCall(context, node) { | ||
function transformPropertyCall(context, node, calledMethod) { | ||
const signature = context.checker.getResolvedSignature(node); | ||
if (node.expression.expression.kind === ts.SyntaxKind.SuperKeyword) { | ||
if (calledMethod.expression.kind === ts.SyntaxKind.SuperKeyword) { | ||
// Super calls take the format of super.call(self,...) | ||
@@ -151,3 +151,4 @@ const parameters = transformArguments(context, node.arguments, signature, ts.factory.createThis()); | ||
var _a; | ||
if (node.expression.kind === ts.SyntaxKind.ImportKeyword) { | ||
const calledExpression = getCalledExpression(node); | ||
if (calledExpression.kind === ts.SyntaxKind.ImportKeyword) { | ||
return (0, import_1.transformImportExpression)(node, context); | ||
@@ -158,4 +159,4 @@ } | ||
} | ||
const optionalContinuation = ts.isIdentifier(node.expression) | ||
? (0, optional_chaining_1.getOptionalContinuationData)(node.expression) | ||
const optionalContinuation = ts.isIdentifier(calledExpression) | ||
? (0, optional_chaining_1.getOptionalContinuationData)(calledExpression) | ||
: undefined; | ||
@@ -171,4 +172,4 @@ const wrapResultInTable = (0, multi_1.isMultiReturnCall)(context, node) && (0, multi_1.shouldMultiReturnCallBeWrapped)(context, node); | ||
} | ||
if (ts.isPropertyAccessExpression(node.expression)) { | ||
const ownerType = context.checker.getTypeAtLocation(node.expression.expression); | ||
if (ts.isPropertyAccessExpression(calledExpression)) { | ||
const ownerType = context.checker.getTypeAtLocation(calledExpression.expression); | ||
const annotations = (0, annotations_1.getTypeAnnotations)(ownerType); | ||
@@ -178,6 +179,6 @@ if (annotations.has(annotations_1.AnnotationKind.LuaTable)) { | ||
} | ||
const result = transformPropertyCall(context, node); | ||
const result = transformPropertyCall(context, node, calledExpression); | ||
return wrapResultInTable ? (0, lua_ast_1.wrapInTable)(result) : result; | ||
} | ||
if (ts.isElementAccessExpression(node.expression)) { | ||
if (ts.isElementAccessExpression(calledExpression)) { | ||
const result = transformElementCall(context, node); | ||
@@ -188,3 +189,3 @@ return wrapResultInTable ? (0, lua_ast_1.wrapInTable)(result) : result; | ||
// Handle super calls properly | ||
if (node.expression.kind === ts.SyntaxKind.SuperKeyword) { | ||
if (calledExpression.kind === ts.SyntaxKind.SuperKeyword) { | ||
const parameters = transformArguments(context, node.arguments, signature, ts.factory.createThis()); | ||
@@ -198,3 +199,3 @@ return lua.createCallExpression(lua.createTableIndexExpression(context.transformExpression(ts.factory.createSuper()), lua.createStringLiteral("____constructor")), parameters); | ||
if (!isContextualCall) { | ||
[callPath, parameters] = transformCallAndArguments(context, node.expression, node.arguments, signature); | ||
[callPath, parameters] = transformCallAndArguments(context, calledExpression, node.arguments, signature); | ||
} | ||
@@ -205,3 +206,3 @@ else { | ||
const callContext = useGlobalContext ? ts.factory.createIdentifier("_G") : ts.factory.createNull(); | ||
[callPath, parameters] = transformCallAndArguments(context, node.expression, node.arguments, signature, callContext); | ||
[callPath, parameters] = transformCallAndArguments(context, calledExpression, node.arguments, signature, callContext); | ||
} | ||
@@ -215,2 +216,10 @@ const callExpression = lua.createCallExpression(callPath, parameters, node); | ||
exports.transformCallExpression = transformCallExpression; | ||
function getCalledExpression(node) { | ||
function unwrapExpression(expression) { | ||
expression = ts.skipOuterExpressions(expression); | ||
return ts.isNonNullExpression(expression) ? unwrapExpression(expression.expression) : expression; | ||
} | ||
return unwrapExpression(node.expression); | ||
} | ||
exports.getCalledExpression = getCalledExpression; | ||
//# sourceMappingURL=call.js.map |
@@ -8,4 +8,6 @@ "use strict"; | ||
const lua_ast_1 = require("../utils/lua-ast"); | ||
const lualib_1 = require("../utils/lualib"); | ||
const scope_1 = require("../utils/scope"); | ||
const typescript_1 = require("../utils/typescript"); | ||
const async_await_1 = require("./async-await"); | ||
const block_1 = require("./block"); | ||
@@ -15,11 +17,46 @@ const identifier_1 = require("./identifier"); | ||
const return_1 = require("./return"); | ||
const transformAsyncTry = (statement, context) => { | ||
const [tryBlock] = (0, block_1.transformScopeBlock)(context, statement.tryBlock, scope_1.ScopeType.Try); | ||
if (context.options.luaTarget === __1.LuaTarget.Lua51 && !context.options.lua51AllowTryCatchInAsyncAwait) { | ||
context.diagnostics.push((0, diagnostics_1.unsupportedForTargetButOverrideAvailable)(statement, "try/catch inside async functions", __1.LuaTarget.Lua51, "lua51AllowTryCatchInAsyncAwait")); | ||
return tryBlock.statements; | ||
} | ||
// __TS__AsyncAwaiter(<catch block>) | ||
const awaiter = (0, async_await_1.wrapInAsyncAwaiter)(context, tryBlock.statements, false); | ||
const awaiterIdentifier = lua.createIdentifier("____try"); | ||
const awaiterDefinition = lua.createVariableDeclarationStatement(awaiterIdentifier, awaiter); | ||
// local ____try = __TS__AsyncAwaiter(<catch block>) | ||
const result = [awaiterDefinition]; | ||
if (statement.finallyBlock) { | ||
const awaiterFinally = lua.createTableIndexExpression(awaiterIdentifier, lua.createStringLiteral("finally")); | ||
const finallyFunction = lua.createFunctionExpression(lua.createBlock(context.transformStatements(statement.finallyBlock.statements))); | ||
const finallyCall = lua.createCallExpression(awaiterFinally, [awaiterIdentifier, finallyFunction], statement.finallyBlock); | ||
// ____try.finally(<finally function>) | ||
result.push(lua.createExpressionStatement(finallyCall)); | ||
} | ||
if (statement.catchClause) { | ||
// ____try.catch(<catch function>) | ||
const [catchFunction] = transformCatchClause(context, statement.catchClause); | ||
if (catchFunction.params) { | ||
catchFunction.params.unshift(lua.createAnonymousIdentifier()); | ||
} | ||
const awaiterCatch = lua.createTableIndexExpression(awaiterIdentifier, lua.createStringLiteral("catch")); | ||
const catchCall = lua.createCallExpression(awaiterCatch, [awaiterIdentifier, catchFunction]); | ||
// await ____try.catch(<catch function>) | ||
const promiseAwait = (0, lualib_1.transformLuaLibFunction)(context, __1.LuaLibFeature.Await, statement, catchCall); | ||
result.push(lua.createExpressionStatement(promiseAwait, statement)); | ||
} | ||
else { | ||
// await ____try | ||
const promiseAwait = (0, lualib_1.transformLuaLibFunction)(context, __1.LuaLibFeature.Await, statement, awaiterIdentifier); | ||
result.push(lua.createExpressionStatement(promiseAwait, statement)); | ||
} | ||
return result; | ||
}; | ||
const transformTryStatement = (statement, context) => { | ||
var _a; | ||
if ((0, typescript_1.isInAsyncFunction)(statement)) { | ||
return transformAsyncTry(statement, context); | ||
} | ||
const [tryBlock, tryScope] = (0, block_1.transformScopeBlock)(context, statement.tryBlock, scope_1.ScopeType.Try); | ||
if (context.options.luaTarget === __1.LuaTarget.Lua51 && | ||
(0, typescript_1.isInAsyncFunction)(statement) && | ||
!context.options.lua51AllowTryCatchInAsyncAwait) { | ||
context.diagnostics.push((0, diagnostics_1.unsupportedForTargetButOverrideAvailable)(statement, "try/catch inside async functions", __1.LuaTarget.Lua51, "lua51AllowTryCatchInAsyncAwait")); | ||
return tryBlock.statements; | ||
} | ||
if (context.options.luaTarget === __1.LuaTarget.Lua51 && (0, typescript_1.isInGeneratorFunction)(statement)) { | ||
@@ -38,7 +75,3 @@ context.diagnostics.push((0, diagnostics_1.unsupportedForTarget)(statement, "try/catch inside generator functions", __1.LuaTarget.Lua51)); | ||
// try with catch | ||
const [catchBlock, catchScope] = (0, block_1.transformScopeBlock)(context, statement.catchClause.block, scope_1.ScopeType.Catch); | ||
const catchParameter = statement.catchClause.variableDeclaration | ||
? (0, identifier_1.transformIdentifier)(context, statement.catchClause.variableDeclaration.name) | ||
: undefined; | ||
const catchFunction = lua.createFunctionExpression(catchBlock, catchParameter ? [lua.cloneIdentifier(catchParameter)] : []); | ||
const [catchFunction, catchScope] = transformCatchClause(context, statement.catchClause); | ||
const catchIdentifier = lua.createIdentifier("____catch"); | ||
@@ -102,2 +135,10 @@ result.push(lua.createVariableDeclarationStatement(catchIdentifier, catchFunction)); | ||
exports.transformThrowStatement = transformThrowStatement; | ||
function transformCatchClause(context, catchClause) { | ||
const [catchBlock, catchScope] = (0, block_1.transformScopeBlock)(context, catchClause.block, scope_1.ScopeType.Catch); | ||
const catchParameter = catchClause.variableDeclaration | ||
? (0, identifier_1.transformIdentifier)(context, catchClause.variableDeclaration.name) | ||
: undefined; | ||
const catchFunction = lua.createFunctionExpression(catchBlock, catchParameter ? [lua.cloneIdentifier(catchParameter)] : []); | ||
return [catchFunction, catchScope]; | ||
} | ||
//# sourceMappingURL=errors.js.map |
@@ -117,3 +117,3 @@ "use strict"; | ||
if (node && (0, async_await_1.isAsyncFunction)(node)) { | ||
bodyStatements = (0, async_await_1.wrapInAsyncAwaiter)(context, bodyStatements); | ||
bodyStatements = [lua.createReturnStatement([(0, async_await_1.wrapInAsyncAwaiter)(context, bodyStatements)])]; | ||
} | ||
@@ -120,0 +120,0 @@ const headerStatements = transformFunctionBodyHeader(context, scope, parameters, spreadIdentifier); |
@@ -64,2 +64,7 @@ "use strict"; | ||
const results = [...values]; | ||
if ((0, typescript_1.isInAsyncFunction)(node)) { | ||
return lua.createReturnStatement([ | ||
lua.createCallExpression(lua.createIdentifier("____awaiter_resolve"), [lua.createNilLiteral(), ...values]), | ||
]); | ||
} | ||
if (isInTryCatch(context)) { | ||
@@ -69,6 +74,2 @@ // Bubble up explicit return flag and check if we're inside a try/catch block | ||
} | ||
else if ((0, typescript_1.isInAsyncFunction)(node)) { | ||
// Add nil error handler in async function and not in try | ||
results.unshift(lua.createNilLiteral()); | ||
} | ||
return lua.createReturnStatement(results, node); | ||
@@ -75,0 +76,0 @@ } |
{ | ||
"name": "typescript-to-lua", | ||
"version": "1.4.4", | ||
"version": "1.5.0", | ||
"description": "A generic TypeScript to Lua transpiler. Write your code in TypeScript and publish Lua!", | ||
@@ -5,0 +5,0 @@ "repository": "https://github.com/TypeScriptToLua/TypeScriptToLua", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
878399
14023