X Tutup
Skip to content

Commit c962837

Browse files
authored
Fixes for Optimize PR (#1312)
* Fix function prototype calls for explicitly given function types * Fix getPropertyValue type flags optimization. * Fix language extension calls in optional chaining
1 parent b0deccc commit c962837

File tree

5 files changed

+44
-12
lines changed

5 files changed

+44
-12
lines changed

src/transformation/builtins/function.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ import * as lua from "../../LuaAST";
44
import { TransformationContext } from "../context";
55
import { unsupportedForTarget, unsupportedProperty, unsupportedSelfFunctionConversion } from "../utils/diagnostics";
66
import { ContextType, getFunctionContextType } from "../utils/function-context";
7-
import { createUnpackCall } from "../utils/lua-ast";
87
import { LuaLibFeature, transformLuaLibFunction } from "../utils/lualib";
98
import { transformCallAndArguments } from "../visitors/call";
9+
import { createUnpackCall } from "../utils/lua-ast";
1010

1111
export function transformFunctionPrototypeCall(
1212
context: TransformationContext,

src/transformation/builtins/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,8 @@ function tryTransformBuiltinPropertyCall(
145145
case "ReadonlyArray":
146146
return transformArrayPrototypeCall(context, node, calledMethod);
147147
case "Function":
148+
case "CallableFunction":
149+
case "NewableFunction":
148150
return transformFunctionPrototypeCall(context, node, calledMethod);
149151
}
150152
}

src/transformation/utils/language-extensions.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,8 @@ export function getExtensionKindForType(context: TransformationContext, type: ts
6363
}
6464

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

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

7878
export function getExtensionKindForNode(context: TransformationContext, node: ts.Node): ExtensionKind | undefined {
79-
const type = context.checker.getTypeAtLocation(node);
79+
const originalNode = ts.getOriginalNode(node);
80+
let type = context.checker.getTypeAtLocation(originalNode);
81+
if (ts.isOptionalChain(originalNode)) {
82+
type = context.checker.getNonNullableType(type);
83+
}
8084
return getExtensionKindForType(context, type);
8185
}
8286

test/unit/functions/functions.spec.ts

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -159,24 +159,31 @@ test("Class static dot method with parameter", () => {
159159
`.expectToMatchJsResult();
160160
});
161161

162-
test("Function bind", () => {
162+
const functionTypeDeclarations = [
163+
["arrow", ": (...args: any) => any"],
164+
["call signature", ": { (...args: any): any; }"],
165+
["generic", ": Function"],
166+
["inferred", ""],
167+
];
168+
169+
test.each(functionTypeDeclarations)("Function bind (%s)", (_, type) => {
163170
util.testFunction`
164-
const abc = function (this: { a: number }, a: string, b: string) { return this.a + a + b; }
171+
const abc${type} = function (this: { a: number }, a: string, b: string) { return this.a + a + b; }
165172
return abc.bind({ a: 4 }, "b")("c");
166173
`.expectToMatchJsResult();
167174
});
168175

169-
test("Function apply", () => {
176+
test.each(functionTypeDeclarations)("Function apply (%s)", (_, type) => {
170177
util.testFunction`
171-
const abc = function (this: { a: number }, a: string) { return this.a + a; }
178+
const abc${type} = function (this: { a: number }, a: string) { return this.a + a; }
172179
return abc.apply({ a: 4 }, ["b"]);
173180
`.expectToMatchJsResult();
174181
});
175182

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

196-
test("Function call", () => {
203+
test.each(functionTypeDeclarations)("Function call (%s)", (_, type) => {
197204
util.testFunction`
198-
const abc = function (this: { a: number }, a: string) { return this.a + a; }
205+
const abc${type} = function (this: { a: number }, a: string) { return this.a + a; }
199206
return abc.call({ a: 4 }, "b");
200207
`.expectToMatchJsResult();
201208
});

test/unit/language-extensions/table.spec.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -424,3 +424,22 @@ describe("LuaTable extension interface", () => {
424424
.expectToEqual({ foo: 1, bar: 3, baz: 5 });
425425
});
426426
});
427+
428+
test.each([
429+
[undefined, undefined],
430+
["new LuaSet()", true],
431+
])("call on optional table with strictNullChecks (%s)", (value, expected) => {
432+
util.testFunction`
433+
function getFoo(): LuaSet<string> | undefined {
434+
return ${value}
435+
}
436+
const foo = getFoo()
437+
foo?.add("foo")
438+
return foo?.has("foo")
439+
`
440+
.setOptions({
441+
strictNullChecks: true,
442+
})
443+
.withLanguageExtensions()
444+
.expectToEqual(expected);
445+
});

0 commit comments

Comments
 (0)
X Tutup