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
7 changes: 6 additions & 1 deletion src/Transpiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1462,7 +1462,12 @@ export abstract class LuaTranspiler {
const identifierName = this.transpileIdentifier(node.name);
if (node.initializer) {
const value = this.transpileExpression(node.initializer);
return `local ${identifierName} = ${value}`;
if (ts.isFunctionExpression(node.initializer) || ts.isArrowFunction(node.initializer)) {
// Separate declaration and assignment for functions to allow recursion
return `local ${identifierName}; ${identifierName} = ${value}`;
} else {
return `local ${identifierName} = ${value}`;
}
} else {
return `local ${identifierName} = nil`;
}
Expand Down
2 changes: 1 addition & 1 deletion test/translation/lua/shorthandPropertyAssignment.lua
Original file line number Diff line number Diff line change
@@ -1 +1 @@
local f = function(x) return ({x = x}) end;
local f; f = function(x) return ({x = x}) end;
24 changes: 24 additions & 0 deletions test/unit/functions.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -245,4 +245,28 @@ export class FunctionTests {

Expect(result).toBe(3);
}

@Test("Recursive function definition")
public recursiveFunctionDefinition(): void {
const result = util.transpileAndExecute(
`function f() { return typeof f; } return f();`);

Expect(result).toBe("function");
}

@Test("Recursive function expression")
public recursiveFunctionExpression(): void {
const result = util.transpileAndExecute(
`let f = function() { return typeof f; } return f();`);

Expect(result).toBe("function");
}

@Test("Recursive arrow function")
public recursiveArrowFunction(): void {
const result = util.transpileAndExecute(
`let f = () => typeof f; return f();`);

Expect(result).toBe("function");
}
}