Skip to content

Commit 727715d

Browse files
committed
refactor(cli/check): outline some functions and add some comments
1 parent 8595322 commit 727715d

File tree

1 file changed

+73
-50
lines changed

1 file changed

+73
-50
lines changed

script/cli/check_worker.lua

Lines changed: 73 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,65 @@ local function quiet()
120120
return QUIET or QUIET_WORKER
121121
end
122122

123+
--- @param i integer
124+
--- @param max integer
125+
--- @param results table<string, table[]>
126+
local function report_progress(i, max, results)
127+
local filesWithErrors = 0
128+
local errors = 0
129+
for _, diags in pairs(results) do
130+
filesWithErrors = filesWithErrors + 1
131+
errors = errors + #diags
132+
end
133+
134+
clear_line()
135+
io.write(
136+
('>'):rep(math.ceil(i / max * 20)),
137+
('='):rep(20 - math.ceil(i / max * 20)),
138+
' ',
139+
('0'):rep(#tostring(max) - #tostring(i)),
140+
tostring(i),
141+
'/',
142+
tostring(max)
143+
)
144+
if errors > 0 then
145+
io.write(' [', lang.script('CLI_CHECK_PROGRESS', errors, filesWithErrors), ']')
146+
end
147+
io.flush()
148+
end
149+
150+
--- @param uri string
151+
--- @param checkLevel integer
152+
local function apply_check_level(uri, checkLevel)
153+
local config_disables = util.arrayToHash(config.get(uri, 'Lua.diagnostics.disable'))
154+
local config_severities = config.get(uri, 'Lua.diagnostics.severity')
155+
for name, serverity in pairs(define.DiagnosticDefaultSeverity) do
156+
serverity = config_severities[name] or serverity
157+
if serverity:sub(-1) == '!' then
158+
serverity = serverity:sub(1, -2)
159+
end
160+
if define.DiagnosticSeverity[serverity] > checkLevel then
161+
config_disables[name] = true
162+
end
163+
end
164+
config.set(uri, 'Lua.diagnostics.disable', util.getTableKeys(config_disables, true))
165+
end
166+
167+
local function downgrade_checks_to_opened(uri)
168+
local diagStatus = config.get(uri, 'Lua.diagnostics.neededFileStatus')
169+
for d, status in pairs(diagStatus) do
170+
if status == 'Any' or status == 'Any!' then
171+
diagStatus[d] = 'Opened!'
172+
end
173+
end
174+
for d, status in pairs(protoDiag.getDefaultStatus()) do
175+
if status == 'Any' or status == 'Any!' then
176+
diagStatus[d] = 'Opened!'
177+
end
178+
end
179+
config.set(uri, 'Lua.diagnostics.neededFileStatus', diagStatus)
180+
end
181+
123182
function export.runCLI()
124183
lang(LOCALE)
125184

@@ -139,18 +198,16 @@ function export.runCLI()
139198
end
140199
rootUri = rootUri:gsub("/$", "")
141200

142-
if CHECKLEVEL then
143-
if not define.DiagnosticSeverity[CHECKLEVEL] then
144-
print(lang.script('CLI_CHECK_ERROR_LEVEL', 'Error, Warning, Information, Hint'))
145-
return
146-
end
201+
if CHECKLEVEL and not define.DiagnosticSeverity[CHECKLEVEL] then
202+
print(lang.script('CLI_CHECK_ERROR_LEVEL', 'Error, Warning, Information, Hint'))
203+
return
147204
end
148205
local checkLevel = define.DiagnosticSeverity[CHECKLEVEL] or define.DiagnosticSeverity.Warning
149206

150207
util.enableCloseFunction()
151208

152209
local lastClock = os.clock()
153-
local results = {}
210+
local results = {} --- @type table<string, table[]>
154211

155212
local function errorhandler(err)
156213
print(err)
@@ -182,31 +239,12 @@ function export.runCLI()
182239

183240
ws.awaitReady(rootUri)
184241

185-
local disables = util.arrayToHash(config.get(rootUri, 'Lua.diagnostics.disable'))
186-
for name, serverity in pairs(define.DiagnosticDefaultSeverity) do
187-
serverity = config.get(rootUri, 'Lua.diagnostics.severity')[name] or serverity
188-
if serverity:sub(-1) == '!' then
189-
serverity = serverity:sub(1, -2)
190-
end
191-
if define.DiagnosticSeverity[serverity] > checkLevel then
192-
disables[name] = true
193-
end
194-
end
195-
config.set(rootUri, 'Lua.diagnostics.disable', util.getTableKeys(disables, true))
242+
-- Disable any diagnostics that are above the check level
243+
apply_check_level(rootUri, checkLevel)
196244

197-
-- Downgrade file opened status to Opened for everything to avoid reporting during compilation on files that do not belong to this thread
198-
local diagStatus = config.get(rootUri, 'Lua.diagnostics.neededFileStatus')
199-
for d, status in pairs(diagStatus) do
200-
if status == 'Any' or status == 'Any!' then
201-
diagStatus[d] = 'Opened!'
202-
end
203-
end
204-
for d, status in pairs(protoDiag.getDefaultStatus()) do
205-
if status == 'Any' or status == 'Any!' then
206-
diagStatus[d] = 'Opened!'
207-
end
208-
end
209-
config.set(rootUri, 'Lua.diagnostics.neededFileStatus', diagStatus)
245+
-- Downgrade file opened status to Opened for everything to avoid
246+
-- reporting during compilation on files that do not belong to this thread
247+
downgrade_checks_to_opened(rootUri)
210248

211249
local uris = files.getChildFiles(rootUri)
212250
local max = #uris
@@ -215,28 +253,12 @@ function export.runCLI()
215253
if (i % numThreads + 1) == threadId then
216254
files.open(uri)
217255
diag.doDiagnostic(uri, true)
218-
-- Print regularly but always print the last entry to ensure that logs written to files don't look incomplete.
256+
-- Print regularly but always print the last entry to ensure
257+
-- that logs written to files don't look incomplete.
219258
if not quiet() and (os.clock() - lastClock > 0.2 or i == #uris) then
220259
lastClock = os.clock()
221260
client:update()
222-
local output = '\x0D'
223-
.. ('>'):rep(math.ceil(i / max * 20))
224-
.. ('='):rep(20 - math.ceil(i / max * 20))
225-
.. ' '
226-
.. ('0'):rep(#tostring(max) - #tostring(i))
227-
.. tostring(i) .. '/' .. tostring(max)
228-
io.write(output)
229-
local filesWithErrors = 0
230-
local errors = 0
231-
for _, diags in pairs(results) do
232-
filesWithErrors = filesWithErrors + 1
233-
errors = errors + #diags
234-
end
235-
if errors > 0 then
236-
local errorDetails = ' [' .. lang.script('CLI_CHECK_PROGRESS', errors, filesWithErrors) .. ']'
237-
io.write(errorDetails)
238-
end
239-
io.flush()
261+
report_progress(i, max, results)
240262
end
241263
end
242264
end
@@ -257,7 +279,8 @@ function export.runCLI()
257279

258280
if CHECK_FORMAT == 'json' or CHECK_OUT_PATH then
259281
outpath = CHECK_OUT_PATH or LOGPATH .. '/check.json'
260-
-- Always write result, even if it's empty to make sure no one accidentally looks at an old output after a successful run.
282+
-- Always write result, even if it's empty to make sure no one
283+
-- accidentally looks at an old output after a successful run.
261284
util.saveFile(outpath, jsonb.beautify(results))
262285
end
263286

0 commit comments

Comments
 (0)