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
4 changes: 3 additions & 1 deletion src/transformation/utils/lua-ast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,9 @@ export function createLocalOrExportedOrGlobalDeclaration(
if (scope.type === ScopeType.Switch || (!isFunctionDeclaration && hasMultipleReferences(scope, lhs))) {
// Split declaration and assignment of identifiers that reference themselves in their declaration
declaration = lua.createVariableDeclarationStatement(lhs, undefined, tsOriginal);
assignment = lua.createAssignmentStatement(lhs, rhs, tsOriginal);
if (rhs) {
assignment = lua.createAssignmentStatement(lhs, rhs, tsOriginal);
}
} else {
declaration = lua.createVariableDeclarationStatement(lhs, rhs, tsOriginal);
}
Expand Down
11 changes: 11 additions & 0 deletions test/unit/hoisting.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -254,3 +254,14 @@ test("Hoisting Shorthand Property", () => {
return foo();`;
expect(util.transpileAndExecute(code)).toBe("foobar");
});

// https://github.com/TypeScriptToLua/TypeScriptToLua/issues/944
test("Hoisting variable without initializer", () => {
util.testFunction`
function foo() {
return x;
}
let x: number;
return foo();
`.expectToMatchJsResult();
});