Skip to content

Commit 92de529

Browse files
authored
fixed compiler crash when unable to determine signature of function… (#321)
* fixed compiler crash when unable to determine signature of function argument when passing a function expression * added test
1 parent c880ef6 commit 92de529

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

src/TSHelper.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -370,7 +370,9 @@ export class TSHelper {
370370
if (i >= 0) {
371371
const parentSignature = checker.getResolvedSignature(signatureDeclaration.parent);
372372
const parentSignatureDeclaration = parentSignature.getDeclaration();
373-
declType = checker.getTypeAtLocation(parentSignatureDeclaration.parameters[i]);
373+
if (parentSignatureDeclaration) {
374+
declType = checker.getTypeAtLocation(parentSignatureDeclaration.parameters[i]);
375+
}
374376
}
375377
} else if (ts.isReturnStatement(signatureDeclaration.parent)) {
376378
declType = this.getContainingFunctionReturnType(signatureDeclaration.parent, checker);

test/unit/assignments.spec.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,20 @@ export class AssignmentTests {
300300
Expect(result).toBe(expectResult);
301301
}
302302

303+
@TestCase("s => s", "foo")
304+
@TestCase("function(s) { return s; }", "foo")
305+
@TestCase("function(this: void, s: string) { return s; }", "foo")
306+
@Test("Valid function expression argument with no signature")
307+
public validFunctionExpressionArgumentNoSignature(func: string, expectResult: string): void {
308+
const code = `${AssignmentTests.funcAssignTestCode}
309+
const takesFunc: any = (fn: (s: string) => string) => {
310+
return (fn as any)("foo");
311+
}
312+
return takesFunc(${func});`;
313+
const result = util.transpileAndExecute(code);
314+
Expect(result).toBe(expectResult);
315+
}
316+
303317
@TestCase("func", "foo+func")
304318
@TestCase("lambda", "foo+lambda")
305319
@TestCase("Foo.staticMethod", "foo+staticMethod")

0 commit comments

Comments
 (0)