mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
type name check fixes
This commit is contained in:
parent
8bce5cb810
commit
ee437de8d3
@ -915,17 +915,6 @@ fn (p mut Parser) fn_call_args(f mut Fn) *Fn {
|
||||
// p.gen(')')
|
||||
}
|
||||
|
||||
fn contains_capital(s string) bool {
|
||||
// for c in s {
|
||||
for i := 0; i < s.len; i++ {
|
||||
c := s[i]
|
||||
if c >= `A` && c <= `Z` {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// "fn (int, string) int"
|
||||
fn (f Fn) typ_str() string {
|
||||
mut sb := strings.new_builder(50)
|
||||
|
@ -492,6 +492,9 @@ fn (p mut Parser) struct_decl() {
|
||||
p.check(.dot)
|
||||
name = p.check_name()
|
||||
}
|
||||
if !is_c && !good_type_name(name) {
|
||||
p.error('bad struct name, e.g. use `HttpRequest` instead of `HTTPRequest`')
|
||||
}
|
||||
// Specify full type name
|
||||
if !is_c && !p.builtin_pkg && p.mod != 'main' {
|
||||
name = p.prepend_pkg(name)
|
||||
@ -592,7 +595,7 @@ fn (p mut Parser) struct_decl() {
|
||||
if field_name in names {
|
||||
p.error('duplicate field `$field_name`')
|
||||
}
|
||||
if p.mod != 'os' && contains_capital(field_name) {
|
||||
if !is_c && p.mod != 'os' && contains_capital(field_name) {
|
||||
p.error('struct fields cannot contain uppercase letters, use snake_case instead')
|
||||
}
|
||||
names << field_name
|
||||
|
@ -758,3 +758,29 @@ fn (s mut Scanner) create_type_string(T Type, name string) {
|
||||
s.inside_string = inside_string
|
||||
}
|
||||
|
||||
fn contains_capital(s string) bool {
|
||||
// for c in s {
|
||||
for i := 0; i < s.len; i++ {
|
||||
c := s[i]
|
||||
if c >= `A` && c <= `Z` {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// HTTPRequest bad
|
||||
// HttpRequest good
|
||||
fn good_type_name(s string) bool {
|
||||
if s.len < 4 {
|
||||
return true
|
||||
}
|
||||
for i in 2 .. s.len {
|
||||
if s[i].is_capital() && s[i-1].is_capital() && s[i-2].is_capital() {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
|
||||
|
@ -188,6 +188,10 @@ pub fn (c byte) str() string {
|
||||
return str
|
||||
}
|
||||
|
||||
pub fn (c byte) is_capital() bool {
|
||||
return c >= `A` && c <= `Z`
|
||||
}
|
||||
|
||||
pub fn (b []byte) clone() []byte {
|
||||
mut res := [byte(0); b.len]
|
||||
for i := 0; i < b.len; i++ {
|
||||
|
Loading…
Reference in New Issue
Block a user