Skip to content

Commit

Permalink
Enable Luau strict mode (#22)
Browse files Browse the repository at this point in the history
# Problem

The repo is using non-strict which is letting some minor type errors get
through

# Solution

Enabled strict mode via .luaurc and fixed the errors it uncovered for
the Lune scripts and ModuleLoader itself
  • Loading branch information
vocksel authored Sep 8, 2024
1 parent 47c0f7b commit d0744b5
Show file tree
Hide file tree
Showing 10 changed files with 28 additions and 20 deletions.
1 change: 1 addition & 0 deletions .luaurc
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"languageMode": "strict",
"aliases": {
"pkg": "./Packages",
"root": "./src"
Expand Down
1 change: 1 addition & 0 deletions .lune/build.luau
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ local watch = require("./lib/watcher/watch")
local args = parseArgs(process.args)

local target = if args.target then args.target else "prod"
assert(target == "dev" or target == "prod", `bad value for target (must be one of "dev" or "prod", got "{target}")`)

local function build()
run("rm", { "-rf", project.BUILD_PATH })
Expand Down
2 changes: 1 addition & 1 deletion .lune/lib/findClientSettings.luau
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
local run = require("./run")

local function findClientSettings(os: string)
local function findClientSettings(os: string): string?
if os == "macos" then
return "/Applications/RobloxStudio.app/Contents/MacOS/ClientSettings"
elseif os == "windows" then
Expand Down
13 changes: 6 additions & 7 deletions .lune/lib/parseArgs.luau
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
local FLAG_PATTERN = "%-%-(%w+)"
local FLAG_ALL_IN_ONE_PATTERN = `{FLAG_PATTERN}=(%w+)`

local function parseArgs(args: { string }): { [string]: string | boolean | number }
local parsedArgs: { [string]: string } = {}
local function parseArgs(args: { string })
local parsedArgs: { [string]: string | boolean | number } = {}

local skipNextToken = false

for index, token in args do
Expand All @@ -28,16 +29,14 @@ local function parseArgs(args: { string }): { [string]: string | boolean | numbe
if nextToken then
-- When processing `--foo` in `--foo --bar` treat it like a boolean
if nextToken:match(FLAG_PATTERN) then
flagValue = true
parsedArgs[flagName] = true
else
flagValue = nextToken
parsedArgs[flagName] = nextToken
skipNextToken = true
end
else
flagValue = true
parsedArgs[flagName] = true
end

parsedArgs[flagName] = flagValue
else
error(`something went wrong: {token}`)
end
Expand Down
6 changes: 4 additions & 2 deletions .lune/lib/setFlags.luau
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ local run = require("./run")

local function setFlags(os: string)
local clientSettings = findClientSettings(os)
run("mkdir", { "-p", clientSettings })
run("cp", { "-R", "tests/ClientAppSettings.json", clientSettings })
if clientSettings then
run("mkdir", { "-p", clientSettings })
run("cp", { "-R", "tests/ClientAppSettings.json", clientSettings })
end
end

return setFlags
2 changes: 1 addition & 1 deletion .lune/lib/watcher/watch.luau
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ type Options = {
}

local function watch(options: Options)
local prevWatchedFileMetadata: { [string]: string } = {}
local prevWatchedFileMetadata: { [string]: fs.Metadata } = {}
local watchedFiles = getWatchedFiles(options.filePatterns)
local prevWatchedFiles

Expand Down
18 changes: 12 additions & 6 deletions src/ModuleLoader.luau
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
local GoodSignal = require("@pkg/GoodSignal")
local Janitor = require("@pkg/Janitor")

local bind = require("./bind")
Expand Down Expand Up @@ -33,9 +32,10 @@ type ModuleLoaderProps = {
_cache: { [string]: CachedModule },
_loadstring: typeof(loadstring),
_debugInfo: typeof(debug.info),
_janitors: { [string]: typeof(Janitor.new()) },
_janitors: { [string]: any },
_globals: { [any]: any },

_loadedModuleChangedBindable: BindableEvent,
loadedModuleChanged: RBXScriptSignal,
}

Expand Down Expand Up @@ -82,6 +82,7 @@ function ModuleLoader.new()
self._debugInfo = debug.info
self._janitors = {}
self._globals = {}
self._loadedModuleChangedBindable = Instance.new("BindableEvent")

--[=[
Fired when any ModuleScript required through this class has its ancestry
Expand All @@ -104,7 +105,7 @@ function ModuleLoader.new()
@prop loadedModuleChanged RBXScriptSignal
@within ModuleLoader
]=]
self.loadedModuleChanged = GoodSignal.new()
self.loadedModuleChanged = self._loadedModuleChangedBindable.Event

return setmetatable(self, ModuleLoader)
end
Expand Down Expand Up @@ -211,6 +212,10 @@ function ModuleLoader:require(module: ModuleScript)
local cachedModule = self._cache[module:GetFullName()]
local callerPath = getCallerPath()

if not callerPath then
return nil
end

if cachedModule then
cachedModule.consumers[callerPath] = true
return self:_loadCachedModule(module)
Expand All @@ -220,7 +225,8 @@ function ModuleLoader:require(module: ModuleScript)
local moduleFn, parseError = self._loadstring(source, module:GetFullName())

if not moduleFn then
error(("Could not parse %s: %s"):format(module:GetFullName(), parseError))
local message = if parseError then parseError else "<no error message>"
error(`Could not parse {module:GetFullName()}: {message}`)
end

local globals = createTablePassthrough(self._globals)
Expand All @@ -236,7 +242,7 @@ function ModuleLoader:require(module: ModuleScript)
}
self._cache[module:GetFullName()] = newCachedModule

local env = getEnv(module, globals)
local env: any = getEnv(module, globals)
env.require = bind(self, self.require)
setfenv(moduleFn, env)

Expand Down Expand Up @@ -312,7 +318,7 @@ function ModuleLoader:clearModule(moduleToClear: ModuleScript)
end

for _, module in modulesToClear do
self.loadedModuleChanged:Fire(module)
self._loadedModuleChangedBindable:Fire(module)
end
end

Expand Down
2 changes: 1 addition & 1 deletion src/ModuleLoader.spec.luau
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ type ModuleTestTree = {
[string]: string | ModuleTestTree,
}
local testNumber = 0
local function createModuleTest(tree: ModuleTestTree, parent: Instance?)
local function createModuleTest(tree: ModuleTestTree, parent: Instance?): any
testNumber += 1

local root = Instance.new("Folder")
Expand Down
2 changes: 1 addition & 1 deletion src/getCallerPath.luau
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ local root = script.Parent

local LOADSTRING_PATH_PATTERN = '%[string "(.*)"%]'

local function getCallerPath()
local function getCallerPath(): string?
local level = 1

while true do
Expand Down
1 change: 0 additions & 1 deletion wally.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ include = [
]

[dependencies]
GoodSignal = "stravant/[email protected]"
Janitor = "howmanysmall/[email protected]"

#[dev-dependencies]
Expand Down

0 comments on commit d0744b5

Please sign in to comment.