v vet: give an error for trailing whitespace (#9574)
parent
ddb2e72301
commit
46e7e27ba3
|
@ -5,7 +5,9 @@ import testing
|
||||||
import v.util
|
import v.util
|
||||||
|
|
||||||
const (
|
const (
|
||||||
vet_known_failing_exceptions = []string{}
|
vet_known_failing_exceptions = [
|
||||||
|
'vlib/v/gen/js/js.v' /* trailing space */,
|
||||||
|
]
|
||||||
vet_folders = [
|
vet_folders = [
|
||||||
'vlib/sqlite',
|
'vlib/sqlite',
|
||||||
'vlib/v',
|
'vlib/v',
|
||||||
|
@ -115,7 +117,8 @@ fn tsession(vargs string, tool_source string, tool_cmd string, tool_args string,
|
||||||
}
|
}
|
||||||
|
|
||||||
fn v_test_vetting(vargs string) {
|
fn v_test_vetting(vargs string) {
|
||||||
vet_session := tsession(vargs, 'vvet', 'v vet', 'vet', vet_folders, vet_known_failing_exceptions)
|
expanded_vet_list := util.find_all_v_files(vet_folders) or { return }
|
||||||
|
vet_session := tsession(vargs, 'vvet', 'v vet', 'vet', expanded_vet_list, vet_known_failing_exceptions)
|
||||||
fmt_cmd, fmt_args := if is_fix { 'v fmt -w', 'fmt -w' } else { 'v fmt -verify', 'fmt -verify' }
|
fmt_cmd, fmt_args := if is_fix { 'v fmt -w', 'fmt -w' } else { 'v fmt -verify', 'fmt -verify' }
|
||||||
expanded_vfmt_list := util.find_all_v_files(vfmt_verify_list) or { return }
|
expanded_vfmt_list := util.find_all_v_files(vfmt_verify_list) or { return }
|
||||||
verify_session := tsession(vargs, 'vfmt.v', fmt_cmd, fmt_args, expanded_vfmt_list,
|
verify_session := tsession(vargs, 'vfmt.v', fmt_cmd, fmt_args, expanded_vfmt_list,
|
||||||
|
|
|
@ -0,0 +1,7 @@
|
||||||
|
cmd/tools/vvet/tests/trailing_space.vv:5: error: Looks like you have trailing whitespace.
|
||||||
|
cmd/tools/vvet/tests/trailing_space.vv:6: error: Looks like you have trailing whitespace.
|
||||||
|
cmd/tools/vvet/tests/trailing_space.vv:7: error: Looks like you have trailing whitespace.
|
||||||
|
cmd/tools/vvet/tests/trailing_space.vv:8: error: Looks like you have trailing whitespace.
|
||||||
|
cmd/tools/vvet/tests/trailing_space.vv:9: error: Looks like you have trailing whitespace.
|
||||||
|
cmd/tools/vvet/tests/trailing_space.vv:13: error: Looks like you have trailing whitespace.
|
||||||
|
cmd/tools/vvet/tests/trailing_space.vv:15: error: Looks like you have trailing whitespace.
|
|
@ -0,0 +1,16 @@
|
||||||
|
// NB: This file has and *should* have trailing spaces.
|
||||||
|
// When making changes, please ensure they are not removed.
|
||||||
|
|
||||||
|
fn after_comments() {
|
||||||
|
// spaces after line comments give errors
|
||||||
|
/*
|
||||||
|
in block comments
|
||||||
|
too
|
||||||
|
*/
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
var := 'error about the spaces right there'
|
||||||
|
no_err := "inside multi line strings it's fine.
|
||||||
|
but not after"
|
||||||
|
}
|
|
@ -229,6 +229,7 @@ fn (mut vt Vet) error(msg string, line int, fix vet.FixKind) {
|
||||||
pos: pos
|
pos: pos
|
||||||
kind: .error
|
kind: .error
|
||||||
fix: fix
|
fix: fix
|
||||||
|
typ: .default
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -242,6 +243,7 @@ fn (mut vt Vet) warn(msg string, line int, fix vet.FixKind) {
|
||||||
pos: pos
|
pos: pos
|
||||||
kind: .warning
|
kind: .warning
|
||||||
fix: fix
|
fix: fix
|
||||||
|
typ: .default
|
||||||
}
|
}
|
||||||
if vt.opt.is_werror {
|
if vt.opt.is_werror {
|
||||||
w.kind = .error
|
w.kind = .error
|
||||||
|
|
|
@ -207,12 +207,16 @@ pub fn parse_vet_file(path string, table_ &ast.Table, pref &pref.Preferences) (a
|
||||||
global_scope: global_scope
|
global_scope: global_scope
|
||||||
}
|
}
|
||||||
p.set_path(path)
|
p.set_path(path)
|
||||||
if p.scanner.text.contains('\n ') {
|
if p.scanner.text.contains_any_substr(['\n ', ' \n']) {
|
||||||
source_lines := os.read_lines(path) or { []string{} }
|
source_lines := os.read_lines(path) or { []string{} }
|
||||||
for lnumber, line in source_lines {
|
for lnumber, line in source_lines {
|
||||||
if line.starts_with(' ') {
|
if line.starts_with(' ') {
|
||||||
p.vet_error('Looks like you are using spaces for indentation.', lnumber,
|
p.vet_error('Looks like you are using spaces for indentation.', lnumber,
|
||||||
.vfmt)
|
.vfmt, .space_indent)
|
||||||
|
}
|
||||||
|
if line.ends_with(' ') {
|
||||||
|
p.vet_error('Looks like you have trailing whitespace.', lnumber, .unknown,
|
||||||
|
.trailing_space)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -617,10 +621,10 @@ pub fn (mut p Parser) comment() ast.Comment {
|
||||||
pos.last_line = pos.line_nr + text.count('\n')
|
pos.last_line = pos.line_nr + text.count('\n')
|
||||||
p.next()
|
p.next()
|
||||||
is_multi := text.contains('\n')
|
is_multi := text.contains('\n')
|
||||||
// Filter out space indentation vet errors inside block comments
|
// Filter out false positive space indent vet errors inside comments
|
||||||
if p.vet_errors.len > 0 && is_multi {
|
if p.vet_errors.len > 0 && is_multi {
|
||||||
p.vet_errors = p.vet_errors.filter(!(it.pos.line_nr - 1 > pos.line_nr
|
p.vet_errors = p.vet_errors.filter(it.typ != .space_indent
|
||||||
&& it.pos.line_nr - 1 <= pos.last_line))
|
|| it.pos.line_nr - 1 > pos.last_line || it.pos.line_nr - 1 <= pos.line_nr)
|
||||||
}
|
}
|
||||||
return ast.Comment{
|
return ast.Comment{
|
||||||
is_multi: is_multi
|
is_multi: is_multi
|
||||||
|
@ -1648,7 +1652,7 @@ pub fn (mut p Parser) note_with_pos(s string, pos token.Position) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn (mut p Parser) vet_error(msg string, line int, fix vet.FixKind) {
|
pub fn (mut p Parser) vet_error(msg string, line int, fix vet.FixKind, typ vet.ErrorType) {
|
||||||
pos := token.Position{
|
pos := token.Position{
|
||||||
line_nr: line + 1
|
line_nr: line + 1
|
||||||
}
|
}
|
||||||
|
@ -1658,6 +1662,7 @@ pub fn (mut p Parser) vet_error(msg string, line int, fix vet.FixKind) {
|
||||||
pos: pos
|
pos: pos
|
||||||
kind: .error
|
kind: .error
|
||||||
fix: fix
|
fix: fix
|
||||||
|
typ: typ
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -2339,9 +2344,13 @@ fn (mut p Parser) string_expr() ast.Expr {
|
||||||
pos.last_line = pos.line_nr + val.count('\n')
|
pos.last_line = pos.line_nr + val.count('\n')
|
||||||
if p.peek_tok.kind != .str_dollar {
|
if p.peek_tok.kind != .str_dollar {
|
||||||
p.next()
|
p.next()
|
||||||
if p.vet_errors.len > 0 && val.contains('\n ') {
|
// Filter out false positive vet errors inside strings
|
||||||
p.vet_errors = p.vet_errors.filter(!(it.pos.line_nr > pos.line_nr - 1
|
if p.vet_errors.len > 0 {
|
||||||
&& it.pos.line_nr <= pos.last_line - 1))
|
p.vet_errors = p.vet_errors.filter(
|
||||||
|
(it.typ == .trailing_space && it.pos.line_nr - 1 >= pos.last_line)
|
||||||
|
|| (it.typ != .trailing_space && it.pos.line_nr - 1 > pos.last_line)
|
||||||
|
|| (it.typ == .space_indent && it.pos.line_nr - 1 <= pos.line_nr)
|
||||||
|
|| (it.typ != .space_indent && it.pos.line_nr - 1 < pos.line_nr))
|
||||||
}
|
}
|
||||||
node = ast.StringLiteral{
|
node = ast.StringLiteral{
|
||||||
val: val
|
val: val
|
||||||
|
|
|
@ -447,7 +447,8 @@ fn (mut p Parser) infix_expr(left ast.Expr) ast.Expr {
|
||||||
p.expecting_type = prev_expecting_type
|
p.expecting_type = prev_expecting_type
|
||||||
if p.pref.is_vet && op in [.key_in, .not_in] && right is ast.ArrayInit
|
if p.pref.is_vet && op in [.key_in, .not_in] && right is ast.ArrayInit
|
||||||
&& (right as ast.ArrayInit).exprs.len == 1 {
|
&& (right as ast.ArrayInit).exprs.len == 1 {
|
||||||
p.vet_error('Use `var == value` instead of `var in [value]`', pos.line_nr, vet.FixKind.vfmt)
|
p.vet_error('Use `var == value` instead of `var in [value]`', pos.line_nr, vet.FixKind.vfmt,
|
||||||
|
.default)
|
||||||
}
|
}
|
||||||
mut or_stmts := []ast.Stmt{}
|
mut or_stmts := []ast.Stmt{}
|
||||||
mut or_kind := ast.OrKind.absent
|
mut or_kind := ast.OrKind.absent
|
||||||
|
|
|
@ -1376,6 +1376,7 @@ fn (mut s Scanner) vet_error(msg string, fix vet.FixKind) {
|
||||||
}
|
}
|
||||||
kind: .error
|
kind: .error
|
||||||
fix: fix
|
fix: fix
|
||||||
|
typ: .default
|
||||||
}
|
}
|
||||||
s.vet_errors << ve
|
s.vet_errors << ve
|
||||||
}
|
}
|
||||||
|
|
|
@ -15,6 +15,13 @@ pub enum FixKind {
|
||||||
vfmt
|
vfmt
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ErrorType is used to filter out false positive errors under specific conditions
|
||||||
|
pub enum ErrorType {
|
||||||
|
default
|
||||||
|
space_indent
|
||||||
|
trailing_space
|
||||||
|
}
|
||||||
|
|
||||||
pub struct Error {
|
pub struct Error {
|
||||||
pub mut:
|
pub mut:
|
||||||
kind ErrorKind [required]
|
kind ErrorKind [required]
|
||||||
|
@ -25,4 +32,5 @@ pub:
|
||||||
file_path string // file where the error have origin
|
file_path string // file where the error have origin
|
||||||
pos token.Position // position in the file
|
pos token.Position // position in the file
|
||||||
fix FixKind [required]
|
fix FixKind [required]
|
||||||
|
typ ErrorType [required]
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue