Skip to content

Root level for-loop's variables are transpiled to global variables #1631

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

Closed
inklesspen1rus opened this issue May 9, 2025 · 2 comments · Fixed by #1641
Closed

Root level for-loop's variables are transpiled to global variables #1631

inklesspen1rus opened this issue May 9, 2025 · 2 comments · Fixed by #1641
Labels
bug scope: transformation Transformation of TS to Lua

Comments

@inklesspen1rus
Copy link

When for-loop is located at root level of the file, it's variables from initializer are global
But when I add any level (empty brackets or a function) they're being local

Example:

// Uncomment brackets to make `val` local
// {
    for (let val = 0; val < 2; ++val) print(val);
    print('For loop end')

    // Should be nil, but indeed it prints "2"
    print(val)
// }
--[[ Generated with https://github.com/TypeScriptToLua/TypeScriptToLua ]]
do
    val = 0 -- here "local" keyword missing is
    while val < 2 do
        print(val)
        val = val + 1
    end
end
print("For loop end")
print(val)
@inklesspen1rus
Copy link
Author

Oh, that's controller by noImplicitGlobalVariables parameter, sorry

@Perryvw
Copy link
Member

Perryvw commented May 9, 2025

You can avoid this with noImplicitGlobalVariables, but that should only turn globals into locals, I think your original bug report is valid, this is not the behaviour I expect.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug scope: transformation Transformation of TS to Lua
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants