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
2 changes: 1 addition & 1 deletion src/transformation/builtins/function.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import * as lua from "../../LuaAST";
import { TransformationContext } from "../context";
import { unsupportedForTarget, unsupportedProperty, unsupportedSelfFunctionConversion } from "../utils/diagnostics";
import { ContextType, getFunctionContextType } from "../utils/function-context";
import { createUnpackCall } from "../utils/lua-ast";
import { LuaLibFeature, transformLuaLibFunction } from "../utils/lualib";
import { transformCallAndArguments } from "../visitors/call";
import { createUnpackCall } from "../utils/lua-ast";

export function transformFunctionPrototypeCall(
context: TransformationContext,
Expand Down
2 changes: 2 additions & 0 deletions src/transformation/builtins/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,8 @@ function tryTransformBuiltinPropertyCall(
case "ReadonlyArray":
return transformArrayPrototypeCall(context, node, calledMethod);
case "Function":
case "CallableFunction":
case "NewableFunction":
return transformFunctionPrototypeCall(context, node, calledMethod);
}
}
Expand Down
10 changes: 7 additions & 3 deletions src/transformation/utils/language-extensions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ export function getExtensionKindForType(context: TransformationContext, type: ts
}

const excludedTypeFlags: ts.TypeFlags =
((1 << 18) - 1) & // All flags from Any...Never
ts.TypeFlags.Index &
((1 << 18) - 1) | // All flags from Any...Never
ts.TypeFlags.Index |
ts.TypeFlags.NonPrimitive;

function getPropertyValue(context: TransformationContext, type: ts.Type, propertyName: string): string | undefined {
Expand All @@ -76,7 +76,11 @@ function getPropertyValue(context: TransformationContext, type: ts.Type, propert
}

export function getExtensionKindForNode(context: TransformationContext, node: ts.Node): ExtensionKind | undefined {
const type = context.checker.getTypeAtLocation(node);
const originalNode = ts.getOriginalNode(node);
let type = context.checker.getTypeAtLocation(originalNode);
if (ts.isOptionalChain(originalNode)) {
type = context.checker.getNonNullableType(type);
}
return getExtensionKindForType(context, type);
}

Expand Down
23 changes: 15 additions & 8 deletions test/unit/functions/functions.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,24 +159,31 @@ test("Class static dot method with parameter", () => {
`.expectToMatchJsResult();
});

test("Function bind", () => {
const functionTypeDeclarations = [
["arrow", ": (...args: any) => any"],
["call signature", ": { (...args: any): any; }"],
["generic", ": Function"],
["inferred", ""],
];

test.each(functionTypeDeclarations)("Function bind (%s)", (_, type) => {
util.testFunction`
const abc = function (this: { a: number }, a: string, b: string) { return this.a + a + b; }
const abc${type} = function (this: { a: number }, a: string, b: string) { return this.a + a + b; }
return abc.bind({ a: 4 }, "b")("c");
`.expectToMatchJsResult();
});

test("Function apply", () => {
test.each(functionTypeDeclarations)("Function apply (%s)", (_, type) => {
util.testFunction`
const abc = function (this: { a: number }, a: string) { return this.a + a; }
const abc${type} = function (this: { a: number }, a: string) { return this.a + a; }
return abc.apply({ a: 4 }, ["b"]);
`.expectToMatchJsResult();
});

// Fix #1226: https://github.com/TypeScriptToLua/TypeScriptToLua/issues/1226
test("function apply without arguments should not lead to exception", () => {
test.each(functionTypeDeclarations)("function apply without arguments should not lead to exception (%s)", (_, type) => {
util.testFunction`
const f = function (this: number) { return this + 3; }
const f${type} = function (this: number) { return this + 3; }
return f.apply(4);
`.expectToMatchJsResult();
});
Expand All @@ -193,9 +200,9 @@ test.each(["() => 4", "undefined"])("prototype call on nullable function (%p)",
.expectToMatchJsResult();
});

test("Function call", () => {
test.each(functionTypeDeclarations)("Function call (%s)", (_, type) => {
util.testFunction`
const abc = function (this: { a: number }, a: string) { return this.a + a; }
const abc${type} = function (this: { a: number }, a: string) { return this.a + a; }
return abc.call({ a: 4 }, "b");
`.expectToMatchJsResult();
});
Expand Down
19 changes: 19 additions & 0 deletions test/unit/language-extensions/table.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -424,3 +424,22 @@ describe("LuaTable extension interface", () => {
.expectToEqual({ foo: 1, bar: 3, baz: 5 });
});
});

test.each([
[undefined, undefined],
["new LuaSet()", true],
])("call on optional table with strictNullChecks (%s)", (value, expected) => {
util.testFunction`
function getFoo(): LuaSet<string> | undefined {
return ${value}
}
const foo = getFoo()
foo?.add("foo")
return foo?.has("foo")
`
.setOptions({
strictNullChecks: true,
})
.withLanguageExtensions()
.expectToEqual(expected);
});