Skip to content

Commit

Permalink
tools.vet: move error methods to vvet/errors.v (#21449)
Browse files Browse the repository at this point in the history
  • Loading branch information
ttytm authored May 7, 2024
1 parent 0a9d659 commit 9604cd5
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 64 deletions.
63 changes: 63 additions & 0 deletions cmd/tools/vvet/errors.v
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) 2019-2024 Alexander Medvednikov. All rights reserved.
// Use of this source code is governed by an MIT license that can be found in the LICENSE file.
import v.token
import term

pub enum ErrorKind {
error
Expand Down Expand Up @@ -34,3 +35,65 @@ pub:
fix FixKind @[required]
typ ErrorType @[required]
}

fn (mut vt Vet) error(msg string, line int, fix FixKind) {
pos := token.Pos{
line_nr: line + 1
}
vt.errors << VetError{
message: msg
file_path: vt.file
pos: pos
kind: .error
fix: fix
typ: .default
}
}

fn (mut vt Vet) warn(msg string, line int, fix FixKind) {
pos := token.Pos{
line_nr: line + 1
}
mut w := VetError{
message: msg
file_path: vt.file
pos: pos
kind: .warning
fix: fix
typ: .default
}
if vt.opt.is_werror {
w.kind = .error
vt.errors << w
} else {
vt.warns << w
}
}

fn (mut vt Vet) notice(msg string, line int, fix FixKind) {
pos := token.Pos{
line_nr: line + 1
}
vt.notices << VetError{
message: msg
file_path: vt.file
pos: pos
kind: .notice
fix: fix
typ: .default
}
}

fn (vt &Vet) e2string(err VetError) string {
mut kind := '${err.kind}:'
mut location := '${err.file_path}:${err.pos.line_nr}:'
if vt.opt.use_color {
kind = term.bold(match err.kind {
.warning { term.magenta(kind) }
.error { term.red(kind) }
.notice { term.yellow(kind) }
})
location = term.bold(location)
}
return '${location} ${kind} ${err.message}'
}
64 changes: 0 additions & 64 deletions cmd/tools/vvet/vvet.v
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import os
import os.cmdline
import v.pref
import v.parser
import v.token
import v.ast
import v.help
import term
Expand Down Expand Up @@ -325,21 +324,6 @@ fn (vt &Vet) vprintln(s string) {
println(s)
}

fn (vt &Vet) e2string(err VetError) string {
mut kind := '${err.kind}:'
mut location := '${err.file_path}:${err.pos.line_nr}:'
if vt.opt.use_color {
kind = match err.kind {
.warning { term.magenta(kind) }
.error { term.red(kind) }
.notice { term.yellow(kind) }
}
kind = term.bold(kind)
location = term.bold(location)
}
return '${location} ${kind} ${err.message}'
}

fn (mut vt Vet) vet_in_condition(expr ast.InfixExpr) {
if expr.right is ast.ArrayInit && expr.right.exprs.len == 1 && expr.op in [.key_in, .not_in] {
left := expr.left.str()
Expand All @@ -349,51 +333,3 @@ fn (mut vt Vet) vet_in_condition(expr ast.InfixExpr) {
expr.pos.line_nr, .vfmt)
}
}

fn (mut vt Vet) error(msg string, line int, fix FixKind) {
pos := token.Pos{
line_nr: line + 1
}
vt.errors << VetError{
message: msg
file_path: vt.file
pos: pos
kind: .error
fix: fix
typ: .default
}
}

fn (mut vt Vet) warn(msg string, line int, fix FixKind) {
pos := token.Pos{
line_nr: line + 1
}
mut w := VetError{
message: msg
file_path: vt.file
pos: pos
kind: .warning
fix: fix
typ: .default
}
if vt.opt.is_werror {
w.kind = .error
vt.errors << w
} else {
vt.warns << w
}
}

fn (mut vt Vet) notice(msg string, line int, fix FixKind) {
pos := token.Pos{
line_nr: line + 1
}
vt.notices << VetError{
message: msg
file_path: vt.file
pos: pos
kind: .notice
fix: fix
typ: .default
}
}

0 comments on commit 9604cd5

Please sign in to comment.