Skip to content

Commit 75ab84a

Browse files
committed
refactor(cli/check): outline some functions and add some comments
1 parent 89356a5 commit 75ab84a

File tree

1 file changed

+74
-51
lines changed

1 file changed

+74
-51
lines changed

script/cli/check_worker.lua

Lines changed: 74 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,65 @@ local function clear_line()
102102
io.write('\x0D', (' '):rep(80), '\x0D')
103103
end
104104

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

@@ -121,18 +180,16 @@ function export.runCLI()
121180
end
122181
rootUri = rootUri:gsub("/$", "")
123182

124-
if CHECKLEVEL then
125-
if not define.DiagnosticSeverity[CHECKLEVEL] then
126-
print(lang.script('CLI_CHECK_ERROR_LEVEL', 'Error, Warning, Information, Hint'))
127-
return
128-
end
183+
if CHECKLEVEL and not define.DiagnosticSeverity[CHECKLEVEL] then
184+
print(lang.script('CLI_CHECK_ERROR_LEVEL', 'Error, Warning, Information, Hint'))
185+
return
129186
end
130187
local checkLevel = define.DiagnosticSeverity[CHECKLEVEL] or define.DiagnosticSeverity.Warning
131188

132189
util.enableCloseFunction()
133190

134191
local lastClock = os.clock()
135-
local results = {}
192+
local results = {} --- @type table<string, table[]>
136193

137194
local function errorhandler(err)
138195
print(err)
@@ -164,31 +221,12 @@ function export.runCLI()
164221

165222
ws.awaitReady(rootUri)
166223

167-
local disables = util.arrayToHash(config.get(rootUri, 'Lua.diagnostics.disable'))
168-
for name, serverity in pairs(define.DiagnosticDefaultSeverity) do
169-
serverity = config.get(rootUri, 'Lua.diagnostics.severity')[name] or serverity
170-
if serverity:sub(-1) == '!' then
171-
serverity = serverity:sub(1, -2)
172-
end
173-
if define.DiagnosticSeverity[serverity] > checkLevel then
174-
disables[name] = true
175-
end
176-
end
177-
config.set(rootUri, 'Lua.diagnostics.disable', util.getTableKeys(disables, true))
224+
-- Disable any diagnostics that are above the check level
225+
apply_check_level(rootUri, checkLevel)
178226

179-
-- Downgrade file opened status to Opened for everything to avoid reporting during compilation on files that do not belong to this thread
180-
local diagStatus = config.get(rootUri, 'Lua.diagnostics.neededFileStatus')
181-
for d, status in pairs(diagStatus) do
182-
if status == 'Any' or status == 'Any!' then
183-
diagStatus[d] = 'Opened!'
184-
end
185-
end
186-
for d, status in pairs(protoDiag.getDefaultStatus()) do
187-
if status == 'Any' or status == 'Any!' then
188-
diagStatus[d] = 'Opened!'
189-
end
190-
end
191-
config.set(rootUri, 'Lua.diagnostics.neededFileStatus', diagStatus)
227+
-- Downgrade file opened status to Opened for everything to avoid
228+
-- reporting during compilation on files that do not belong to this thread
229+
downgrade_checks_to_opened(rootUri)
192230

193231
local uris = files.getChildFiles(rootUri)
194232
local max = #uris
@@ -197,28 +235,12 @@ function export.runCLI()
197235
if (i % numThreads + 1) == threadId then
198236
files.open(uri)
199237
diag.doDiagnostic(uri, true)
200-
-- Print regularly but always print the last entry to ensure that logs written to files don't look incomplete.
201-
if (os.clock() - lastClock > 0.2 or i == #uris) and not QUIET then
238+
-- Print regularly but always print the last entry to ensure
239+
-- that logs written to files don't look incomplete.
240+
if not QUIET and (os.clock() - lastClock > 0.2 or i == max) then
202241
lastClock = os.clock()
203242
client:update()
204-
local output = '\x0D'
205-
.. ('>'):rep(math.ceil(i / max * 20))
206-
.. ('='):rep(20 - math.ceil(i / max * 20))
207-
.. ' '
208-
.. ('0'):rep(#tostring(max) - #tostring(i))
209-
.. tostring(i) .. '/' .. tostring(max)
210-
io.write(output)
211-
local filesWithErrors = 0
212-
local errors = 0
213-
for _, diags in pairs(results) do
214-
filesWithErrors = filesWithErrors + 1
215-
errors = errors + #diags
216-
end
217-
if errors > 0 then
218-
local errorDetails = ' [' .. lang.script('CLI_CHECK_PROGRESS', errors, filesWithErrors) .. ']'
219-
io.write(errorDetails)
220-
end
221-
io.flush()
243+
report_progress(i, max, results)
222244
end
223245
end
224246
end
@@ -239,7 +261,8 @@ function export.runCLI()
239261

240262
if CHECK_FORMAT == 'json' or CHECK_OUT_PATH then
241263
outpath = CHECK_OUT_PATH or LOGPATH .. '/check.json'
242-
-- Always write result, even if it's empty to make sure no one accidentally looks at an old output after a successful run.
264+
-- Always write result, even if it's empty to make sure no one
265+
-- accidentally looks at an old output after a successful run.
243266
util.saveFile(outpath, jsonb.beautify(results))
244267
end
245268

0 commit comments

Comments
 (0)