Skip to content

Fix loop variables incorrectly being made global #1641

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 8, 2025
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
5 changes: 5 additions & 0 deletions src/transformation/visitors/loops/for.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@ import { FunctionVisitor } from "../../context";
import { transformInPrecedingStatementScope } from "../../utils/preceding-statements";
import { checkVariableDeclarationList, transformVariableDeclaration } from "../variable-declaration";
import { invertCondition, transformLoopBody } from "./utils";
import { ScopeType } from "../../utils/scope";

export const transformForStatement: FunctionVisitor<ts.ForStatement> = (statement, context) => {
const result: lua.Statement[] = [];

context.pushScope(ScopeType.Loop);

if (statement.initializer) {
if (ts.isVariableDeclarationList(statement.initializer)) {
checkVariableDeclarationList(context, statement.initializer);
Expand Down Expand Up @@ -61,5 +64,7 @@ export const transformForStatement: FunctionVisitor<ts.ForStatement> = (statemen
// while (condition) do ... end
result.push(lua.createWhileStatement(lua.createBlock(body), condition, statement));

context.popScope();

return lua.createDoStatement(result, statement);
};
13 changes: 11 additions & 2 deletions test/unit/loops.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -540,10 +540,10 @@ for (const testCase of [
"for (const a of []) { continue; }",
]) {
const expectContinueVariable: util.TapCallback = builder =>
expect(builder.getMainLuaCodeChunk()).toMatch("local __continue2");
expect(builder.getMainLuaCodeChunk()).toMatch(/local __continue\d+/);

const expectContinueGotoLabel: util.TapCallback = builder =>
expect(builder.getMainLuaCodeChunk()).toMatch("::__continue2::");
expect(builder.getMainLuaCodeChunk()).toMatch(/::__continue\d+::/);

const expectContinueStatement: util.TapCallback = builder =>
expect(builder.getMainLuaCodeChunk()).toMatch("continue;");
Expand Down Expand Up @@ -624,3 +624,12 @@ test("for...in with pre-defined variable keeps last value", () => {
// Need custom matcher because order is not guaranteed in neither JS nor Lua
expect([keyX, keyFoo]).toContain(result);
});

// https://github.com/TypeScriptToLua/TypeScriptToLua/issues/1631
test("loop variables should not be global (#1631)", () => {
const code = util.testModule`
for (let val = 0; val < 2; ++val) {}
`.getMainLuaCodeChunk();

expect(code).toContain("local val");
});
Loading