Issue a warning for top-level local variables used before declaration
Opened this issue · 4 comments
Love the extension!
Here's an issue I noticed with top-level local variables across tabs:
__lua__
a = 1
local b = 1
-->8
-- another tab
print(a) -- no issues as expected
print(b) -- should report issue
Let me know if I'm missing something, cheers!
Why is there an end
on the 4th line of your snippet? Isn't that an error? Also, local
variables at the top-level are just global variables. I tried your code snippet in PICO-8, and after removing the end
to get rid of the error, it runs fine, i.e. print(b)
works.
Why is there an end on the 4th line of your snippet?
Sorry for the confusion @japhib — looks like I left it there by mistake as I was cleaning up the snippet.
Looks like I oversimplified the example as well, here's a better snippet that demonstrates the issue:
__lua__
function _draw()
cls()
print(a) -- no issues as expected
print(b, 0, 8) -- should report issue
end
-->8
-- another tab
a = 1
local b = 1
Hmm… Looks like this has nothing to with tabs indeed — I'm able to recreate it like so:
function _draw()
cls()
print(a) -- no issues as expected
print(b, 0, 8) -- should report issue
end
a = 1
local b = 1
I'm pretty new to Lua so I might be missing something.
Gotcha, I see this does result in b
being nil
inside the function.
I thought that if you declare a local
variable at the top level, i.e. outside of any functions, it's the same as a global variable. But I guess the difference is that it respects ordering in that case. So e.g. this works:
local b = 1
function _draw()
cls()
print(b)
end
But this does not:
function _draw()
cls()
print(b)
end
local b = 1
pico8-ls does not take this into account, so I'll go ahead and re-open the issue.