v / cmd / tools / vvet / errors.v
127 lines · 118 sloc · 2.39 KB · 45b79dfb9711cec3b3494c54c0f0f087e241d503
Raw
1// Copyright (c) 2019-2024 Alexander Medvednikov. All rights reserved.
2// Use of this source code is governed by an MIT license that can be found in the LICENSE file.
3import v.token
4import term
5
6pub enum ErrorKind {
7 error
8 warning
9 notice
10}
11
12pub enum FixKind {
13 unknown
14 doc
15 vfmt
16 repeated_code
17 long_fns
18 empty_fn
19 inline_fn
20}
21
22// ErrorType is used to filter out false positive errors under specific conditions
23pub enum ErrorType {
24 default
25 space_indent
26 trailing_space
27}
28
29@[minify]
30pub struct VetError {
31pub mut:
32 kind ErrorKind @[required]
33pub:
34 // General message
35 message string @[required]
36 details string // Details about how to resolve or fix the situation
37 file_path string // file where the error have origin
38 pos token.Pos // position in the file
39 fix FixKind @[required]
40 typ ErrorType @[required]
41}
42
43fn (mut vt Vet) error(msg string, line int, fix FixKind) {
44 pos := token.Pos{
45 line_nr: line + 1
46 }
47 lock vt.errors {
48 vt.errors << VetError{
49 message: msg
50 file_path: vt.file
51 pos: pos
52 kind: .error
53 fix: fix
54 typ: .default
55 }
56 }
57}
58
59fn (mut vt Vet) warn(msg string, line int, fix FixKind) {
60 pos := token.Pos{
61 line_nr: line + 1
62 }
63 mut w := VetError{
64 message: msg
65 file_path: vt.file
66 pos: pos
67 kind: .warning
68 fix: fix
69 typ: .default
70 }
71 if vt.opt.is_werror {
72 w.kind = .error
73 lock vt.errors {
74 vt.errors << w
75 }
76 } else {
77 lock vt.warns {
78 vt.warns << w
79 }
80 }
81}
82
83fn (mut vt Vet) notice(msg string, line int, fix FixKind) {
84 pos := token.Pos{
85 line_nr: line + 1
86 }
87 lock vt.notices {
88 vt.notices << VetError{
89 message: msg
90 file_path: vt.file
91 pos: pos
92 kind: .notice
93 fix: fix
94 typ: .default
95 }
96 }
97}
98
99fn (mut vt Vet) notice_with_file(file string, msg string, line int, fix FixKind) {
100 pos := token.Pos{
101 line_nr: line + 1
102 }
103 lock vt.notices {
104 vt.notices << VetError{
105 message: msg
106 file_path: file
107 pos: pos
108 kind: .notice
109 fix: fix
110 typ: .default
111 }
112 }
113}
114
115fn (vt &Vet) e2string(err VetError) string {
116 mut kind := '${err.kind}:'
117 mut location := '${err.file_path}:${err.pos.line_nr}:'
118 if vt.opt.use_color {
119 kind = term.bold(match err.kind {
120 .warning { term.magenta(kind) }
121 .error { term.red(kind) }
122 .notice { term.yellow(kind) }
123 })
124 location = term.bold(location)
125 }
126 return '${location} ${kind} ${err.message}'
127}
128