SUPER STRICT for LUA

Showcase your libraries, tools and other projects that help your fellow love users.
MrFariator
Party member
Posts: 509
Joined: Wed Oct 05, 2016 11:53 am

Re: SUPER STRICT for LUA

Post by MrFariator »

I have cases where I let certain functions (debug stuff, mainly) be replaced by empty functions in "release" builds, effectively turning those into NoOPs, so sstrict threw a bunch of hissyfits about that.

Another issue I ran into was the following function (from bump.lua):

Code: Select all

function World:hasItem(item)
  return not not self.rects[item] -- unexpected symbol 'not'
end
A third issue I ran into was along the lines of:

Code: Select all

local val1, _
val1,_,val3,val4 = functionThatReturnsFourValues() -- undefined variable '_'
Granted, can just rename the '_' to be something else

I think some hints for the sstrict via comments might be the way to go? Like how you might tell a linter to ignore the following line of code, or ignore specific warning that the linter would otherwise raise. That way you could still keep the warnings about empty code blocks, but nothing is done if user writes "-- sstrict-disable-next-code-block empty-block" or similar before it, aping eslint hints. Having a bunch of such hinting options wouldn't exactly make sstrict a very drop-in solution (well, it already is only for the bravest of souls in this form for existing projects), but it would alleviate some problems with it.

Lastly, I notice that sstrict doesn't have a license?
User avatar
ivan
Party member
Posts: 1911
Joined: Fri Mar 07, 2008 1:39 pm
Contact:

Re: SUPER STRICT for LUA

Post by ivan »

Thanks for trying SUPERSTRICT.

Code: Select all

return not not self.rects
This is a weird-looking line, but yea I can adjust the parser to support it.

Code: Select all

-- undefined variable '_'
Good catch, this is definitely a bug.
I think some hints for the sstrict via comments might be the way to go
You could use the !strict command as mentioned earlier

Code: Select all

--!strict
function ignoreJustThisFunc()
end
--!strict
well, it already is only for the bravest of souls in this form for existing projects)
Parsing Lua is hard but I think we did a decent job testing it thanks to grump and pgimeno. :)
Lastly, I notice that sstrict doesn't have a license?
Just added an MIT License file, although I don't recommend running SUPERSTRICT in production code (just use it for testing - there is no reason to redistribute SUPERSTRICT itself). Although mentioning 2dengine somewhere in the credits is highly appreciated.

PS. Please check the version on Bitbucket. I have gone ahead and fixed the previous two issues.
Also SUPERSTRICT now allows empty functions. All other empty code blocks are reported.
MrFariator
Party member
Posts: 509
Joined: Wed Oct 05, 2016 11:53 am

Re: SUPER STRICT for LUA

Post by MrFariator »

Ah, I must have missed the --!strict command as I was glancing through the thread and plopping sstrict into my code. Using that in tandem with the updated version with the couple fixes seems to be running mostly fine for now, as far as I can see in my testing. However, I did run in to this one issue:

Code: Select all

MyGuiHandler.createWidget(type)
  :addCallback(callback) -- unexpected symbol ':'
  :addSomethingElse(somethingElse)
This being a fairly common code formatting thing I do for chaining function calls to create things like GUI widgets and textbox objects, since it'd be awful for readability to cram those into a single function or line of code.
I don't recommend running SUPERSTRICT in production code (just use it for testing - there is no reason to redistribute SUPERSTRICT itself). Although mentioning 2dengine somewhere in the credits is highly appreciated.
Of course, it's still just nice to have a license handy - even if it's ever going to be present in internal debug builds and testing, not release stuff.
User avatar
ivan
Party member
Posts: 1911
Joined: Fri Mar 07, 2008 1:39 pm
Contact:

Re: SUPER STRICT for LUA

Post by ivan »

This being a fairly common code formatting thing I do for chaining function calls to create things like GUI widgets and textbox objects, since it'd be awful for readability to cram those into a single function or line of code.
Ok I have pushed another fix so please pull the latest version from BitBucket.
Also, post any other issues here and I'll try to fix them as they come along.
User avatar
zorg
Party member
Posts: 3435
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: SUPER STRICT for LUA

Post by zorg »

ivan wrote: Sat Jan 16, 2021 7:58 am

Code: Select all

return not not self.rects
This is a weird-looking line, but yea I can adjust the parser to support it.
not can be stacked more than twice, just saying that more than two should parse fine as well. :3
ivan wrote: Sat Jan 16, 2021 7:58 am Also SUPERSTRICT now allows empty functions. All other empty code blocks are reported.
so these ones, right?: do, repeat, for, if, then, else, while.
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
MrFariator
Party member
Posts: 509
Joined: Wed Oct 05, 2016 11:53 am

Re: SUPER STRICT for LUA

Post by MrFariator »

Cheers for the another quick fix. I've managed to have sstrict encompass several thousand lines of code pretty neatly, the only minor problem I noticed was that sometimes when you have unused or redefined variables, the error would be raised incorrectly for the next line. Ex:

Code: Select all

local myVar, myUnusedVar
for i = 1, 100 do -- unused var 'myUnusedVar'
-- ...etc
Only reason I'm stopping for now is that sstrict started hitting files that are sandboxed (using this lib by kikito) while they are being parsed, and sstrict naturally has no idea that the _G might get replaced like this. I guess you'd have to recheck _G for every different file, which would add quite a bit of additional overhead to parsing times.
User avatar
ivan
Party member
Posts: 1911
Joined: Fri Mar 07, 2008 1:39 pm
Contact:

Re: SUPER STRICT for LUA

Post by ivan »

zorg wrote: Sat Jan 16, 2021 10:40 amnot can be stacked more than twice, just saying that more than two should parse fine as well. :3
Yep, should work fine
zorg wrote: Sat Jan 16, 2021 10:40 amso these ones, right?: do, repeat, for, if, then, else, while.
Yes!
error would be raised incorrectly for the next line
Thanks, I will look into this!
sstrict naturally has no idea that the _G might get replaced like this
setenv is a runtime thing so you will need the regular strict.lua to check that during execution.
User avatar
pgimeno
Party member
Posts: 3541
Joined: Sun Oct 18, 2015 2:58 pm

Re: SUPER STRICT for LUA

Post by pgimeno »

zorg wrote: Sat Jan 16, 2021 10:40 am not can be stacked more than twice, just saying that more than two should parse fine as well. :3
Indeed, that's pretty common with e.g. flux, see https://gist.github.com/rxi/11265117#fi ... es-lua-L19

This sounds like it's going to be an endless whack-a-bug. Luacheck's parser already covers the full language properly and the use cases of this tool.
User avatar
ivan
Party member
Posts: 1911
Joined: Fri Mar 07, 2008 1:39 pm
Contact:

Re: SUPER STRICT for LUA

Post by ivan »

pgimeno wrote: Sat Jan 16, 2021 6:00 pm This sounds like it's going to be an endless whack-a-bug. Luacheck's parser already covers the full language properly and the use cases of this tool.
It's not that bad. I'm sure Luacheck is a lot more sophisticated, but SUPERSTRICT is easier to install/use.

I have included an additional check for constant conditions in if/else statements:

Code: Select all

if 5+5 > 11 then print('nooo!') end -- constant condition error
Also added an "unnecessary code block" check for code that doesn't affect anything outside of its scope:

Code: Select all

function uneccessaryFunc()
  local a = 5
  a = a + 1 -- unnecessary code block error
end
I have added a couple of special cases for the empty blocks check. If there is any kind of table access (for example foo['a'] or bar.a) we can't be sure if this an empty block or not because there may be metatables involved. Therefore the following is considered to be a non-empty code block:

Code: Select all

while _G['a'] do end
while _G.a do end
-- neither of these is an empty block (we can't be sure) because
-- _G['a'] or _G.a *could* possibly trigger a metatable function
PS.
Did some additional work to catch duplicate fields in table constructors:

Code: Select all

local t = { ['a'] = 100, a = 100 }
User avatar
ivan
Party member
Posts: 1911
Joined: Fri Mar 07, 2008 1:39 pm
Contact:

Re: SUPER STRICT for LUA

Post by ivan »

MrFariator wrote: Sat Jan 16, 2021 11:53 amwhen you have unused or redefined variables, the error would be raised incorrectly for the next line.
This should be fixed after today's update. Would be good if you can confirm!
Post Reply

Who is online

Users browsing this forum: No registered users and 13 guests