Skip to content

Commit ebc5016

Browse files
committed
refactor(cli/check): outline some functions and add some comments
1 parent dec44dd commit ebc5016

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
@@ -106,6 +106,65 @@ local function quiet()
106106
return QUIET or QUIET_WORKER
107107
end
108108

109+
--- @param i integer
110+
--- @param max integer
111+
--- @param results table<string, table[]>
112+
local function report_progress(i, max, results)
113+
local filesWithErrors = 0
114+
local errors = 0
115+
for _, diags in pairs(results) do
116+
filesWithErrors = filesWithErrors + 1
117+
errors = errors + #diags
118+
end
119+
120+
clear_line()
121+
io.write(
122+
('>'):rep(math.ceil(i / max * 20)),
123+
('='):rep(20 - math.ceil(i / max * 20)),
124+
' ',
125+
('0'):rep(#tostring(max) - #tostring(i)),
126+
tostring(i),
127+
'/',
128+
tostring(max)
129+
)
130+
if errors > 0 then
131+
io.write(' [', lang.script('CLI_CHECK_PROGRESS', errors, filesWithErrors), ']')
132+
end
133+
io.flush()
134+
end
135+
136+
--- @param uri string
137+
--- @param checkLevel integer
138+
local function apply_check_level(uri, checkLevel)
139+
local config_disables = util.arrayToHash(config.get(uri, 'Lua.diagnostics.disable'))
140+
local config_severities = config.get(uri, 'Lua.diagnostics.severity')
141+
for name, serverity in pairs(define.DiagnosticDefaultSeverity) do
142+
serverity = config_severities[name] or serverity
143+
if serverity:sub(-1) == '!' then
144+
serverity = serverity:sub(1, -2)
145+
end
146+
if define.DiagnosticSeverity[serverity] > checkLevel then
147+
config_disables[name] = true
148+
end
149+
end
150+
config.set(uri, 'Lua.diagnostics.disable', util.getTableKeys(config_disables, true))
151+
end
152+
153+
local function downgrade_checks_to_opened(uri)
154+
local diagStatus = config.get(uri, 'Lua.diagnostics.neededFileStatus')
155+
for d, status in pairs(diagStatus) do
156+
if status == 'Any' or status == 'Any!' then
157+
diagStatus[d] = 'Opened!'
158+
end
159+
end
160+
for d, status in pairs(protoDiag.getDefaultStatus()) do
161+
if status == 'Any' or status == 'Any!' then
162+
diagStatus[d] = 'Opened!'
163+
end
164+
end
165+
config.set(uri, 'Lua.diagnostics.neededFileStatus', diagStatus)
166+
end
167+
109168
function export.runCLI()
110169
lang(LOCALE)
111170

@@ -125,18 +184,16 @@ function export.runCLI()
125184
end
126185
rootUri = rootUri:gsub("/$", "")
127186

128-
if CHECKLEVEL then
129-
if not define.DiagnosticSeverity[CHECKLEVEL] then
130-
print(lang.script('CLI_CHECK_ERROR_LEVEL', 'Error, Warning, Information, Hint'))
131-
return
132-
end
187+
if CHECKLEVEL and not define.DiagnosticSeverity[CHECKLEVEL] then
188+
print(lang.script('CLI_CHECK_ERROR_LEVEL', 'Error, Warning, Information, Hint'))
189+
return
133190
end
134191
local checkLevel = define.DiagnosticSeverity[CHECKLEVEL] or define.DiagnosticSeverity.Warning
135192

136193
util.enableCloseFunction()
137194

138195
local lastClock = os.clock()
139-
local results = {}
196+
local results = {} --- @type table<string, table[]>
140197

141198
local function errorhandler(err)
142199
print(err)
@@ -169,31 +226,12 @@ function export.runCLI()
169226

170227
ws.awaitReady(rootUri)
171228

172-
local disables = util.arrayToHash(config.get(rootUri, 'Lua.diagnostics.disable'))
173-
for name, serverity in pairs(define.DiagnosticDefaultSeverity) do
174-
serverity = config.get(rootUri, 'Lua.diagnostics.severity')[name] or serverity
175-
if serverity:sub(-1) == '!' then
176-
serverity = serverity:sub(1, -2)
177-
end
178-
if define.DiagnosticSeverity[serverity] > checkLevel then
179-
disables[name] = true
180-
end
181-
end
182-
config.set(rootUri, 'Lua.diagnostics.disable', util.getTableKeys(disables, true))
229+
-- Disable any diagnostics that are above the check level
230+
apply_check_level(rootUri, checkLevel)
183231

184-
-- Downgrade file opened status to Opened for everything to avoid reporting during compilation on files that do not belong to this thread
185-
local diagStatus = config.get(rootUri, 'Lua.diagnostics.neededFileStatus')
186-
for d, status in pairs(diagStatus) do
187-
if status == 'Any' or status == 'Any!' then
188-
diagStatus[d] = 'Opened!'
189-
end
190-
end
191-
for d, status in pairs(protoDiag.getDefaultStatus()) do
192-
if status == 'Any' or status == 'Any!' then
193-
diagStatus[d] = 'Opened!'
194-
end
195-
end
196-
config.set(rootUri, 'Lua.diagnostics.neededFileStatus', diagStatus)
232+
-- Downgrade file opened status to Opened for everything to avoid
233+
-- reporting during compilation on files that do not belong to this thread
234+
downgrade_checks_to_opened(rootUri)
197235

198236
local uris = files.getChildFiles(rootUri)
199237
local max = #uris
@@ -202,28 +240,12 @@ function export.runCLI()
202240
if (i % numThreads + 1) == threadId then
203241
files.open(uri)
204242
diag.doDiagnostic(uri, true)
205-
-- Print regularly but always print the last entry to ensure that logs written to files don't look incomplete.
243+
-- Print regularly but always print the last entry to ensure
244+
-- that logs written to files don't look incomplete.
206245
if not quiet() and (os.clock() - lastClock > 0.2 or i == #uris) then
207246
lastClock = os.clock()
208247
client:update()
209-
local output = '\x0D'
210-
.. ('>'):rep(math.ceil(i / max * 20))
211-
.. ('='):rep(20 - math.ceil(i / max * 20))
212-
.. ' '
213-
.. ('0'):rep(#tostring(max) - #tostring(i))
214-
.. tostring(i) .. '/' .. tostring(max)
215-
io.write(output)
216-
local filesWithErrors = 0
217-
local errors = 0
218-
for _, diags in pairs(results) do
219-
filesWithErrors = filesWithErrors + 1
220-
errors = errors + #diags
221-
end
222-
if errors > 0 then
223-
local errorDetails = ' [' .. lang.script('CLI_CHECK_PROGRESS', errors, filesWithErrors) .. ']'
224-
io.write(errorDetails)
225-
end
226-
io.flush()
248+
report_progress(i, max, results)
227249
end
228250
end
229251
end
@@ -244,7 +266,8 @@ function export.runCLI()
244266

245267
if CHECK_FORMAT == 'json' or CHECK_OUT_PATH then
246268
outpath = CHECK_OUT_PATH or LOGPATH .. '/check.json'
247-
-- Always write result, even if it's empty to make sure no one accidentally looks at an old output after a successful run.
269+
-- Always write result, even if it's empty to make sure no one
270+
-- accidentally looks at an old output after a successful run.
248271
util.saveFile(outpath, jsonb.beautify(results))
249272
end
250273

0 commit comments

Comments
 (0)