selene
selene copied to clipboard
new lint: module returns module
There's not a whole lot of reasons a module should return something it didn't actually define itself. It's probably more often a typo rather than intentional, so I propose a warning for situations like this:
local MyModule = require(...) -- A dependency
local MyOtherModule = {} -- The main export of the module
MyOtherModule.SOME_VALUE = "SomeString" -- Some module content
return MyModule -- Whoops!
You can see that MyModule
is the result of a call to require
. I clearly meant to return the main export of the module, MyOtherModule
, something that was actually defined within this module:
return MyOtherModule -- All good :-)
Sometimes you actually want to do something funky like this when it comes to ModuleScripts on Roblox. It's a little anti-patterny, but whatever - If it's actually desired though, it should be silenced by setting a local variable.
local MyModule = require(...)
local MyActualModule = MyModule -- At least it's locally defined!
return MyActualModule
Here's a more practical situation which inspired this: I wrote an idiomatic abstract Lua class Handler
and a concrete subclass DefaultHandler
. When defining DefaultHandler
, I accidentally returned Handler
, as above. It was challenging to notice this due to the similar names and behaviors of the classes.
P.S. Please suggest a different name for this lint, as I couldn't really come up with a more descriptive one.