Skip to content

Commit 41298cc

Browse files
committed
add severity levels and fix some issues with path not being correct in diagnostics
1 parent 42dd28c commit 41298cc

5 files changed

Lines changed: 125 additions & 41 deletions

File tree

nattlua/analyzer/base/error_handling.lua

Lines changed: 72 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
local table = _G.table
2+
local table_insert = table.insert
23
local type = type
34
local ipairs = ipairs
45
local tostring = tostring
@@ -89,7 +90,8 @@ return function(META--[[#: any]])
8990

9091
function META:ReportDiagnostic(
9192
msg--[[#: {reasons = {[number] = string}} | {[number] = string}]],
92-
severity--[[#: "warning" | "error"]],
93+
severity--[[#: "warning" | "error" | "fatal"]],
94+
level--[[#: number | nil]],
9395
node--[[#: any]],
9496
code--[[#: any]],
9597
start--[[#: number]],
@@ -109,10 +111,22 @@ return function(META--[[#: any]])
109111

110112
local msg_str = error_messages.ErrorMessageToString(msg)
111113

114+
if
115+
severity == "error" and
116+
(
117+
msg_str:find("does not exist", nil, true) or
118+
msg_str:find("has no key", nil, true)
119+
)
120+
then
121+
severity = "warning"
122+
level = 1
123+
end
124+
112125
if self.processing_deferred_calls then
113126
msg_str = "DEFERRED CALL: " .. msg_str
114127
end
115128

129+
--[[
116130
if
117131
self.expect_diagnostic and
118132
self.expect_diagnostic[1] and
@@ -127,9 +141,18 @@ return function(META--[[#: any]])
127141
table.remove(self.expect_diagnostic, 1)
128142
return
129143
end
130-
144+
]]
131145
do
132-
local key = msg_str .. "-" .. "severity" .. start .. "-" .. stop
146+
local key = msg_str .. "-" .. (
147+
severity or
148+
"error"
149+
) .. "-" .. (
150+
start or
151+
0
152+
) .. "-" .. (
153+
stop or
154+
0
155+
)
133156
self.diagnostics_map = self.diagnostics_map or {}
134157

135158
if self.diagnostics_map[key] then return end
@@ -138,10 +161,10 @@ return function(META--[[#: any]])
138161
end
139162

140163
if self.OnDiagnostic and not self:IsTypeProtectedCall() then
141-
self:OnDiagnostic(code, msg_str, severity, start, stop, node)
164+
self:OnDiagnostic(code, msg_str, severity, start, stop, node, level)
142165
end
143166

144-
table.insert(
167+
table_insert(
145168
self.diagnostics,
146169
{
147170
node = node,
@@ -150,6 +173,7 @@ return function(META--[[#: any]])
150173
stop = stop,
151174
msg = msg_str,
152175
severity = severity,
176+
level = level,
153177
traceback = callstack.traceback(),
154178
protected_call = self:IsTypeProtectedCall(),
155179
}
@@ -172,19 +196,55 @@ return function(META--[[#: any]])
172196
end
173197
end
174198

175-
function META:Error(msg, node)
199+
function META:Error(msg, level_or_node, node)
200+
local level
201+
202+
if type(level_or_node) == "number" then
203+
level = level_or_node
204+
else
205+
node = level_or_node
206+
end
207+
176208
node = node or self:GetCurrentExpression() or self:GetCurrentStatement()
177-
self:ReportDiagnostic(msg, "error", node, node.Code, node:GetStartStop())
209+
local start, stop = 0, 0
210+
211+
if node then start, stop = node:GetStartStop() end
212+
213+
self:ReportDiagnostic(msg, "error", level, node, node and node.Code, start, stop)
178214
end
179215

180-
function META:Warning(msg, node)
216+
function META:Warning(msg, level_or_node, node)
217+
local level
218+
219+
if type(level_or_node) == "number" then
220+
level = level_or_node
221+
else
222+
node = level_or_node
223+
end
224+
181225
node = node or self:GetCurrentExpression() or self:GetCurrentStatement()
182-
self:ReportDiagnostic(msg, "warning", node, node.Code, node:GetStartStop())
226+
local start, stop = 0, 0
227+
228+
if node then start, stop = node:GetStartStop() end
229+
230+
self:ReportDiagnostic(msg, "warning", level, node, node and node.Code, start, stop)
183231
end
184232

185-
function META:FatalError(msg)
186-
local node = self:GetCurrentExpression() or self:GetCurrentStatement()
187-
self:ReportDiagnostic(msg, "fatal", node, node.Code, node:GetStartStop())
233+
function META:FatalError(msg, level_or_node, node)
234+
local level
235+
236+
if type(level_or_node) == "number" then
237+
level = level_or_node
238+
else
239+
node = level_or_node
240+
end
241+
242+
local node = node or self:GetCurrentExpression() or self:GetCurrentStatement()
243+
local start, stop = 0, 0
244+
245+
if node then start, stop = node:GetStartStop() end
246+
247+
self:ReportDiagnostic(msg, "fatal", level, node, node and node.Code, start, stop)
188248
error(msg, 2)
189249
end
190250

nattlua/analyzer/operators/function_call_body.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -476,14 +476,14 @@ return function(self, obj, input)
476476
local node = function_node.identifiers[i + 1]
477477

478478
if node and not node.type_expression then
479-
self:Warning(error_messages.untyped_argument(), node.type_expression)
479+
self:Warning(error_messages.untyped_argument(), 2, node.type_expression or node)
480480
end
481481
elseif
482482
function_node.identifiers[i] and
483483
not function_node.identifiers[i].type_expression
484484
then
485485
if not obj:IsInputArgumentsInferred() then
486-
self:Warning(error_messages.untyped_argument(), function_node.identifiers[i])
486+
self:Warning(error_messages.untyped_argument(), 2, function_node.identifiers[i])
487487
end
488488
end
489489
end

nattlua/cli/init.lua

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,6 @@ local function parse_args(args, allowed_options)
3030
val = loadstring("return " .. exp)()
3131
end
3232

33-
print(arg, val)
34-
3533
if allowed_options and options[option] == nil then
3634
error("unknown option " .. option)
3735
end
@@ -100,6 +98,7 @@ config.commands["check"] = {
10098
options = {
10199
{name = "profile", description = "Run with profiler"},
102100
{name = "error-only", description = "Only print errors, not warnings"},
101+
{name = "show-severity", description = "Show severity level for warnings"},
103102
},
104103
cb = function(args, options, config, cli)
105104
if options.profile then require("test.helpers.profiler").Start() end
@@ -108,6 +107,8 @@ config.commands["check"] = {
108107
local cmp = {}
109108
local entry_point = nil
110109

110+
if options["show-severity"] then config.analyzer.show_severity = true end
111+
111112
if #args == 1 and args[1] == "-" then
112113
cmp[1] = Compiler.New(assert(io.read("*all")), "stdin-", config)
113114
elseif #args == 0 and config.entry_point then
@@ -124,9 +125,9 @@ config.commands["check"] = {
124125
for _, cmp in ipairs(cmp) do
125126
if options["error-only"] then
126127
local original_OnDiagnostic = cmp.OnDiagnostic
127-
cmp.OnDiagnostic = function(self, code, msg, severity, ...)
128+
cmp.OnDiagnostic = function(self, code, msg, severity, start, stop, node, level)
128129
if severity == "error" or severity == "fatal" then
129-
return original_OnDiagnostic(self, code, msg, severity, ...)
130+
return original_OnDiagnostic(self, code, msg, severity, start, stop, node, level)
130131
end
131132
end
132133
end

nattlua/compiler.lua

Lines changed: 38 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -64,12 +64,9 @@ function META:__tostring()
6464
return str
6565
end
6666

67-
function META:OnDiagnostic(code, msg, severity, start, stop, node, ...)
68-
local t = 0
67+
function META:OnDiagnostic(code, msg, severity, start, stop, node, level, ...)
68+
local t = #self.errors + 1
6969
msg = stringx.replace(msg, " because ", "\nbecause ")
70-
71-
if t > 0 then msg = "\n" .. msg end
72-
7370
local messages = {}
7471

7572
if self.analyzer then
@@ -90,19 +87,45 @@ function META:OnDiagnostic(code, msg, severity, start, stop, node, ...)
9087
table.insert(messages, path .. ":" .. info.line_start .. ":" .. info.character_start)
9188
else
9289
for k, v in pairs(v.obj) do
93-
print(k, v)
90+
91+
-- print(k, v)
9492
end
9593
end
9694
end
9795
end
9896
end
9997

100-
table.insert(messages, formating.FormatMessage(msg, ...))
101-
local msg = formating.BuildSourceCodePointMessage2(
102-
code:GetString(),
98+
msg = formating.FormatMessage(msg, ...)
99+
local title = severity
100+
101+
if
102+
level and
103+
self.Config and
104+
self.Config.analyzer and
105+
self.Config.analyzer.show_severity
106+
then
107+
title = severity .. "(" .. level .. ")"
108+
end
109+
110+
table.insert(messages, msg)
111+
local str_code = "unknown code"
112+
local path = "unknown path"
113+
114+
if code then
115+
str_code = code:GetString()
116+
path = code:GetName()
117+
end
118+
119+
msg = formating.BuildSourceCodePointMessage2(
120+
str_code,
103121
start,
104122
stop,
105-
{path = code:GetName(), messages = messages, surrounding_line_count = 1}
123+
{
124+
path = path,
125+
messages = messages,
126+
surrounding_line_count = 1,
127+
title = title,
128+
}
106129
) .. "\n"
107130

108131
if not NATTLUA_MARKDOWN_OUTPUT then
@@ -239,8 +262,8 @@ function META:Analyze(analyzer, ...)
239262
analyzer = analyzer or Analyzer(self.Config and self.Config.analyzer)
240263
analyzer.compiler = self
241264
self.analyzer = analyzer
242-
analyzer.OnDiagnostic = function(analyzer, ...)
243-
self:OnDiagnostic(...)
265+
analyzer.OnDiagnostic = function(analyzer, code, msg, severity, start, stop, node, level)
266+
self:OnDiagnostic(code, msg, severity, start, stop, node, level)
244267
end
245268

246269
if self.default_environment then
@@ -264,8 +287,7 @@ function META:Analyze(analyzer, ...)
264287

265288
if not ok then return nil, res end
266289

267-
if self.errors[1] then return nil, table.concat(self.errors, "\n") end
268-
290+
-- if self.errors[1] then return nil, table.concat(self.errors, "\n") end
269291
return self
270292
end
271293

@@ -288,7 +310,7 @@ function META.New(
288310
config--[[#: CompilerConfig]],
289311
level--[[#: number | nil]]
290312
)
291-
local path, line, name = callstack.get_path_line(level or 2)
313+
local path, line, parent_name = callstack.get_path_line(level or 2)
292314
path = path or "unknown name"
293315
line = line or "unknown line"
294316
name = name or path .. ":" .. line
@@ -305,7 +327,7 @@ function META.New(
305327
return META.NewObject(
306328
{
307329
Code = Code(lua_code, name),
308-
ParentSourceLine = parent_line,
330+
ParentSourceLine = line,
309331
ParentSourceName = parent_name,
310332
Config = config or false,
311333
Tokens = false,

nattlua/other/formating.lua

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -421,18 +421,19 @@ function formating.BuildSourceCodePointMessage2(
421421
if #v > longest_line then longest_line = #v end
422422
end
423423

424+
local total_len = longest_line - number_length - #SEPARATOR
425+
local title = config.title or ""
426+
427+
if title ~= "" then title = "__" .. title .. "__" end
428+
424429
annotated[1] = (
425430
" "
426-
):rep(number_length + #SEPARATOR) .. (
431+
):rep(number_length + #SEPARATOR) .. title .. (
427432
"_"
428-
):rep(longest_line - number_length - #SEPARATOR)
433+
):rep(math.max(0, total_len - #title))
429434
table.insert(
430435
annotated,
431-
(
432-
" "
433-
):rep(number_length + #SEPARATOR) .. (
434-
"-"
435-
):rep(longest_line - number_length - #SEPARATOR)
436+
(" "):rep(number_length + #SEPARATOR) .. ("-"):rep(total_len)
436437
)
437438
end
438439

0 commit comments

Comments
 (0)