Skip to content

Commit 237a788

Browse files
authored
Fixed incorrect variable declarations in some cases when there is no initializer (#952)
1 parent 5ced4c7 commit 237a788

2 files changed

Lines changed: 14 additions & 1 deletion

File tree

src/transformation/utils/lua-ast.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,9 @@ export function createLocalOrExportedOrGlobalDeclaration(
176176
if (scope.type === ScopeType.Switch || (!isFunctionDeclaration && hasMultipleReferences(scope, lhs))) {
177177
// Split declaration and assignment of identifiers that reference themselves in their declaration
178178
declaration = lua.createVariableDeclarationStatement(lhs, undefined, tsOriginal);
179-
assignment = lua.createAssignmentStatement(lhs, rhs, tsOriginal);
179+
if (rhs) {
180+
assignment = lua.createAssignmentStatement(lhs, rhs, tsOriginal);
181+
}
180182
} else {
181183
declaration = lua.createVariableDeclarationStatement(lhs, rhs, tsOriginal);
182184
}

test/unit/hoisting.spec.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,3 +254,14 @@ test("Hoisting Shorthand Property", () => {
254254
return foo();`;
255255
expect(util.transpileAndExecute(code)).toBe("foobar");
256256
});
257+
258+
// https://github.com/TypeScriptToLua/TypeScriptToLua/issues/944
259+
test("Hoisting variable without initializer", () => {
260+
util.testFunction`
261+
function foo() {
262+
return x;
263+
}
264+
let x: number;
265+
return foo();
266+
`.expectToMatchJsResult();
267+
});

0 commit comments

Comments
 (0)