Skip to content

Commit c1205a4

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

5 files changed

Lines changed: 125 additions & 43 deletions

File tree

nattlua/analyzer/base/error_handling.lua

Lines changed: 71 additions & 11 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,6 +111,18 @@ return function(META--[[#: any]])
109111

110112
local msg_str = error_messages.ErrorMessageToString(msg)
111113

114+
if
115+
not _G.TEST and
116+
severity == "error" and
117+
(
118+
msg_str:find("does not exist", nil, true) or
119+
msg_str:find("has no key", nil, true)
120+
)
121+
then
122+
severity = "warning"
123+
level = 1
124+
end
125+
112126
if self.processing_deferred_calls then
113127
msg_str = "DEFERRED CALL: " .. msg_str
114128
end
@@ -129,7 +143,16 @@ return function(META--[[#: any]])
129143
end
130144

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: 39 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -64,12 +64,10 @@ 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-
70+
msg = formating.FormatMessage(msg, ...)
7371
local messages = {}
7472

7573
if self.analyzer then
@@ -90,19 +88,44 @@ function META:OnDiagnostic(code, msg, severity, start, stop, node, ...)
9088
table.insert(messages, path .. ":" .. info.line_start .. ":" .. info.character_start)
9189
else
9290
for k, v in pairs(v.obj) do
93-
print(k, v)
91+
92+
-- print(k, v)
9493
end
9594
end
9695
end
9796
end
9897
end
9998

100-
table.insert(messages, formating.FormatMessage(msg, ...))
101-
local msg = formating.BuildSourceCodePointMessage2(
102-
code:GetString(),
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
@@ -178,7 +201,7 @@ end
178201
function META:Lex()
179202
local lexer = Lexer(self.Code, self.Config and self.Config.lexer)
180203
lexer.OnError = function(lexer, code, msg, start, stop, ...)
181-
self:OnDiagnostic(code, msg, "fatal", start, stop, nil, ...)
204+
self:OnDiagnostic(code, msg, "fatal", start, stop, nil, nil, ...)
182205
end
183206
local ok, tokens = xpcall(function()
184207
return lexer:GetTokens()
@@ -201,7 +224,7 @@ function META:Parse()
201224

202225
local parser = Parser(self.Tokens, self.Code, self.Config and self.Config.parser)
203226
parser.OnError = function(parser, code, msg, start, stop, ...)
204-
self:OnDiagnostic(code, msg, "fatal", start, stop, nil, ...)
227+
self:OnDiagnostic(code, msg, "fatal", start, stop, nil, nil, ...)
205228
end
206229
parser.OnPreCreateNode = function(_, node)
207230
self:OnPreCreateNode(node)
@@ -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
@@ -288,7 +311,7 @@ function META.New(
288311
config--[[#: CompilerConfig]],
289312
level--[[#: number | nil]]
290313
)
291-
local path, line, name = callstack.get_path_line(level or 2)
314+
local path, line, parent_name = callstack.get_path_line(level or 2)
292315
path = path or "unknown name"
293316
line = line or "unknown line"
294317
name = name or path .. ":" .. line
@@ -305,7 +328,7 @@ function META.New(
305328
return META.NewObject(
306329
{
307330
Code = Code(lua_code, name),
308-
ParentSourceLine = parent_line,
331+
ParentSourceLine = line,
309332
ParentSourceName = parent_name,
310333
Config = config or false,
311334
Tokens = false,

nattlua/other/formating.lua

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -421,19 +421,17 @@ 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)
429-
table.insert(
430-
annotated,
431-
(
432-
" "
433-
):rep(number_length + #SEPARATOR) .. (
434-
"-"
435-
):rep(longest_line - number_length - #SEPARATOR)
436-
)
433+
):rep(math.max(0, total_len - #title))
434+
table.insert(annotated, (" "):rep(number_length + #SEPARATOR) .. ("-"):rep(total_len))
437435
end
438436

439437
local header = config.show_line_numbers == false and

0 commit comments

Comments
 (0)